ex_咖喱棒
2016-04-05 11:38
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> ul li{ list-style:none;} li{ height:50px; width:200px; background-color:#666; margin-bottom:10px;} </style> <script> window.onload=function(){ var speed; var li1=document.getElementById("li1"); var li2=document.getElementById("li2"); var li3=document.getElementById("li3"); li1.onmouseover=function(){move(this,"width",400)} li1.onmouseout=function(){move(this,"widht",200)} li2.onmouseover=function(){move(this,"height",400)} li2.onmouseout=function(){move(this,"height",200)} li3.onmouseover=function(){move(this,"height",400)} li3.onmouseout=function(){move(this,"height",200)} } function getStyle(obj,attr){ if(obj.currentStyle){return obj.currentStyle[attr]} else{return getComputedStyle(obj,false)[attr]; } } function move(obj,attr,target){ clearInterval(obj.time); obj.time=setInterval(function() { var num=parseInt(getStyle(obj,attr)); speed=(target-num)/10; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(num==target){clearInterval(obj.time)} else{ obj.style.attr=num+speed+"px";} },30) } </script> </head> <body> <div> <ul> <li id="li1"></li> <li id="li2"></li> <li id="li3"></li> </ul> </div> </body> </html>
obj.style[attr]是去取obj的样式中的某个属性,attr是变量。可以是height,可以是width。
obj.style.attr是去取obj的样式中的attr属性,而实际上这个属性是不存在的,所以不行。
还有 li1.onmouseout=function(){move(this,"widht",200)} 中的宽写错了
obj.style.attr=num+speed+"px";
改成
obj.style[attr]=num+speed+"px";
JS动画效果
113925 学习 · 1443 问题
相似问题