How do I check if the mouse is over an element in jQuery?

前端 未结 24 2138
不思量自难忘°
不思量自难忘° 2020-11-22 08:10

Is there a quick & easy way to do this in jQuery that I\'m missing?

I don\'t want to use the mouseover event because I\'m already using it for something else. I

24条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 09:13

    I combined ideas from this topic and came up with this, which is useful for showing/hiding a submenu:

    $("#menu_item_a").mouseenter(function(){
       clearTimeout($(this).data('timeoutId'));
       $("#submenu_a").fadeIn("fast");
    }).mouseleave(function(){
       var menu_item = $(this);
    
       var timeoutId = setTimeout(function(){
          if($('#submenu_a').is(':hover'))
          {
            clearTimeout(menu_item.data('timeoutId'));
          }
          else
          {
            $("#submenu_a").fadeOut("fast");
          }
       }, 650);
    
        menu_item.data('timeoutId', timeoutId); 
    });
    
     $("#submenu_a").mouseleave(function(){
       $(this).fadeOut("fast");
     });
    

    Seems to work for me. Hope this helps someone.

    EDIT: Now realizing this approach is not working correctly in IE.

提交回复
热议问题