menu mouseenter mouseleave click

前端 未结 1 1836
野性不改
野性不改 2021-01-21 20:44

Anyone can help me? I want a dropdown menu with animation on mouseenter and mouseleave events but when clicked after mouse enter I want the submenu stay visible until new click

相关标签:
1条回答
  • 2021-01-21 21:09

    This could be significantly simplified using event delegation:

    $('document').on("click",'#menu > li', function() {
        $('#menu > li').off('mouseenter mouseleave');
    });
    
    $('document').on("click",':not(#menu > li)', function() {
        $('#menu > li').on('mouseenter', function(){
             //your mouseenter handler goes here
        });
        $('#menu > li').on('mouseleave', function(){
             //your mouseleave handler goes here
        });
    });
    

    This is semantically equivalent to:

    If a click happens on a menu item, unbind mouse enter/leave events
    
    If a click happens on a non menu item, bind mouse enter/leave events
    
    0 讨论(0)
提交回复
热议问题