jQuery的链式动画写法中加入setTimeout

来源:互联网 发布:mac第三方压缩软件 编辑:程序博客网 时间:2024/05/29 19:16

今日闲逛,看到一个简单的封装,用于在两次animate之间,加入一定的时间间隔的等待。
原文地址:http://docs.jquery.com/Cookbook/wait
你还可以在里边看到demo

  1. $.fn.wait = function(timetype) {
  2.     time = time || 1000;
  3.     type = type || "fx";
  4.     return this.queue(typefunction() {
  5.         var self = this;
  6.         setTimeout(function() {
  7.             $(self).dequeue();
  8.         }time);
  9.     });
  10. };

具体用法是,.wait( [time], [type] )
第一个参数是时间,默认是1000毫秒。
第二个参数是类型,默认是fx即动画效果

  1. function runIt() {
  2.     $("div").wait()
  3.         .animate({left:'+=200'},2000)
  4.         .wait()
  5.         .animate({left:'-=200'},1500,runIt);
  6.     }
  7. runIt();