onclick event change background-image with fade transition

邮差的信 提交于 2019-12-08 02:15:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!