为什么我的代码就是实现不了多物体运动?请大神看看。

来源:4-1 JS多物体动画

雪神仙

2016-08-09 08:12

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</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");
    for(var i=0;i<Ali.length;i++){
       Ali[i].onmouseover=function(){
        startMove(this,400);

       }

       Ali[i].onmouseout=function(){
           startMove(this,200);
       }
    }


    }

var timer=null;
function startMove(obj,iTarget){
clearInterval(timer);
timer=setInterval(function(){
    var speed=(iTarget-obj.offsetwidth)/8;
    speed=speed>0?Math.ceil(speed):Math.floor(speed);
    if(obj.offsetwidth=iTarget){
     clearInterval(timer);

    }
    else{
        obj.style.width=obj.offsetwidth+speed+'px';
    }


},30)


}

</script>

</head>




<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>



    </ul>
</body>
</html>

写回答 关注

2回答

  • 宝慕林2917688
    2016-08-26 10:19:07

       var speed=(iTarget-obj.offsetwidth)/8;

    为什么要除以8的


  • 雪神仙
    2016-08-09 08:20:17

    不好意思重复了,已经解决


JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113932 学习 · 1443 问题

查看课程

相似问题