javascript 如何:基于点击事件暂停和播放 flexslider (jQuery)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17309561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Howto: Pause and Play flexslider based on click event (jQuery)
提问by sleeper
- I am trying to bind the
slider.pause()
andslider.play()
events to my buttons (see code below). - It works unless I click the play button twiceor I click the play button while the slider is running.
- Then it seems to run anotherinstance (or something) as it runs at twice the speed and the pause button no longer stops slider
- 我正在尝试将
slider.pause()
和slider.play()
事件绑定到我的按钮(请参阅下面的代码)。 - 除非我单击播放按钮两次或在滑块运行时单击播放按钮,否则它会起作用。
- 然后它似乎运行另一个实例(或其他东西),因为它以两倍的速度运行并且暂停按钮不再停止滑块
Question: Is there a way to test whether the slider is running before calling slider.play()
or are the pause() and/or play() calls in the wrong place?
问题:有没有办法在调用之前测试滑块是否正在运行,slider.play()
或者 pause() 和/或 play() 调用是否在错误的位置?
Please advise.
请指教。
$(document).ready(function(){
$('.flexslider').flexslider({
animation: "fade",
slideshowSpeed: 2000,
pauseOnHover: false,
touch: true,
controlsContainer: ".fs-container",
controlNav: true,
manualControls: ".flex-control-nav li",
start: function(slider) {
$('.icon-pause').click(function(){
slider.pause();
});
$('.icon-play').click(function(){
slider.play();
});
}
});
});
采纳答案by Kyle
7 months old question, but yea the start
property on flexslider fires every time an animation starts.
7 个月大的问题,但是start
每次动画开始时 flexslider 上的属性都会触发。
So the code you have above is binding rebinding the click event.
所以你上面的代码是绑定重新绑定点击事件。
Instead you want something like:
相反,你想要这样的东西:
$('.icon-pause').click(function(){
$('#your_slider_id').pause();
});
And you'd put this in your page init functions -- or anywhere, just not inside the flexslider initialization.
你可以把它放在你的页面初始化函数中——或者任何地方,只是不在 flexslider 初始化中。
(Of course .play()
is also a method.)
(当然.play()
也是一种方法。)
回答by priyaqb
Try:
尝试:
$('.flex-slider').flexslider('pause');
and $('.flex-slider').flexslider('play');
.
$('.flex-slider').flexslider('pause');
和$('.flex-slider').flexslider('play');
。