我按照老师的思路写的,内容也没区别,但是在最后obj.style.left的时候一直报错显示obj.style未定义是为什么,我代码在下面,就大神解惑
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{padding:0;margin:0;}
ul,li{
list-style:none;
}
ul li{
width:200px;
height: 100px;
background: yellow;
margin-bottom:20px;
}
</style>
<script>
window.onload=function(){
var oLi=document.getElementsByTagName('li');
for(var i=0,l=oLi.length;i<l;i++){
this.onmouseover=function(){
move(this,400);
}
this.onmouseout=function(){
move(this,200);
}
}}
var timer=null;
function move(obj,iTarget){
clearInterval(timer);
timer=setInterval(function(){
var speed=(iTarget-obj.offsetWidth)/2>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetWidth==iTarget){
clearInterval(timer);
}else{
obj.style.width=obj.offsetWidth+speed+'px';//这里的obj.style一直显示未定义
}
},50);
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>