JS簡單浮動碰撞效果

<html>
<head>
<title>小太陽碰撞效果</title>
<script language="javascript">
<!--
		directX=1;  //X軸方向
		directY=1;  //Y軸方向
		sunX=0;
		sunY=0;
		
	function sunMov(){
		//定兩個方向
		sunX+=directX*2;
		sunY+=directY*2;

		//修改div的left top
		sundiv.style.top=sunY+"px";
		sundiv.style.left=sunX+"px";

		//判斷什麼時候,轉變方向
		//x方向(offestWidth可以返回,當前這個對象的實際寬度)
		if(sunX+sundiv.offsetWidth>=document.body.clientWidth || sunX<=0){
			directX=-directX;
		}
		if(sunY+sundiv.offsetHeight>=document.body.clientHeight || sunY<=0){
			directY=-directY;
		}
	}

	setInterval("sunMov()",10);

//-->
</script>
</head>
<body style="background-image:URL('a.jpg');background-size:cover; background-repeat:no-repeat">
<div id="sundiv" style="position:absolute">
<img src="sun.png"/>
</div>
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章