慕标2475242
2016-08-11 21:16
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>多物体动画2</title>
<style type="text/css">
body{margin:0;padding:0;}
ul{list-style:none; }
ul li{width:200px;height:150px;background:#0f0;margin-bottom:20px;border:1px solid #0ff;}
</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,600);
}
aLi[i].onmouseout=function(){
startMove(this,300);
}
}
}
//getStyle
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 icur=parseInt(getStyle(obj,'width'));
var speed=(iTarget-icur)/6;
speed=speed>0?Math.ceil(speed):Math.ceil(speed);
if(icur==iTarget){
clearInterval(obj.timer);
}
else{
obj.style["width"]=icur+speed+'px';
}
},30)
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
startMove(this,300);你的css原来样式为200px啊
JS动画效果
113925 学习 · 1443 问题
相似问题
回答 1
回答 1