若忆浅搁
2016-03-21 13:33
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JS多物体运动动画</title>
<style type="text/css">
ul,li{
list-style:none;}
ul li{
width:200px;
height:100px;
background:yellow;
margin-bottom:20px;}
</style>
<script type="text/avascript">
window.onload=function(){
var aLi=document.getElementsByTagName("li");//aLi为数组
for(var i=0;i<aLi.length;i++)
{
aLi.time=null;
aLi[i].onmouseover=function(){
startMove(this,400);
}
aLi[i].onmouseout=function(){
startMove(this,200);
}
}
}
aLi[i].timer=null;
function startMove(obj,terget){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var speed=(terget-obj.offsetWidth)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetWidth==terget){clearInterval(obj.timer);}
else{obj.style.width=obj.offsetWidth+speed+"px";
},30)
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
你好,其实只是一些小问题,
<script type="text/avascript">是javascript,这个应该影响不大,
aLi.time=null;aLi[i].timer=null;//应该是点的哪一个
function startMove(obj,terget){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var speed=(terget-obj.offsetWidth)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetWidth==terget){clearInterval(obj.timer);}
else{obj.style.width=obj.offsetWidth+speed+"px";
},30) //掉了一个'}',这是主要的
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JS多物体运动动画</title>
<style type="text/css">
ul,li{
list-style:none;}
ul li{
width:200px;
height:100px;
background:yellow;
margin-bottom:20px;}
</style>
<script type="text/javascript">
window.onload=function(){
var aLi=document.getElementsByTagName("li");//aLi为数组
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 startMove(obj,terget){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var speed=(terget-obj.offsetWidth)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(obj.offsetWidth==terget){clearInterval(obj.timer);}
else{obj.style.width=obj.offsetWidth+speed+"px";
}
},30)
}
</script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
JS动画效果
113925 学习 · 1443 问题
相似问题