<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>JS动画2</title>
<style type="text/css">
ul li {
list-style: none;
width: 200px;
height: 100px;
background: yellow;
margin-bottom: 20px;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
<script type="text/javascript">
window.onload=function() {
var lis=document.getElementsByTagName("li");
for(var i=0;i<lis.length;i++){
lis[i]=onmouseover=function(){
Move(this,400);
}
lis[i]=onmouseover=function(){
Move(this,200);
}
}
}
var timer=null;
function Move(tar,aim) {
clearInterval(timer);
timer=setInterval(function(){
var sp=(aim-tar.offsetWidth)/8;
sp=sp>0?Math.ceil(sp):Math.floor(sp);
if(tar.offsetWidth==aim){
clearInterval(timer);
}else{
tar.offsetWidth=tar.style.width+sp+"px";
}
},10);
}
</script>
</html>
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>JS动画2</title> <style type="text/css"> ul li { list-style: none; width: 200px; height: 100px; background: yellow; margin-bottom: 20px; } </style> </head> <body> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </body> <script type="text/javascript"> window.onload=function() { var lis=document.getElementsByTagName("li"); for(var i=0;i<lis.length;i++){ lis[i].timer=null;//给每一个li设一个私有的定时器 lis[i].onmouseover=function(){//不是等于符号是点. Move(this,400); } lis[i].onmouseout=function(){//不是等于符号是点. Move(this,200); } } } //var timer=null;不能共用同一个定时去 function Move(tar,aim) { clearInterval(tar.timer); tar.timer=setInterval(function(){ var sp=(aim-tar.offsetWidth)/8; sp=sp>0?Math.ceil(sp):Math.floor(sp); if(tar.offsetWidth==aim){ clearInterval(tar.timer); }else{ tar.style.width=tar.offsetWidth+sp+"px";//这里你写反了 } },10); } </script> </html>