qq_慕仙5504668
2019-08-23 17:06
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>多物体运动</title> <style type="text/css"> *{ padding:0; margin:0; } ul,li{ list-style: none; } ul li{ width:200px; height:100px; background-color: chartreuse; margin-bottom:20px; } </style> </head> <body> <ul> <li></li> <li></li> <li></li> <li></li> </ul> <script> window.onload = function(){ var aLi=document.getElementsByName('li'); for(i=0;i<aLi.length;i++){ aLi[i].onmouseover= function(){ starMove(this,400); } aLi[i].onmouseout= function(){ starMove(this,200); } } var timer = null; function starMove(obj,target){ clearInterval(obj.timer); timer = setInterval(function(){ var speed=(target-obj.offsetWidth)/8; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(obj.offsetWidth== target){ clearInterval(timer); }else{ obj.style.width=obj.offsetWidth+speed+'px'; } },30) } } </script> </body> </html>
?
var aLi = document.getElementsByTagName('li');
这句打错了
for循环里未定义aLi[i].timer=null,在starMove方法中就直接使用
JS动画效果
113925 学习 · 1443 问题
相似问题