2016-11-20 36 views
0

我想創造一個像水平單臂強盜遊戲的東西。水平單臂強盜動畫

我有12個元素彼此相鄰放置,當我按下按鈕時,我希望它們水平移動並停在隨機位置。

你能告訴我怎麼做到這一點嗎?

回答

1

這是不是很有效,但你可以做這樣的事情:

var move = setInterval(animation, 50); //50 is only an example value 

function animation(){ 
    $('yourDOMelement').css('transform', 'translateY(1px)'); //maybe -1 depends on direction... 
    if(Math.random()*100==2){ //adjust the 100 to your needs 
    clearInterval(move); 
    } 
} 

注意,代碼是未經測試,我希望你至少會得到的想法我會怎麼做,然後通過它你的問題。