Hi to all. I'm searching for a trip in jQuery for changing the background-image of the body (or of a 100%-height "wrapper" div) on a click event of a class element, with a cross-fade transition effect. Is it possible? Thank you in advance.
+1
A:
UPDATED 2 improved fade effect!
DEMO: http://jsbin.com/egoku4/7
CSS:
* {
margin: 0;
padding: 0
}
html , body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
#background{
position:absolute;
height:100%;
width: 100%;
margin: 0;
padding: 0;
z-index: 0;
}
HTML:
<div><img id="background" src="image.jpg" alt="" /></div>
JQUERY
var images = [ "home_photo_welshboy.jpg", "home_photo_jam343.jpg", "home_photo_xdjio.jpg", "home_photo_ccgd.jpg" ];
$(function() {
$('.change').click(function(e) {
var image = images[Math.floor(Math.random()*images.length)];
$('#background').parent().fadeOut(200, function() {
$('#background').attr('src', 'http://l.yimg.com/g/images/'+image);
$(this).fadeIn(200);
});
});
});
aSeptik
2010-06-08 17:19:15
Looks nice, but on my system the old image fades out and the new one pops up at once. I think you could better place the new image behind the old one and then fade out the old image, so the new image seems to appear slowly.
Marcel Korpel
2010-06-08 20:51:27
Thank you for your answer, but I can't really see the fade transition effect between an image and another one.I'm on Firefox 3.6 on a Mac.
bobighorus
2010-06-11 21:03:47
see the updates! let me know! ;)
aSeptik
2010-06-11 21:22:05
Yes, I think now it works better!I noticed that you edit the Fade value. Thank you so much, you're very gentle! :)
bobighorus
2010-06-16 08:25:15
can you please check the answer! for sake! you are welcome!
aSeptik
2010-06-16 10:09:45