javascript實(shí)現(xiàn)小球的自由移動(dòng)代碼
用javascript實(shí)現(xiàn)小球的自由移動(dòng)是不是很神奇啊?下面小編交給大家實(shí)現(xiàn)的方法,歡迎閱讀以下代碼,更多詳情請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
代碼如下:
<!DOCTYPE HTML PUBLIC "-/pic/pic/pic/p>
<html>
<head>
<title>javascript實(shí)現(xiàn)小球的自由移動(dòng)-www.javaweb.cc</title>
<link rel="stylesheet" type="text/css" href="">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script type="text/javascript">
/pic/p>
/pic/p>
ballX=0;
ballY=0;
/pic/p>
directX=1;
directY=1;
/pic/p>
function ballMove(){
/pic/p>
ballX+=2*directX;
ballY+=2*directY;
/pic/p>
p2.style.top=ballY+'px';
p2.style.left=ballX+'px';
/pic/p>
/pic/p>
/pic/p>
/pic/p>
if(ballX+p2.offsetWidth>=p1.offsetWidth || ballX<=0){
directX=-directX;
}
if(ballY+p2.offsetHeight>=p1.offsetHeight || ballY<=0){
directY=-directY;
}
}
/pic/p>
var i=setInterval("ballMove()",10);
</script>
</head>
<body>
<p id="p1" style="width:400px;height:300px;border:1px solid silver;POSITION: absolute; TOP: 100px">
<p id="p2" style="position:absolute;left:0px;top:0px;">
<img src="ball.png">
</p>
</p>
</body>
</html>
【javascript實(shí)現(xiàn)小球的自由移動(dòng)代碼】相關(guān)文章:
JavaScript簡(jiǎn)單實(shí)現(xiàn)放大鏡效果代碼02-21
Javascript實(shí)現(xiàn)全選并賦值給文本框代碼實(shí)例11-26
php動(dòng)態(tài)生成JavaScript代碼03-12
JavaScript重置表單的實(shí)現(xiàn)12-15
PHP滾動(dòng)日志的代碼實(shí)現(xiàn)09-30
Java 隊(duì)列實(shí)現(xiàn)原理及簡(jiǎn)單實(shí)現(xiàn)代碼03-18
php實(shí)現(xiàn)無(wú)限級(jí)分類(lèi)實(shí)現(xiàn)代碼10-05