问答详情
源自:2-1 JS速度动画

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

<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>


提问者:慕丝7111264 2020-04-03 11:50

个回答

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

     box1.onmouseover = function () {

                startMove();

            }

    你单词拼错了