问题
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:
- DEMO: http://so.devilmaycode.it/onclick-event-change-background-image-with-fade-transition
CSS:
*{margin:0;padding:0}
html ,body{width:100%;height:100%;margin:0;padding:0;overflow:hidden}
#bg{position:absolute;height:100%;width:100%;margin:0;padding:0;z-index:0}
HTML:
<div><img id="bg" src="image.jpg" alt="" /></div>
JQUERY
var images = ["home_photo_welshboy.jpg","home_photo_jam343.jpg"];
$(function() {
$('.change').click(function(e) {
var image = images[Math.floor(Math.random()*images.length)];
$('#bg').parent().fadeOut(200, function() {
$('#bg').attr('src', 'http://l.yimg.com/g/images/'+image);
$(this).fadeIn(200);
});
});
});
来源:https://stackoverflow.com/questions/2999562/onclick-event-change-background-image-with-fade-transition