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
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.
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.
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-image
s, but div
elements containing the image, positioned using position: absolute
(or fixed
) and z-index
for stacking. You would then animate those div
s.
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With