February 27, 2012

Fade out a background-image

Question by user1235147

I have a simple image menu. There is a background-image which disappears on hover to show the menu items. I am looking for jQuery solution to fade out this image slowly. Any ideas?

HTML:

<div id="m1">
    <ul class="joomla-nav">
         <li>...</li>
    </ul>
</div>

CSS:

#m1 {
background-image:url('../images/m1.jpg');
width:320px;
height:210px; 
background-color:#1F91B7;
float:left;}

#m1:hover {
background-image:none;
background-color:transparent}

#m1:hover .joomla-nav {
display:block!important; }

#m1 .joomla-nav {
display:none!important; }

Many thanks!

Answer by Starx

jQuery’s .animate() is only able to animate numeric Values.

Unlike background-color a background-image is not represented by numbers, so it cannot be animated.

What you can do is use another div with no background. And then fade the current div, so that it will look like the current background is fading out.

...

Please fill the form - I will response as fast as I can!