wanoneQ
2016-02-19 20:15
<!DOCTYPE html> <html> <head> <title>20160218</title> <style type="text/css"> *{padding: 0;margin:0;} ul li{ width:300px; height: 100px; background-color: red; margin-bottom:20px; border:5px solid green; } </style> <script type="text/javascript"> window.onload=function(){ var opp=document.getElementsByTagName("li"); for (var i=0;i<opp.length;i++){ opp[i].timer=null;//为什么var opp[i].timer;不行? opp[i].onmouseover=function(){ startMove(this,500); } opp[i].onmouseout=function(){ startMove(this,300); } } } function startMove(obj,iTarget){ clearInterval(obj.timer); obj.timer=setInterval(function(){ var icur = parseInt(getStyle(obj,"width")); var speed=(iTarget-icur)/8; speed = speed>0?Math.ceil(speed):Math.floor(speed); if(icur==iTarget){ clearInterval(obj.timer); } else{ obj.style[attr]=icur+speed+'px'; } },30) } function getStyle(obj,attr){ if (obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj,false)[attr]; } } </script> </head> <body> <ul> <li></li> <li></li> <li></li> </ul> </body> </html>
第44行的attr没有定义,应该把attr换成'width',因为attr是在getStyle()里面的局部变量,离开了getStyle()就会销毁
JS动画效果
113925 学习 · 1443 问题
相似问题