<!doctype html><!--申明當前文檔的類型--><html> <head> <!--中文編碼集:GBK ?GB2312 ? 國際編碼:utf-8--> <meta http-equiv="content-type" content="text/html" charset="utf-8" /> <!--申明當前頁面的三要素--> <meta name="Keywords" content="關鍵字,關鍵詞" /> <meta name="Description" content="描述和簡介" /> <title>1612屆零基礎班</title> <style type="text/css"> #box1{width:200px;height:200px;background:#ccc;position:absolute;left:0px;top:0px;} </style> </head>?<body> <div id="box1"></div>?</body> <script type="text/javascript"> //div的位置 var sx=0; var sy=0; var speed=10;//速度 var xpe=speed; var ype=speed; var box=document.getElementById("box1"); var maxwidth=window.innerWidth - box.offsetWidth;//瀏覽器寬度-盒子寬度 var maxheight=window.innerHeight - box.offsetHeight; function draw(){ sx++; sy++; if(sx>=maxwidth){ xpe=-speed; sx=maxwidth; } if(sx<=0){ xpe=-speed; x=0; } if(sy>=maxheight){ ype=-speed; sy=maxheight; } if(sy <=0){ ype =speed; sy= maxheight; } box.style.left=sx+"px"; box.style.top=sy+"px"; }; setInterval(function(){ draw(); },1); </script></html>
這個碰撞運動,變成水平怎么解決
慕粉3884565
2017-03-09 22:26:23