js实现多物体运动
Posted YH丶浩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js实现多物体运动相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div{ width: 200px; height: 100px; margin: 20px; float: left; background: red; border: blue 1px solid; } </style> <script> window.onload = function () { var oDiv = document.getElementById(‘div1‘); var oDiv2 = document.getElementById(‘div2‘) oDiv.onmouseover = function(){ startMove(this,"height",300); }; oDiv.onmouseout = function(){ startMove(this,"height",100); }; oDiv2.onmouseover = function(){ startMove(this,"width",400); }; oDiv2.onmouseout = function(){ startMove(this,"width",200); } } function getStyle(obj,name){ if (obj.currentStyle) { return obj.currentStyle[name]; } else { return getComputedStyle(obj,false)[name] } } //var alpha = 30;所有的东西都不能公用 function startMove (obj,attr,iTarget) { clearInterval(obj.time); obj.time = setInterval(function(){ var cur = parseInt(getStyle(obj,attr)); var speed = (iTarget-cur)/6; speed = speed>0?Math.ceil(speed):Math.floor(speed); if (cur == iTarget) { clearInterval(obj.time); } else { obj.style[attr] = cur+speed+‘px‘; } },30) } </script> </head> <body> <div id="div1">变高</div> <div id="div2">变宽</div> </body> </html>
以上是关于js实现多物体运动的主要内容,如果未能解决你的问题,请参考以下文章