2010-03-28 36 views
14

我知道如何上下移動jQuery中的元素。如何在jQuery中移動對角線運動中的元素?

$("#div").animate({"left": "+=100"}, 1000); //move 100px to the right 

但我不知道在對角線移動中移動。我正在做棋盤,我不知道如何移動主教效果。請看看以下網址http://chess.diem-project.org/

我不喜歡這樣......但它不是一種合適的方式。

for(var i = 0;i<50;i++){ // move down and move right 1 pixel at a time to get effect 
$("#div").animate({"left": "+="+x}, 1); 
$("#div").animate({"top": "+="+x}, 1); 
} 

有什麼想法嗎?真的很感謝你的幫助!

回答

26

做這樣的:

$("#div").animate({left: '+=50', top: '+=50'}, 1000); 

你想一個動畫讓你有... for循環隊列100級的動畫在你的情況,你只需要一個:) See a demo here

+0

究竟是什麼我需要。令人驚歎的答案!非常感謝你尼克! – Devyn 2010-03-28 10:32:38

+0

非常好!謝謝! – Combine 2017-11-25 13:59:31