javascript - jCarouselLite 重置自动滚动间隔

标签 javascript jquery autoscroll jcarousellite

如何重置 jCarouselLite 上的自动滚动间隔?在某个事件之后轮播,以便它可以让您查看整个时间间隔的内容,而不管您单击下一个或上一个时计时器已经走了多远?现在,如果我在 9 秒后单击下一个或上一个,它会在 1 秒后再次滚动。

jCarouselLite source code第 274-277 行是使用 setInterval 实现自动滚动的地方。我知道如果你有 setInterval 返回的 ID,你可以使用 clearInterval,但除了修改源代码之外我无法获得任何 ID,我不想那样做。

有什么想法吗?谢谢!

最佳答案

jCarouselLite 本身没有提供任何简单的方法来停止自动滚动,这是一个更容易的问题,然后做你想做的事(?我理解的对吗:你只是想让自动滚动在点击和然后继续)

Hacky + 完全停止自动滚动的潜在错误方法

var x; //hold interval id
$(function() {
    var y = window.setInterval; //backup original setInterval function
    //overwrite with new function which stores the id for us
    window.setInterval = function() {
        x = y(arguments[0], arguments[1]);
        return x; 
    };
    //now construct carousel
    $(".anyClass").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        auto: 500 
    });
    //now restore original setInterval function
    //as we only needed the custom one for the carousel to capture the hidden
    //internal call to setInterval
    window.setInterval = y;
});
$("#stopAutoScrollButton").click(function() {
    clearInterval(x);
});

真正的解决方案

由于我们无法让 jCarouselLite 自行执行此操作,因此我们自己模拟了 auto 行为。

$(function() {
    var autoTime = 5000; //5s
    $(".anyClass").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev"
    });
    //simulate autoscroll by simulating "click" on next link
    var x = setInterval("$('.next').trigger('click');", autoTime);
    //if stopAuto is clicked the autoscroll is suspended for autoTime
    //no matter how far along the timer already was
    $("#stopAuto").click(function() {
        clearInterval(x);
        x = setInterval("$('.next').trigger('click');", autoTime);
    });
});

关于javascript - jCarouselLite 重置自动滚动间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1863373/

相关文章:

JavaScript 不断重新加载页面

javascript - 从原型(prototype)继承与从构造函数继承

jquery - 是否可以在 HTML 输入类型=提交值中显示 unicode 字符?

java - 动态添加 JPanel 时 JScrollPane 自动向下滚动

ios - 拖放工作 - 自动滚动不

c# - .net 滚动条自动滚动问题

javascript - onClick <li> 中的项目更改颜色

javascript - 如何在不循环的情况下更新数组内的 JS 对象值?

javascript - d3 强制仅在单击时显示某些链接和节点

javascript - 切换类事件无法正常工作