看,這是與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(){ //pause
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]))
{
// motion part
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) // end animation
clearInterval(timer);
car.style.left = (pos + speed) + 'px';
}
你使用jQuery嗎? – Cheery 2012-02-02 05:53:57
'汽車循環,如果例如實現在汽車左側添加1px:值'你可以解釋這是什麼意思? – Ved 2012-02-02 05:54:48
如果您想要一個提示,請查看使用while循環。這將允許您運行動畫,直到汽車駛過指定的距離。 – 2012-02-02 05:55:20