bobighorus
bobighorus

Reputation: 1152

onclick event change background-image with fade transition

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.

Upvotes: 2

Views: 7437

Answers (1)

Luca Filosofi
Luca Filosofi

Reputation: 31173

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);
        });
    });
});

Upvotes: 3

Related Questions