今日闲逛,看到一个简单的封装,用于在两次animate之间,加入一定的时间间隔的等待。
原文地址:http://docs.jquery.com/Cookbook/wait
你还可以在里边看到demo
- $.fn.wait = function(time, type) {
- time = time || 1000;
- type = type || "fx";
- return this.queue(type, function() {
- var self = this;
- setTimeout(function() {
- $(self).dequeue();
- }, time);
- });
- };
具体用法是,.wait( [time], [type] )
第一个参数是时间,默认是1000毫秒。
第二个参数是类型,默认是fx即动画效果
- function runIt() {
- $("div").wait()
- .animate({left:'+=200'},2000)
- .wait()
- .animate({left:'-=200'},1500,runIt);
- }
- runIt();