【问题标题】:jQuery animation without queuing无需排队的 jQuery 动画
【发布时间】:2013-02-09 06:10:22
【问题描述】:

我想弄清楚 .stop() 和 clearQueue() 是如何工作的。 这是我在 jsfiddle 中的示例代码:http://jsfiddle.net/PsTQv/1/ 如果将鼠标悬停在几个块上,您将看到动画正在排队。 为了解决这个问题,我尝试使用 stop() 和 clearQueue.Simple add stop after hide() 或 show() 或两者都不起作用。如下行为:

1. .stop().hide() : text disappears at last;
2. .stop.show(): text is alway there, won't be hidden any more
3. add both: Same as only add to show()
4. add .clearQueue().stop() in the beginning: text disappears at last, like .stop().hide()

我想了解clearQueue和stop之间的区别来解释上面的行为。另外我想弄清楚在这个例子中如何实现不排队的动画,即悬停在块上,文本显示在幻灯片效果。

谢谢

【问题讨论】:

  • 试试 stop(true, true) 或者阅读文档怎么样 :)

标签: javascript jquery jquery-animate


【解决方案1】:

需要在幻灯片动画完成时执行的回调函数中clear the animation queue

$('.block').hover(function(){
    $('section').hide('fast');
    //$('section').stop().show('slide',{direction:'left'},1000);
    $('section').show('slide',{direction:'left'},1000,function(){$(this).clearQueue()});
},function(){});


jsFiddle

【讨论】:

    【解决方案2】:
    var inAnimation = new Array();
    
    $("div").hover(function(){
        if (!inAnimation[$("div").index(this)] ) {
            $(this).animate({ width: "200px" });
        }
    }, function() {
        inAnimation[$("div").index(this)] = true;
        $(this).animate({ width: "100px" }, "normal", "linear", function() {
            inAnimation[$("div").index(this)] = false; 
        })
    });
    

    【讨论】:

      【解决方案3】:
      <!doctype html>
      <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>clearQueue demo</title>
        <style>
        div {
          margin: 3px;
          width: 40px;
          height: 40px;
          position: absolute;
          left: 0px;
          top: 30px;
          background: green;
          display: none;
        }
        div.newcolor {
          background: blue;
        }
        </style>
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      </head>
      <body>
      
      <button id="start">Start</button>
      <button id="stop">Stop</button>
      <div></div>
      
      <script>
      $( "#start" ).click(function() {
        var myDiv = $( "div" );
        myDiv.show( "slow" );
        myDiv.animate({
          left:"+=200"
        }, 5000 );
      
        myDiv.queue(function() {
          var that = $( this );
          that.addClass( "newcolor" );
          that.dequeue();
        });
      
        myDiv.animate({
          left:"-=200"
        }, 1500 );
        myDiv.queue(function() {
          var that = $( this );
          that.removeClass( "newcolor" );
          that.dequeue();
        });
        myDiv.slideUp();
      });
      
      $( "#stop" ).click(function() {
        var myDiv = $( "div" );
        myDiv.clearQueue();
        myDiv.stop();
      });
      </script>
      
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2010-10-14
        • 1970-01-01
        • 1970-01-01
        • 2012-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-24
        • 2011-09-06
        相关资源
        最近更新 更多