2014-01-17 43 views
1

我遇到了問題。我想要一個圖像出現在隨機高度。 這是編碼:jQuery的隨機高度

$("#duck").css({ 
    #duck{ top: Math.floor((Math.random()*100)+1);%; } 
}); 

有人能告訴我什麼,我需要改變嗎?

+1

這不是如何'的.css()'作品。重新閱讀[文檔](http://api.jquery.com/css/)。 –

+0

我對此很新,我正在做這個項目以獲得更多的經驗。我應該如何使用它? – Thomas

+0

'$(「#duck」)。height(Math.floor((Math.random()* 100)+1))' – adeneo

回答

0

你可能想這樣做:

$("#duck").css("top", Math.floor((Math.random()*100)+1)); 

jQuery CSS Documentation

+3

儘管這不是圖像高度,但這是圖像偏移量。 – Sampson

+0

「出現在隨機高度」,我相信@Thomas希望圖像出現在隨機偏移(頂部高度) –

+0

有效點。 OP在這方面不是很清楚。 – Sampson

0

你可以試試:

function getRandom(min, max) { 
return min + Math.floor(Math.random() * (max - min + 1)); 
} 

setInterval(function(){ 
var randomtop=(getRandom(0,($(window).height()-$("#duck").height())+"px"); 
$("#duck").css("top",randomtop); 
},5000)