简体   繁体   English

为什么鼠标悬停时我的悬停按钮下拉菜单没有保持打开状态?

[英]Why does my hover button dropdown not stay open when I mouse-over?

First things first: http://jsfiddle.net/9L81kfah/第一件事: http : //jsfiddle.net/9L81kfah/

I have a Bootstrap dropdown that's supposed to open and stay open if somebody does a mouse-over for more than 200ms, especially if they then move the mouse over the menu content, but it's not staying open.我有一个 Bootstrap 下拉菜单,如果有人将鼠标悬停在 200 毫秒以上,它应该会打开并保持打开状态,尤其是当他们将鼠标移到菜单内容上时,但它不会保持打开状态。 What am I doing wrong here?我在这里做错了什么?

This is the dropdown code:这是下拉代码:

<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
    Dropdown
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Something else here</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Separated link</a></li>
  </ul>
</div>

And the jQuery:和 jQuery:

jQuery('#dropdownMenu1').hover(function() {
        jQuery(this).next('.dropdown-menu').stop(true, true).delay(200).fadeIn();
}, function() {
        jQuery(this).next('.dropdown-menu').stop(true, true).delay(200).fadeOut();
});

It's because your hover handler is on the button element and as soon as you mouseover the menu element the "mouseout" handler triggers because you left the button.这是因为您的悬停处理程序位于按钮元素上,一旦您将鼠标悬停在菜单元素上,“mouseout”处理程序就会触发,因为您离开了按钮。 Instead your handler should be on the surrounding .dropdown element.相反,您的处理程序应该位于周围的.dropdown元素上。

$('.dropdown').hover(function () {
  $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn();
}, function () {
  $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut();
});

Now when you're hovering the button it will work because the button is a child of the .dropdown element and the hover event bubbles up through the parent elements.现在,当您将鼠标悬停在按钮上时,它将起作用,因为该按钮是.dropdown元素的子元素,并且悬停事件通过父元素向上冒泡 When you move the mouse from the button to the dropdown menu you'll still be hovering over .dropdown as well because the menu too is a child of .dropdown .当您将鼠标从按钮移动到下拉菜单时,您仍然会悬停在.dropdown ,因为菜单也是.dropdown的子.dropdown Only when you leave the parent element entirely will the "mouseout" handler fire.只有当您完全离开父元素时,“mouseout”处理程序才会触发。

http://jsfiddle.net/1xpLm4jw/ http://jsfiddle.net/1xpLm4jw/

You're telling your dropdown to fade out after you mouse off the button.鼠标离开按钮后,您告诉下拉菜单淡出。 Instead, you should tell it to fade out after you mouse off the entire dropdown group.相反,您应该在鼠标移开整个下拉组后告诉它淡出。

jQuery('#dropdownMenu1').hover(function() {
        jQuery(this).next('.dropdown-menu').stop(true, true).delay(200).fadeIn();
});
jQuery('.dropdown').on("mouseout", function() {
        jQuery('.dropdown-menu').stop(true, true).delay(200).fadeOut();
});

You would need to have a timer to get this working.您需要有一个计时器才能使其正常工作。 Only trigger the fadeOut if the mouse is not over the dropdown .如果鼠标不在dropdown则仅触发淡出。 Simply put you can use the combination of mouseover and mouseout events.简单地说,您可以结合使用mouseovermouseout事件。

var timeout;
jQuery('.dropdown').on('mouseover', function () {
    clearInterval(timeout);
    jQuery('.dropdown-menu', this).stop(true, true).delay(200).fadeIn();
});

jQuery('.dropdown').on('mouseout', function () {
    var thisView = this;
    timeout = setTimeout(function () {
        jQuery('.dropdown-menu', thisView).stop(true, true).delay(200).fadeOut();
    }, 200);
});

Check Fiddle检查小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM