慕粉3207663
2016-06-10 09:38
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>多物体运动</title>
<style type="text/css">
*{
margin:0;
padding:0;
}
ul,li{
list-style:none;
}
ul li{
height:100px;
width:200px;
background:yellow;
margin-bottom:20px;
border: 1px solid;
}
</style>
<script type="text/javascript">
window.onload=function(){
var aLi=document.getElementsByTagName('li');
for(var i=0;i<aLi.length;i++){
aLi[i].timer=null;
aLi[i].onmouseover=function(){
startMove(this,400);
}
aLi[i].onmouseout=function(){
startMove(this,200);
}
}
}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
//var timer=null;
function startMove(obj,iTarget){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var kuan=parseInt(getStyle(obj,'width'));
var speed=(iTarget-kuan)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if (kuan==iTarget) {
clearInterval(obj.timer);
}else{
obj.style.width=kuan+speed+'px';
}
},
30)
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
你代码可以执行,我在谷歌、火狐、IE都测试了,你看看你的浏览器是不是报错什么代码
JS动画效果
113925 学习 · 1443 问题
相似问题