【问题标题】:Stopping and restarting an animation on hover in jQuery在jQuery中停止和重新启动悬停动画
【发布时间】:2011-03-16 14:21:58
【问题描述】:

我正在尝试使用 jQuery 创建一个动画按钮,该按钮应在悬停时停止,然后在未悬停时重新启动。

对于动画,我使用如下所示的循环:

$(document).ready(function test() {
        $('#box').animate({opacity:0.5}, {duration:750})
        .animate({opacity:1}, {duration:1500})
        .animate({opacity:0.5}, {duration:750, complete: test})
        ;
    });

我尝试使用 .stop() 添加悬停,但是我不知道如何重新启动动画。你有什么建议吗?

另外,我在这里有一个小例子:http://jsfiddle.net/fTpZZ/

【问题讨论】:

    标签: jquery animation loops hover restart


    【解决方案1】:

    hover() 事件处理程序可以将 handlerIn 和 handlerOut 方法分别作为第一个和第二个参数处理。

    悬停(handlerIn(eventObject), handlerOut(eventObject))

    创建 2 个单独的函数——一个用于“on”状态,一个用于“off”

    .hover() - jQuery API

    【讨论】:

      【解决方案2】:

      工作解决方案:(http://jsfiddle.net/fTpZZ/50/)

      $(document).ready(function()
          {
              $('#box').hover(function() {                   
                  $('#box').stop();
                  $('#box').animate({opacity:0},1);
              });
      
              function foo()
              {
                  $('#box').animate({opacity:0.5}, 750, function(){
                      $('#box').animate({opacity:1}, 1500, function(){
                          $('#box').animate({opacity:0.5}, 750, foo());    
                      });
                  });             
              }
      
              foo();          
          });
      

      【讨论】:

      • 你能让动画重新开始吗?它停止(在我进出几次之后),但不会重新开始。 FF 3.6.x 和 FF 4.xb
      • 我在最新的 Chrome、Firefox 3.6 和 Opera 11 中进行了测试,当我离开该区域时动画没有重新启动。除此之外效果很好,谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多