Let’s first take a look at the previous motion code to see whether it supports multi-object motion and what kind of problems may arise. Copy code The code is as follows: <br> div {<br> width: 100px;<br> Height: 50px;<br> background: red;<br> margin: 10px;<br> }<br> <br> </div> <p></p> <div class="codetitle"><span><a style="CURSOR: pointer" data="48754" class="copybut" id="copybut48754" onclick="doCopy('code48754')">Copy code<u></u></a> The code is as follows:</span></div> <div class="codebody" id="code48754"> <body><br> </body><br> <br><br> <br>The following is the Javascript code: <br> </div> <p></p> <p>Copy code</p> <div class="codetitle"><span><a style="CURSOR: pointer" data="26727" class="copybut" id="copybut26727" onclick="doCopy('code26727')"> The code is as follows:<u><div class="codebody" id="code26727"> <br> <script type="text/javascript"><br> window.onload = function() {<br> var aDiv = document.getElementsByTagName('div');<br> for (var i = 0; i < aDiv.length; i ) {<br /> aDiv[i].onmouseover = function() {<br /> startMove(this, 400);<br /> };<br /> aDiv[i].onmouseout = function() {<br /> startMove(this, 100);<br /> };<br /> }<br /> }<br /> var timer = null;<br /> function startMove(obj, iTarget) {<br /> clearInterval(timer);<br /> timer = setInterval(function() {<br /> var speed = (iTarget - obj.offsetWidth) / 6;<br /> speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);<br> if (obj.offsetWidth == iTarget) {<br> clearInterval(timer);<br> } else {<br> obj.style.width = obj.offsetWidth speed 'px';<br> }<br> }, 30);<br> }<br> </script><br> </div> </u><p>此时当鼠标移入到第一个div 时,他是正常运行的。但是如果现在又移动到第二个或者第三个div时候就会出现bug。</p> <p>image 这个是什么原因呢? 看图可以看出并没有运动完成。实际上是这样的,</p> <p>整个程序就一个定时器, 比如第一个div开始动了,第二个div 鼠标移入了 前一个定时器就被干掉了,那么自然就卡在那里了。</p> <p>所以最大的问题就是整个程序就只有一个定时器。那么怎么解决这个问题呢?</p> <p><strong>解决方案:</strong></p> <p>其实很简单,把定时器作为一个物体的属性加上,那么每个物体都有一个定时器在,当关闭定时器的时候是关闭物体上的定时器,开也是物体上的定时器</p> <p>那么他们之间就可以完全互不干扰的运行。</p> <p>看下修改后的Javascript代码:</p> <p></p> <div class="codetitle"> <span><a style="CURSOR: pointer" data="66248" class="copybut" id="copybut66248" onclick="doCopy('code66248')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code66248"> <br> <script type="text/javascript"><br> window.onload = function() {<br> var aDiv = document.getElementsByTagName('div');<br> for (var i = 0; i < aDiv.length; i ) {<br /> aDiv[i].timer=null; // Store the timer as an attribute of an object <br /> aDiv[i].onmouseover = function() {<br /> StartMove(this, 400);<br /> };<br /> aDiv[i].onmouseout = function() {<br /> StartMove(this, 100);<br /> };<br /> }<br /> }<br /> function startMove(obj, iTarget) {<br /> Clearinterval (obj.timer); <br /> obj.timer = setInterval(function() {<br /> var speed = (iTarget - obj.offsetWidth) / 6;<br /> Speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);<br> If (obj.offsetWidth == iTarget) {<br> } else {<br> Obj.style.width = obj.offsetWidth Speed 'PX'; <br> }<br> }, 30);<br> }<br> <br><br> </div>In this way, the program will have no problems and can support the movement of multiple objects. </a></span></div>