请问大佬哪儿错了啊???

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

qq_慕仙5504668

2019-08-23 17:06

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>多物体运动</title>

    <style type="text/css">

        *{
            padding:0;
            margin:0;

        }

        ul,li{
            list-style: none;
        }

        ul li{
            width:200px;
            height:100px;
            background-color: chartreuse;
            margin-bottom:20px;
        }

    </style>
</head>
<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>

<script>
    window.onload = function(){
        var aLi=document.getElementsByName('li');
        for(i=0;i<aLi.length;i++){
            aLi[i].onmouseover= function(){
                    starMove(this,400);
            }
            aLi[i].onmouseout= function(){
                starMove(this,200);
            }
        }
        var timer = null;
        function starMove(obj,target){
            clearInterval(obj.timer);
            timer = setInterval(function(){
                var speed=(target-obj.offsetWidth)/8;
                speed=speed>0?Math.ceil(speed):Math.floor(speed);
                if(obj.offsetWidth== target){
                    clearInterval(timer);
                }else{
                    obj.style.width=obj.offsetWidth+speed+'px';
                }
            },30)
        }
    }

</script>

</body>
</html>

?

写回答 关注

2回答

  • 爱前端的小鑫
    2020-05-05 21:35:28

    var aLi = document.getElementsByTagName('li');

    这句打错了

  • qq_流星泪_5
    2019-08-23 20:30:08

    for循环里未定义aLi[i].timer=null,在starMove方法中就直接使用


JS动画效果

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

113925 学习 · 1443 问题

查看课程

相似问题