jQuery Hover Menu disappears when right click

Deadly 提交于 2019-12-23 02:16:56

问题


I have a Menu that opens when hovering. But on right click the menu disappears when the contextmenu opens. But I can't figure out why. I need the hover menu open at the same time with contextmenu/right click.

The jQuery Code (Version jquery-1.11.2.min.js) :

    jQuery(document).on('mouseover','#main_menu',function() {

        jQuery('#main_menu_inner').show();

    });
    jQuery(document).on('mouseleave','#main_menu',function() {

        jQuery('#main_menu_inner').hide();

    });

The HTML:

<div id="main_menu">
    <img id="menu_button" src="/skin/images/all/structure/menu_button.png" alt="Men&uuml;" />
    <div id="main_menu_inner">
        <img id="menu_arrow" src="/skin/images/all/structure/main_menu_arrow_down.png" alt="Arrow" />
        <div class="clear_right"></div>
        <ul>
            <li>
                <a href="">Link</a>
            </li>
            <li>
                <a href="">Link</a>
            </li>
            <li>
                <a href="">Link</a>
            </li>
        </ul>
    </div>
</div>

回答1:


You can "hack" the mouseover/mouseleave behaviour by adding a boolean to check if the context menu is opened into the mouseleave event handler. That's not a really good practice, but it makes your request possible:

var contextMenuOpened = false;

$(document).on('mouseover', '#main_menu', function () {
    $('#main_menu_inner').show();
    contextMenuOpened = false;
});

$(document).on('mouseleave', '#main_menu', function () {
    $("#main_menu").on('contextmenu', function (e) {
        contextMenuOpened = true;
    });
    if (!contextMenuOpened) {
        $('#main_menu_inner').hide();
    }
});

Live exemple



来源:https://stackoverflow.com/questions/29693979/jquery-hover-menu-disappears-when-right-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!