Mouseleave triggered by click

前端 未结 6 1560
囚心锁ツ
囚心锁ツ 2021-01-01 11:38

I have an absolutely-positioned div, and I\'m trying to keep track of when the mouse moves over it, and when the mouse leaves. Unfortunately clicking on the text in the box

6条回答
  •  醉酒成梦
    2021-01-01 12:02

    This seems to be a bug (I could reproduce it in Chrome with clicks that have the mouse down and mouse up happening rapidly after each other).

    I would suggest to work around this issue by checking whether the mouse is still over the element at the moment the event is fired:

    tooltip.onmouseleave = (e) => {
        if (tooltip === document.elementFromPoint(e.clientX, e.clientY)) {
            console.log('false positive');
            return;
        }
        console.log('tooltip mouse OUT')
    }
    

    The downside is that when the browser window loses focus, that is also considered a false positive. If that is an issue for you, then check this answer.

提交回复
热议问题