看,这是使用jQuery完成的 http://jsfiddle.net/qCx69/
$('img').animate({left: '100px'}, 2000, function() {
$(this).delay(1000).animate({left: '300px'}, 2000);
});
与之交互
<img src='http://boydstire.com/img/car_img.jpg' id='car'
style='position:absolute;left:0px;top:100px;'>
或者没有它(这不是一个优化的解决方案)
http://jsfiddle.net/8bZTA/1/
var timer = setInterval(function(){
update_car(100,1);
}, 50);
function update_car(x,path)
{
var car = document.getElementById('car'),
pos = parseInt(car.style.left) + 1;
car.style.left = pos + 'px';
if (pos > x)
{
clearInterval(timer);
if (path!=2)
{
setTimeout(function(){
timer = setInterval(function(){update_car(200,2);}, 50);}
, 5000);
}
}
}
更新:
你甚至可以创建一组停止和动作(它可以根据速度变化进行更新)
http://jsfiddle.net/hFH4U/5/
var timer = setInterval(function(){update_car();}, 50);
var path = {'path1': 100, 'pause1': 2000, 'path2': 200,
'pause2': 2000, 'path3': 220},
cur_step = 0, steps = [], speed = 1;
for (var i in path) steps.push(i);
function update_car()
{
var car = document.getElementById('car'),
pos = parseInt(car.style.left);
if (/^path/.test(steps[cur_step]))
{
if (pos > path[steps[cur_step]])
cur_step++;
}
if (/^pause/.test(steps[cur_step]))
{
clearTimeout(timer);
setTimeout(function(){
cur_step++;
timer = setInterval(function(){ update_car(); }, 50);
}, path[steps[cur_step]]);
}
if (cur_step >= steps.length)
clearInterval(timer);
car.style.left = (pos + speed) + 'px';
}