How to loop a css background image with Jquery every few seconds?

前端 未结 3 467
清酒与你
清酒与你 2020-12-09 21:13

I would like to change a header css background image every few seconds, so its look like a slideshow.

For example first 2 seconds be:

body#home h1#s         


        
相关标签:
3条回答
  • 2020-12-09 21:31

    Now with fade

    Try this:

    var currentBackground = 0;
    var backgrounds = [];
    backgrounds[0] = '../images/header1.jpg';
    backgrounds[1] = '../images/header2.jpg';
    backgrounds[2] = '../images/header3.jpg';
    
    function changeBackground() {
        currentBackground++;
        if(currentBackground > 2) currentBackground = 0;
    
        $('body#home h1#siteH1').fadeOut(100,function() {
            $('body#home h1#siteH1').css({
                'background-image' : "url('" + backgrounds[currentBackground] + "')"
            });
            $('body#home h1#siteH1').fadeIn(100);
        });
    
    
        setTimeout(changeBackground, 2000);
    }
    
    $(document).ready(function() {
        setTimeout(changeBackground, 2000);        
    });
    
    0 讨论(0)
  • 2020-12-09 21:36

    checkout the queue functionality:

    jQuery Queue

    0 讨论(0)
  • 2020-12-09 21:47

    Late to the party, but here's what I just came up with for similar requirement.

    <script type="text/javascript">
    
        // populate image set
        var imageArray = [
            "1.jpg",
            "2.jpg",
            "3.jpg",
            "4.jpg"
        ];
    
        // in milliseconds
        var fadeSpeed   = 1000;
        var timeout     = 3000;
    
    
        function fadeInFront (i) {
        
            $('#faderFront').css( {
                "background-image" : "url(" + imageArray[i] + ")"
            });
        
            $('#faderFront').fadeIn(fadeSpeed);
        
            i++;
        
            if ( i == imageArray.length ) {
                i=0;
            }
    
            setTimeout(function() {
                fadeOutFront(i);
            },timeout);
        
        }
    
        function fadeOutFront (i){
        
            $('#faderBack').css( {
                "background-image" : "url(" + imageArray[i] + ")"
            });
        
            $('#faderFront').fadeOut(fadeSpeed);
        
            i++;
        
            if ( i == imageArray.length ) {
                i=0;
            }
        
            setTimeout(function() {
                fadeInFront(i);
            },timeout);
        
        }
        
        function preload(arrayOfImages) {
        
            $(arrayOfImages).each(function() {
        
                $('<img/>')[0].src = this;
        
            });
        
        }
    
        $(document).ready(function(){
    
            preload(imageArray);
    
            setTimeout(function() {
                fadeOutFront(0);
            }, timeout);
    
        });
    
    </script>
    
    <style type="text/css">
    
        .imageContainer {
            width: 700px;
            height: 400px;
            position: relative;
        }
        
        #faderBack,
        #faderFront,
        #inFrontOfBoth {
            position: absolute;
            top: 0; left: 0;
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            width: 100%;
            height: 100%;
        }
    
    </style>
       
    <div id="container">
    
        <div class="imageContainer">
            
            <div id="faderBack"></div>
            <div id="faderFront" style="background-image: url('1.jpg');"></div>
            <div id="inFrontOfBoth">Hello World</div>
    
        </div>
    
    </div>
    
    0 讨论(0)
提交回复
热议问题