這是我的代碼裏面:randomaly移動圖像的DIV
<style type="text/css">
#box
{
width: 400px;
height: 400px;
border: 1px solid;
cursor: none;
}
#Parachute
{
width: 25px;
height: 25px;
cursor: none;
position: absolute;
}
</style>
<script type="text/javascript">
$("#StartGame").click(function() {
animateDiv();
});
function makeNewPosition() {
// Get viewport dimensions (remove the dimension of the div)
var h = $("#box").height() - 25;
var w = $("#box").width() - 25;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh, nw];
}
function animateDiv() {
var newq = makeNewPosition();
$('#Parachute').animate({ top: newq[0], left: newq[1] }, function() {
animateDiv();
});
};
<script>
<centre>
<div id="box">
<img id="Parachute" src="Parachute.gif" width="25px" height="25px" />
</div>
<centre>
<input type="button" id="StartGame" value="Start Game" />
圖像不動我到底做錯了什麼?
你能撥弄它嗎? – Gautam3164
如果這是你的腳本和html的實際順序,你試圖在'#StartGame'附加一個處理程序,然後它就會存在。 –