jCarousellite AutoScrollインターバルをリセットします
-
16-09-2019 - |
質問
自動スクロール間隔をリセットするにはどうすればよいですか jcarousellite いくつかのイベントの後のCarouselは、次のクリックしたときにタイマーに沿ってどれくらいの距離であったかに関係なく、完全な間隔のコンテンツを見ることができるようにしますか?現在、9秒後に次または前のクリックをクリックすると、1秒後に再びスクロールします。
の中に jcarouselliteソースコード 274〜277行目には、自動スクロールが使用されている場所です。 setInterval
. 。私はあなたが使用できることを知っています clearInterval
IDがSetIntervalによって返された場合、ソースコードの変更以外に取得できるものはありません。それを行いたくありません。
何か案は?ありがとう!
解決
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);
});
});
他のヒント
これは、マウスオーバーが組み込まれているために一時停止したバージョンです。うまく機能します。http://github.com/cheald/jcarousel-lite
これらの答えのいずれも私が探していたものではありませんでしたが、これは私が「JCarouselliteリセットタイマー」をグーグルでグーグルするときに起こるものです。
- 以前/次のスライドボタンをクリックすると、タイマーをリセットします
- ホバーでスライドショーを一時停止します
それならこれは私が私のために働くものであるものです:
(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スクリプトと交換して、同じように使用してください。
プラグインコードを変更できる/許可されている場合:
変数を追加して、インターバル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()
プラグインで機能し、aを追加します runAuto()
, 、したがって、関数が移動するたびに、間隔をリセットすると呼ばれます。
もちろん、追加する必要があります runAuto()
に呼び出します if(o.auto)
したがって、間隔は最初から始まります。