<!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>無(wú)標(biāo)題文檔</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>
2016-04-05
obj.style[attr]是去取obj的樣式中的某個(gè)屬性,attr是變量??梢允莌eight,可以是width。
obj.style.attr是去取obj的樣式中的attr屬性,而實(shí)際上這個(gè)屬性是不存在的,所以不行。
還有?li1.onmouseout=function(){move(this,"widht",200)} 中的寬寫錯(cuò)了
2016-04-05
obj.style.attr=num+speed+"px";
改成
obj.style[attr]=num+speed+"px";