将Autoscroll添加到JavaScript滑块

时间:2012-11-05 21:36:13

标签: javascript jquery slider carousel jquery-slider

我有一个简单的问题,我似乎无法弄清楚。我在网站working on中有一个JavaScript滑块。您需要手动单击此滑块中的下一个图像。我想让滑块自动滚动图像。以下是我尝试过的代码:

//Carousel interaction
(function() {
    if (document.getElementById('carousel-nav')) {
        var slides = document.getElementById('carousel-images'),
            slidesItems = slides.getElementsByTagName('li'),
            nav = document.getElementById('carousel-nav'),
            navItems = nav.getElementsByTagName('li'),
            current = 0;

        function showSlide(i) {
            if (i != current && slidesItems[i]) {
                slide = slidesItems[i];
                slide.className += ' show';
                setTimeout (function() {
                    slide.className = slide.className.replace('show', 'appear');                    
                }, 1);
                setTimeout(function() {
                    slidesItems[current].className = slidesItems[current].className.replace('current', '');
                    slide.className = slide.className.replace('appear', 'current');
                    current = i;
                }, 300);
                navItems[i+1].className += ' current';
                navItems[current+1].className = navItems[current+1].className.replace('current', '');
                if (i == 0) {
                    if (navItems[0].className.indexOf('disabled') == -1) {
                        navItems[0].className += ' disabled';
                    }
                } else {
                    navItems[0].className = navItems[0].className.replace(' disabled', '');
                }
                var l = navItems.length - 1;
                if (i == slidesItems.length - 1) {
                    if (navItems[l].className.indexOf('disabled') == -1) {
                        navItems[l].className += ' disabled';
                    }
                } else {
                    navItems[l].className = navItems[l].className.replace(' disabled', '');
                }
            }
        }   

        nav.onclick = function(e) {
            e = e || window.event; e = e.target || e.srcElement;
            e = getParentByTagName(e, 'A');
            if (e) {
                var action = e.getAttribute('data-action');
                if (action == 'prev') {
                    showSlide(current - 1);
                } else if (action == 'next') {
                    showSlide(current + 1);
                } else {
                    showSlide(parseInt(action));
                }
            }
            return false;
        }
    }
})();

我尝试将setInterval(current+1, 10000);添加到底部代码块中:

nav.onclick = function(e) {
        e = e || window.event; e = e.target || e.srcElement;
        e = getParentByTagName(e, 'A');
        if (e) {
            var action = e.getAttribute('data-action');
            if (action == 'prev') {
                showSlide(current - 1);
            } else if (action == 'next') {
                showSlide(current + 1);
            } else {
                showSlide(parseInt(action));
            }
        }
        return false;
    }

    setInterval(current+1, 10000);
}
})();

我是JavaScript的新手,无法弄清楚我做错了什么。我真的很感激任何帮助!谢谢。

2 个答案:

答案 0 :(得分:1)

您无法将值传递给setInterval,您必须传递一个要执行的函数。试试这个:

setInterval('showSlide(current + 1);', 10000);

此致

答案 1 :(得分:1)

另一个答案很接近,真的帮我搞清楚了。唯一的问题是语法有点偏。

setInterval(function() {
            showSlide(current + 1)
        },5000);

以上是最终对我有用的东西。所以这就是它在上面代码中的表现:

nav.onclick = function(e) {
        e = e || window.event; e = e.target || e.srcElement;
        e = getParentByTagName(e, 'A');
        if (e) {
            var action = e.getAttribute('data-action');
            if (action == 'prev') {
                showSlide(current - 1);
            } else if (action == 'next') {
                showSlide(current + 1);
            } else {
                showSlide(parseInt(action));
            }
        }
        return false;
    }

}
    setInterval(function() { showSlide(current + 1) }, 5000);
})();

下一步是弄清楚滑块到达最后一张幻灯片后如何重复播放到第一张幻灯片。

再次感谢Andrea的帮助!