【问题标题】:jCarouselLite reset autoscroll intervaljCarouselLite 重置自动滚动间隔
【发布时间】:2010-12-24 05:13:30
【问题描述】:

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

在第 274-277 行的 jCarouselLite source code 中,使用 setInterval 实现了自动滚动。我知道如果你有 setInterval 返回的 ID,你可以使用clearInterval,但是除了修改源代码之外我没有其他办法,我不想这样做。

有什么想法吗?谢谢!

【问题讨论】:

    标签: javascript jquery autoscroll jcarousellite


    【解决方案1】:

    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);
        });
    });
    

    【讨论】:

    • 是的,基本上我只想有一个机制来控制自己的自动滚动。我只想创建自己的事件来暂停、恢复、完全停止和重新启动自动滚动。您使用自定义代码模拟自动滚动行为的解决方案将是完美的!谢谢!
    【解决方案2】:

    这是一个内置鼠标悬停暂停的版本。效果很好。 http://github.com/cheald/jcarousel-lite

    【讨论】:

      【解决方案3】:

      这些答案都不是我想要的,但这是我在谷歌搜索“jcarouselite 重置计时器”时出现的问题,所以对于下一个人来说:

      • 单击上一个/下一个滑动按钮时重置计时器
      • 悬停时暂停幻灯片

      那么这就是我整理的对我有用的东西:

      (function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var running=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var div=$(this),a=$("#featuredlistings a.next"),ul=$("ul",div),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v;}var li=$("li",ul),itemLength=li.size(),curr=o.start;div.css("visibility","visible");li.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});div.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var liSize=o.vertical?height(li):width(li);var ulSize=liSize*itemLength;var divSize=liSize*v;li.css({width:li.width(),height:li.height()});ul.css(sizeCss,ulSize+"px").css(animCss,-(curr*liSize));div.css(sizeCss,divSize+"px");if(o.btnPrev)$(o.btnPrev).click(function(){resetAuto(); return go(curr-o.scroll);});if(o.btnNext)$(o.btnNext).click(function(){resetAuto(); return go(curr+o.scroll);});if(o.btnGo)$.each(o.btnGo,function(i,val){$(val).click(function(){return go(o.circular?o.visible+i:i);});});if(o.mouseWheel&&div.mousewheel)div.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll);});if(o.auto){autoScroll=setInterval(function(){go(curr+o.scroll);},o.auto+o.speed);function resetAuto(){clearInterval(autoScroll);autoScroll=setInterval(function(){go(curr+o.scroll);},o.auto+o.speed);};div.hover(function(){clearInterval(autoScroll);},function(){autoScroll=setInterval(function(){go(curr+o.scroll);},o.auto+o.speed);});}function vis(){return li.slice(curr).slice(0,v);};function go(to){if(!running){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(to<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*liSize)+"px");curr=to==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll;}else if(to>=itemLength-v+1){ul.css(animCss,-((v)*liSize)+"px");curr=to==itemLength-v+1?v+1:v+o.scroll;}else curr=to;}else{if(to<0||to>itemLength-v)return;else curr=to;}running=true;ul.animate(animCss=="left"?{left:-(curr*liSize)}:{top:-(curr*liSize)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());running=false;});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled");}}return false;};});};function css(el,prop){return parseInt($.css(el[0],prop))||0;};function width(el){return el[0].offsetWidth+css(el,'marginLeft')+css(el,'marginRight');};function height(el){return el[0].offsetHeight+css(el,'marginTop')+css(el,'marginBottom');};})(jQuery);
      

      只需将其替换为您当前的 jCarouselLite 脚本并照此使用即可。

      【讨论】:

        【解决方案4】:

        如果您能够/被授权更改插件代码:

        添加一个变量以将间隔 id 保存到插件默认值

        interval: null
        

        搜索:

        if(o.auto)
        

        获取此处执行的代码并使用它创建一个内部函数,例如:

        function runAuto() {
            setInterval(function() {
                go(curr+o.scroll);
            }, o.auto+o.speed);
        }
        

        现在只需将间隔保存到您定义的变量中,但先清除它:

        function runAuto() {
            clearInterval(o.interval);
            o.interval = setInterval(function() {
                go(curr+o.scroll);
            }, o.auto+o.speed);
        }
        

        在插件中搜索go()函数并添加runAuto(),这样每次调用函数go都会重置间隔。

        当然,您还必须将runAuto() 调用添加到if(o.auto),以便间隔首先开始。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-26
          • 1970-01-01
          相关资源
          最近更新 更多