[英]Buggy jquery hover menu
I got tasked with fixing a buggy dropdown menu that reacts on hover. 我的任务是修复在悬停时有反应的错误下拉菜单。 When I move the mouse from the button that should trigger the dropdown to the dropdown it sometimes closes the dropdown. 当我将鼠标从应该触发下拉菜单的按钮移到下拉菜单时,有时会关闭下拉菜单。 I feel like it triggers the leave-event when I leave the button even though I'm not leaving the wrapper. 我觉得即使我没有离开包装器,当我离开按钮时它也会触发离开事件。
The html is something like this: html是这样的:
<div id="shopcart_wrapper">
<div class="hover_here">Shopcart</div>
<div id="shopcartsummary" class="closed">
Shopcartstuff here
</div>
</div>
Javascript is like this: Javascript是这样的:
shopcartShowMouseEnter: function () {
$('#shopcart_wrapper').on('mouseenter', function(){
if(Modernizr.cssanimations){
$('#shopcartsummary').addClass('open').removeClass('closed');
}else{
if(!$('#shopcartsummary').is(':animated')){
eStore.shopcart.show();
}
}
});
},
shopcartHideMouseLeave: function(){
$('#shopcart_wrapper').on('mouseleave', function(){
if(Modernizr.cssanimations){
$('#shopcartsummary').removeClass('open').addClass('closed');
}else{
if(!$('#shopcartsummary').is(':animated')){
eStore.shopcart.hide();
}
}
});
}
I feel like the fix should be to check if I'm still hovering over the #shopcartsummary before I trigger any events in the mouseleave. 我觉得解决方法应该是在触发mouseleave中的任何事件之前检查我是否仍在#shopcartsummary上徘徊。 But how do I do this in the best way? 但是,如何以最佳方式做到这一点?
I think the solution I was after is this: 我认为我追求的解决方案是:
shopcartHideMouseLeave: function(){
if(!$('#shopcartsummary').is(':hover')){
$('#shopcart_wrapper').on('mouseleave', function(){
if(Modernizr.cssanimations){
$('#shopcartsummary').removeClass('open').addClass('closed');
}else{
if(!$('#shopcartsummary').is(':animated')){
eStore.shopcart.hide();
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.