Trying to make multiple background images cycle through a slideshow with CSS and JQUERY

前端 未结 3 790
-上瘾入骨i
-上瘾入骨i 2020-11-28 16:14

I\'ve placed three background images in a div. I\'m trying to make all three of them cycle through on a timer that fades in and out. I\'ve actually found similar quesitons o

3条回答
  •  离开以前
    2020-11-28 16:59

    CSS

    #background-slideshow {
        transition: background 1.5s ease; 
        background-position: center top, center top, center top; 
        background-size: cover, cover, cover;
        background-repeat:  no-repeat, no-repeat, no-repeat;
        background-image: url(img/background-1.jpg), url(img/background-2.jpg), url(img/background-3.jpg);
    }
    

    JS

    var i = 1;
    setInterval(function() { 
        e = document.getElementById('background-slideshow');
        switch (i) {
            case 0:
                e.style.backgroundPosition = 'center top, center top, center top';
                break;
            case 1:
                e.style.backgroundPosition = window.innerWidth + 'px top, center top, center top';
                break;
            case 2:
                e.style.backgroundPosition = window.innerWidth + 'px top, ' + window.innerWidth + 'px top, center top';
                break;
        }
        i++;    
        if (i > 2) i = 0;   
    }, 3000);
    
    

提交回复
热议问题