问题
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ü" />
<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