Mouseleave triggered by click

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

    var trackmouseup = null;
    
    $('.box').mouseup(function(event){
        if(trackmouseup){
            clearTimeout(trackmouseup);
        }
        trackmouseup = setTimeout(function(){
            trackmouseup = null;
        }, 2); //it must be 2ms or more
    
    });
    
    
    $('.box').mouseleave(function(event){
        //if this event is triggered by click, there must be a mouse up event triggered 2ms ago
        if(trackmouseup){
            return;
        }
    
        //to do something
    });
    

提交回复
热议问题