鼠标移动上去没有停止

来源:8-2 编程练习

慕后端8593059

2018-12-23 15:36:03

     //补充代码

        var posx=30,

            posy=0,

            dir=1,

            isMouseInRect=false;

        canvas.onmousemove=function(e){

            var mouseX=e.offsetX;

            var mouseY=e.offsetY;

            if( mouseX > posx && mouseX < posx+30 && 

                mouseY > posy && mouseY < posy+30){

                isMouseInRect=true;

            }else{

                isMouseInRect=false;

            }

        }

        setInterval(function(){

            if(!isMouseInRect){

                posy+=10*dir;

            }

            context.clearRect(30,0,canvas.width,canvas.height);

            context.fillStyle='red';

            context.fillRect(posx,posy,30,30);

            posy+=10*dir;

            if(posy+30>=canvas.height){

                dir=-1;

            }else if(posy<=0){

                dir=1;

            }

        },1000);

写回答

1回答

好帮手慕星星

2018-12-24

同学你好,经测试在onmousemove事件中并没有清除定时器,建议给定时器放在一个函数中,可以调用,参考:

http://img.mukewang.com/climg/5c20543900016bbb09870673.jpg

http://img.mukewang.com/climg/5c20544b000171e610310603.jpg

自己可以参考上面修改测试下。

祝学习愉快!

0

0 学习 · 4826 问题

查看课程