幻灯片放映有一种奇怪的行为

时间:2016-11-28 09:42:17

标签: javascript slideshow

自上次幻灯片以来,我的幻灯片放映(Javascript)正常运行,但重新启动时,不会停止指定的冻结时间。这是我的js代码:

var pos;
var slides;
var timeOut;
var slideTime = 3000;

window.onload = function (){
    pos = -1;
    var slideContent = document.getElementById("slides");
    slides = slideContent.getElementsByTagName("article");
    initSlides();
};

function initSlides(){
    if (pos !== -1){
        pos++;
        if (pos === slides.length){
            pos = 0;
            exitSlide(slides[slides.length-1], slides[pos]);
        }else{
            exitSlide(slides[pos-1], slides[pos]);
        }
    }else{
        pos ++;
        slides[pos].style.display = "block";
        timeOut = setTimeout(initSlides, slideTime);
    }
}

function changeSlide(next){
    if (next){
        pos++;
        if (pos === slides.length){
            pos = 0;
            exitSlide(slides[slides.length-1], slides[pos]);
        }else{
            exitSlide(slides[pos-1], slides[pos]);
        }
    }else{
        pos --;
        if (pos === -1){
            pos = slides.length - 1;
        }
    }
}

function enterSlide(obj){
    obj.style.display = "block";
    var left = obj.offsetLeft;
    var current = obj.offsetWidth;
    obj.style.marginLeft = current + "px";
    function frame(){
        current -= obj.offsetWidth/120;
        if (current >= left){
            obj.style.marginLeft = current + "px";
        }else{
            clearInterval(id);
            timeOut = setTimeout(initSlides, slideTime);
        }
    }

    var id = setInterval(frame, 1);
}

function exitSlide(obj, objNext){
    clearTimeout(timeOut);
    var left = obj.offsetLeft;
    var current = -obj.offsetWidth;
    function frame(){
        left += current/120;
        if (left >= current){
            obj.style.marginLeft = left + "px";
        }else{
            clearInterval(id);
            obj.style.display = "none";
            enterSlide(objNext);
        }
    }

    var id = setInterval(frame, 1);
}

检查代码控制台没有给我任何错误,但幻灯片放映是如此错误。由于第二次转弯,幻灯片不会停止,一段时间后,幻灯片有时会停止,有时会显示但没有交易,有时意味着工作正常,但下一张幻灯片也有问题。我不知道为什么会这样。如果可以,请帮助我。

1 个答案:

答案 0 :(得分:0)

最后,我坚持尝试设计我自己的幻灯片,我正在使用这个插件:Slick - the last carousel