我什么我打开直接就收回去了,是代码有问题吗????求大佬看下

来源:2-1 JS速度动画

慕丝7111264

2020-04-03 11:50

<style type="text/css">

            *{

                margin: 0;

                padding: 0;

            }

            #box1{

                width: 200px;

                height: 200px;

                background: red;

                position: relative;

                left: -200px;

                top: 0;

            }

            #span{

                width: 20px;

                height: 50px;

                background-color: #bfa;

                position: absolute;

                left: 200px;

                top: 75px;

            }

        </style>

        <script  type="text/javascript">

            window.onload = function(){

                var box1 = document.getElementById("box1");

                box1.onmouseenter = function(){

                    startMove();

                }

                box1.onmouseout = function(){

                    startMove2();

                }

            }

            var timer = null;

            function startMove(){

                clearInterval(timer);

                timer = setInterval(function(){

                    if(box1.offsetLeft == 0){

                        clearInterval(timer);

                    }

                    else{

                        box1.style.left = box1.offsetLeft + 10 + "px";

                    }

                },30);

            };

            function startMove2(){

                clearInterval(timer);

                timer = setInterval(function(){

                    if(box1.offsetLeft == -200){

                        clearInterval(timer);

                    }

                    else{

                        box1.style.left = box1.offsetLeft - 10 + "px";

                    }

                },30);

            };

        </script>

    </head>

    <body>

        <div id="box1"><span id="span">分享</span></div>

    </body>


写回答 关注

1回答

  • 爱前端的小鑫
    2020-05-05 18:10:43

     box1.onmouseover = function () {

                startMove();

            }

    你单词拼错了

JS动画效果

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

113925 学习 · 1443 问题

查看课程

相似问题