你的問題讓我感興趣讓我一起扔一個簡單的例子上的jsfiddle:http://jsfiddle.net/jJ8vJ/
的基本思想是使用jQuery的.animate()
功能的回調函數來遍歷DOM元素的列表動畫。
//first I'm setting up the variables I'll need
var $items = [$('#one'), $('#two'), $('#three'), $('#four')],//stores the DOM elements to be animated in order
item_index = 0,//stores the current index in the animation queue
animations = {
'0px0px' : { left : '100px', top : '0px' },
'100px0px' : { left : '100px', top : '100px' },
'0px100px' : { left : '0px' , top : '0px' },
'100px100px' : { left : '0px' , top : '100px' }
};//this stores the conversion between where the element is and where it should animate to
//setup a function that can call itself inside the animation callback to iterate through the DOM elements to be animated
function run_animation($element) {
//check to make sure there are more DOM elements to animate, if none are found then the process is done
if (item_index in $items) {
//get this element's top and left CSS properties and put them together so we can convert that to the next coordinates it needs
var css = $items[item_index].css('left') + '' + $items[item_index].css('top');
//now animate this element by converting the current top/left CSS properties to its next coordinates
$items[item_index].animate({
left : animations[css].left,
top : animations[css].top
}, 1500, function() {
//here we run this same function for the next index
item_index++;
run_animation($items[item_index]);
});
}
}
//run the animation function for the first time
run_animation($items[item_index]);
爲什麼在'.animate()'函數提供回調時設置超時? – Jasper
@Jasper:因爲你可以讓動畫部分重疊,而不是完全按順序運行。 –