Change the body background image with fade effect in jquery

后端 未结 7 1668
说谎
说谎 2020-12-01 09:40

Hey i want to fade in a new background image let´s say every 60 seconds. I´ve set the background image like this:

body {background-image: url(background.jpg)         


        
7条回答
  •  不知归路
    2020-12-01 10:20

    Building on Deryck's answer...

    If jQuery Color does not function properly (which it sometimes does), you can use fadeIn() and fadeOut() instead:

     $(document).ready(function () {
        var img_array = [1, 2, 3, 4, 5],
            newIndex = 0,
            index = 0,
            interval = 5000;
        (function changeBg() {
    
            //  --------------------------
            //  For random image rotation:
            //  --------------------------
    
                //  newIndex = Math.floor(Math.random() * 10) % img_array.length;
                //  index = (newIndex === index) ? newIndex -1 : newIndex;
    
            //  ------------------------------
            //  For sequential image rotation:
            //  ------------------------------
    
                index = (index + 1) % img_array.length;
    
            $('body').css('backgroundImage', function () {
                $('#fullPage').fadeOut(1000, function () {
                    setTimeout(function () {
                        $('#fullPage').fadeIn(1000);
                    }, 3000);
                });
                return 'url(http://www.fleeceitout.com/images/field.' + img_array[index] + '.jpg)';
            });
            setTimeout(changeBg, interval);
        })();
    });
    

提交回复
热议问题