Mouseleave triggered by click

前端 未结 6 1542
囚心锁ツ
囚心锁ツ 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:04

    I had previously looked at the answers and comments here, but recently found a way to check if the mouseleave event was triggered erroneously

    I added a check in my mouseleave handler:

    private handleMouseLeave(event: MouseEvent) {
        if(event.relatedTarget || event.toElement){
            // do whatever
        }
        // otherwise ignore
    }
    

    From my testing on Chrome v64, both of these values will be null whenever fast clicking causes the mouseleave event to be triggered. The relatedTarget is for older browser compatibility

    Note: both of these values will also be null if the mouse leaves the target element and enteres the Browser (e.g. the tabs, menus etc), or leaves the browser window. For my purposes that was not a problem, as it is a sliding menu I am working with, and leaving the Browser window should not close the menu in my particular case.

    Note: latest Firefox release (Feb 2018) seems to trigger mouseleave on every click of my menu! Will have to look into it

提交回复
热议问题