如果我的鼠标不在其中,为什么我可以点击按钮?

时间:2016-04-02 23:43:57

标签: javascript mouse pause

我正在用暂停按钮制作一个js游戏。按钮工作正常,但是如果我的鼠标进入按钮,离开,然后单击,则会触发效果。这是我的代码:

    document.onmousemove=function(mouse){
    mouseX=mouse.clientX-8;
    mouseY=mouse.clientY-8;
    }
    //some time later in code
    if(mouseX > Img.pause.x && mouseX < Img.pause.x+Img.pause.width && mouseY>Img.pause.y&& mouseY<Img.pause.y+Img.pause.height){//checked if mouse in button
    document.getElementById('ctx').style.cursor="pointer";
    document.onclick=function(){
    pause=true;//if mouse in button AND it clicks
    }
    }
    else{
    document.getElementById('ctx').style.cursor="default";  
    }

问题是鼠标不必在按钮中,它只需要在其中一段时间​​。我怎样才能使鼠标必须在里面然后点击?

0 个答案:

没有答案