hover - 仅在悬停时使用 jQuery Cycle

标签 hover jquery-cycle

我试图让 jQuery Cycle 仅在幻灯片悬停时运行(这与它们内置的功能相反)。

这是我所在的位置:http://jsfiddle.net/zSBMU/

$(document).ready(function() {

$('.slideshow').hover(
    function() {
        $(this).cycle({
            fx:     'fade',
            speed:   600,
            timeout: 300,
            pause:   0
        });
    },
    function(){
        $(this).cycle('stop');
    }
).trigger('hover');

​ });

第一次悬停时,效果很好,运行良好。但是,如果您尝试再次将鼠标悬停在同一个上,它只会经历一次淡入淡出,而不是再次循环。

有什么想法吗?

请忽略一些粗略的代码,在这里使用一个相当旧的主题,尝试清理它!

最佳答案

您正在使用“停止”并重新创建循环,因此您在对象上添加了多个循环。

您必须使用“暂停”和“恢复”。

示例如下:

var cycleConfigured = false;
$(document).ready(function() {

    $('.slideshow').hover(
        function() {
            if(cycleConfigured)
                $(this).cycle('resume');
            else
            {
                $(this).cycle({
                    fx:     'fade',
                    speed:   600,
                    timeout: 300,
                    pause:   0
                });
                cycleConfigured = true;
            }
        },
        function(){
            $(this).cycle('pause');
        }
    ).trigger('hover');

});​

变量cycleConfigured将用于控制我们的循环插件,以检查它是否已经实例化。 或者,您可以在 $(document).ready() 上创建它,然后像这样暂停它:

$(document).ready(function() {
    // configure the cycle plugin
    $('.slideshow').cycle({
         fx:     'fade',
         speed:   600,
         timeout: 300,
         pause:   0
    });
    $('.slideshow').cycle('pause'); // pause it right away.

    $('.slideshow').hover(
        function() {
                $(this).cycle('resume'); // start playing.
        },
        function(){
            $(this).cycle('pause'); // pause the slideshow.
        }
    ).trigger('hover');

});​

然后您需要做的就是在 out 上使用 $(this).cycle('pause')$(this).cycle('resume')就结束了。

有什么事情请告诉我。

关于hover - 仅在悬停时使用 jQuery Cycle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10199175/

相关文章:

jquery - .is (":hover") 自 jQuery 1.9 起已损坏 如何修复

html - <tr> 上的 onMouseOver 不覆盖带有背景图像的单元格

java - 将鼠标悬停在动画菜单上并单击 selenium 中的菜单项

javascript - jquery循环允许覆盖

javascript - Ember.js jquery 循环幻灯片 : zero elements found error

jquery - 无法修复的重叠

html - 悬停不能按预期工作

javascript - 有什么更好的选择来代替 jQuery 的 .is (‘:hover’ ) 在悬停在另一个 div 上时显示一个 div。

jQuery 循环问题

jquery - 带循环的 vimeo 幻灯片 - 缩略图在 ie 中不显示第二轮