首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery滚动SlideShow

jQuery滚动SlideShow
EN

Stack Overflow用户
提问于 2012-04-24 12:12:20
回答 1查看 319关注 0票数 1

我目前正在创建一个滚动幻灯片从头开始,并遇到了一个问题。

当我多次按下next按钮时,幻灯片开始一起运行,如何确保下一张幻灯片等到当前幻灯片停止后才开始播放。

代码语言:javascript
复制
    var scrollAmount=910
    var image0Left=-scrollAmount;
    var image1Left=0;
    var image2Left=scrollAmount;
    var scrollSpeed0=2000;
    var scrollSpeed1=2000;
    var scrollSpeed2=2000;
    $(document).ready(function() {
        $("#left-arrow").click(showPreviousSlide);
        $("#right-arrow").click(showNextSlide);
    });

    function showPreviousSlide(){
        image0Left+=scrollAmount;
        if(image0Left > scrollAmount){
            image0Left=-scrollAmount;
            scrollSpeed0=0;
        }

        image1Left+=scrollAmount;
        if(image1Left > scrollAmount){
            image1Left=-scrollAmount;
            scrollSpeed1=0;
        }

        image2Left+=scrollAmount;
        if(image2Left > scrollAmount){
            image2Left=-scrollAmount;
            scrollSpeed2=0;
        }

        $("#slide0").animate({left: image0Left}, scrollSpeed0);
        scrollSpeed0=2000;
        $("#slide1").animate({left: image1Left}, scrollSpeed1);
        scrollSpeed1=2000;
        $("#slide2").animate({left: image2Left}, scrollSpeed2);
        scrollSpeed2=2000;
    }

    function showNextSlide() {
        image0Left-=scrollAmount;
        if(image0Left < -scrollAmount){
            image0Left=scrollAmount;
            scrollSpeed0=0;
        }

        image1Left-=scrollAmount;
        if(image1Left < -scrollAmount){
            image1Left=scrollAmount;
            scrollSpeed1=0;
        }

        image2Left-=scrollAmount;
        if(image2Left < -scrollAmount){
            image2Left=scrollAmount;
            scrollSpeed2=0;
        }


        $("#slide0").animate({left: image0Left}, scrollSpeed0);
        scrollSpeed0=2000;
        $("#slide1").animate({left: image1Left}, scrollSpeed1);
        scrollSpeed1=2000;
        $("#slide2").animate({left: image2Left}, scrollSpeed2);
        scrollSpeed2=2000;
    }

这就是所有的控制脚本代码。这是一个实际站点的链接。Site

每次调用showPreviousSlide或showNextSlide时,都会移动三张图像幻灯片。如何确保showPreviousSlide/showNextSlide函数的一次迭代在再次调用之前完成了幻灯片的移动?我删除了我的幻灯片div overfill:hidden,这样就可以更容易地看到我的幻灯片上发生了什么。

谢谢你的帮助。

摩根

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-24 12:38:37

您可以像这样将完成回调传递给.animate()

代码语言:javascript
复制
animationCompleted = false;
$("#slide0").animate({left: image0Left}, scrollSpeed0, function() {
    animationCompleted = true;
});

然后检查函数中animationCompleted的值:

代码语言:javascript
复制
function showPreviousSlide(){
    if (!animationCompleted) return;
    // ...
}

并查看docs以获取更多信息和示例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10291644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档