2012-04-17 57 views
2

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

下面是我在哪里: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'); 

});

第一次你徘徊时,它很棒,工作正常。但是,如果您尝试再次悬停同一个,则只会经历一次淡出而不是再次循环。

任何想法?

请忽略一些粗糙的代码,在这里使用一个非常古老的主题,试图清理它!

+0

请在这里发布代码。这是关于帮助你现在和其他人从现在起2年......在你的小提琴走了之后。 – 2012-04-17 21:09:54

回答

2

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

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

例波纹管:

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

});​ 

你需要做的一切都是在使用$(this).cycle('pause')上并$(this).cycle('resume')

任何事情都让我知道。

+0

啊,是的,我太傻了,谢谢一堆! – graygilmore 2012-04-17 22:10:29

+0

我唯一的问题是,如果我在一个页面上有多个幻灯片,我不确定这里是否会出现冲突? – graygilmore 2012-04-17 22:14:40

+0

你好!:)如果你在'$(document).ready()'上配置它们都不会有任何问题,现在问题就是“资源”的用法,循环会给DOM添加一些元素。唯一的**“解决方案”**将杀死周期并重置它,但是那样你就不会拥有你正在寻找的动画。 您期望使用多少个周期和图像? – 2012-04-17 22:18:49