Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate background image change with jQuery

I finally have this working now but would like to know how I can use JQuery's animate function to make the background image changes fade in nicely when you hover over the list items on the homepage:-

http://www.thebalancedbody.ca/

The Code to make this happen so far is:-

$("ul#frontpage li#277 a").hover(     function() {         $('#homepage_container').css('background-image', 'url(http://www.thebalancedbody.ca/wp-content/themes/balancedbody_V1/images/nutrition_background.jpg)');     },     function() {         $('#homepage_container').css('background-image', 'url(http://www.thebalancedbody.ca/wp-content/themes/balancedbody_V1/images/default_background.jpg)');     } );  $("ul#frontpage li#297 a").hover(     function() {         $('#homepage_container').css('background-image', 'url(http://www.thebalancedbody.ca/wp-content/themes/balancedbody_V1/images/vibration_training.jpg)');     },     function() {         $('#homepage_container').css('background-image', 'url(http://www.thebalancedbody.ca/wp-content/themes/balancedbody_V1/images/default_background.jpg)');     } ); 

etc etc

How would I add the ANIMATE function to this please - thanks!!!

Thanks

Jonathan

like image 343
Jonathan Lyon Avatar asked Jun 06 '10 11:06

Jonathan Lyon


People also ask

How to animate background image in jQuery?

The scrollBg() function is used to change the background image position by using jQuery. In the function, we have used Javascript setInterval() to change position for every 10 seconds. This function is used to change the direction of background image animation.

Can you animate background color in jQuery?

The working of the jQuery background colour animate – Suppose we have a div element and we need to apply the background colour animation effects. So we can use the animate() function as “$('#bid'). animate({ backgroundColor: “gray”, color: “white” })”, which will change the background colour of the selected element.


2 Answers

I don't think this can be done using jQuery's animate function because the background image does not have the necessary CSS properties to do such fading. jQuery can only utilize what the browser makes possible. (jQuery experts, correct me if I'm wrong of course.)

I guess you would have to work around this by not using genuine background-images, but div elements containing the image, positioned using position: absolute (or fixed) and z-index for stacking. You would then animate those divs.

like image 100
Pekka Avatar answered Sep 29 '22 19:09

Pekka


building on XGreen's approach above, with a few tweaks you can have an animated looping background. See here for example:

http://jsfiddle.net/srd76/36/

$(document).ready(function(){  var images = Array("http://placekitten.com/500/200",                "http://placekitten.com/499/200",                "http://placekitten.com/501/200",                "http://placekitten.com/500/199"); var currimg = 0;   function loadimg(){     $('#background').animate({ opacity: 1 }, 500,function(){          //finished animating, minifade out and fade new back in                    $('#background').animate({ opacity: 0.7 }, 100,function(){              currimg++;              if(currimg > images.length-1){                  currimg=0;              }              var newimage = images[currimg];              //swap out bg src                             $('#background').css("background-image", "url("+newimage+")");               //animate fully back in             $('#background').animate({ opacity: 1 }, 400,function(){                  //set timer for next                 setTimeout(loadimg,5000);              });          });      });    }   setTimeout(loadimg,5000);  }); 
like image 42
James Avatar answered Sep 29 '22 21:09

James