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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:49:59  来源:igfitidea点击:

Howto: Pause and Play flexslider based on click event (jQuery)

javascriptjqueryjquery-pluginsflexslider

提问by sleeper

  1. I am trying to bind the slider.pause()and slider.play()events to my buttons (see code below).
  2. It works unless I click the play button twiceor I click the play button while the slider is running.
  3. Then it seems to run anotherinstance (or something) as it runs at twice the speed and the pause button no longer stops slider
  1. 我正在尝试将slider.pause()slider.play()事件绑定到我的按钮(请参阅下面的代码)。
  2. 除非我单击播放按钮两次在滑块运行时单击播放按钮否则它会起作用。
  3. 然后它似乎运行另一个实例(或其他东西),因为它以两倍的速度运行并且暂停按钮不再停止滑块

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 startproperty 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');