csdnceshi64
游.程
采纳率33.3%
2012-01-16 09:36 阅读 665

如何使 Bootstrap 菜单下拉鼠标悬停而不是点击

I'd like to have my Bootstrap menu automatically drop down on hover, rather than having to click the menu title. I'd also like to lose the little arrows next to the menu titles.

转载于:https://stackoverflow.com/questions/8878033/how-to-make-twitter-bootstrap-menu-dropdown-on-hover-rather-than-click

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

30条回答 默认 最新

  • 已采纳
    csdnceshi77 狐狸.fox 2012-04-13 14:46

    I created a pure on hover dropdown menu based on the latest (v2.0.2) Bootstrap framework that has support for multiple submenus and thought I'd post it for future users:

    body {
      padding-top: 60px;
      padding-bottom: 40px;
    }
    
    .sidebar-nav {
      padding: 9px 0;
    }
    
    .dropdown-menu .sub-menu {
      left: 100%;
      position: absolute;
      top: 0;
      visibility: hidden;
      margin-top: -1px;
    }
    
    .dropdown-menu li:hover .sub-menu {
      visibility: visible;
    }
    
    .dropdown:hover .dropdown-menu {
      display: block;
    }
    
    .nav-tabs .dropdown-menu,
    .nav-pills .dropdown-menu,
    .navbar .dropdown-menu {
      margin-top: 0;
    }
    
    .navbar .sub-menu:before {
      border-bottom: 7px solid transparent;
      border-left: none;
      border-right: 7px solid rgba(0, 0, 0, 0.2);
      border-top: 7px solid transparent;
      left: -7px;
      top: 10px;
    }
    
    .navbar .sub-menu:after {
      border-top: 6px solid transparent;
      border-left: none;
      border-right: 6px solid #fff;
      border-bottom: 6px solid transparent;
      left: 10px;
      top: 11px;
      left: -6px;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet" />
    
    <div class="navbar navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container-fluid">
          <a data-target=".nav-collapse" data-toggle="collapse" class="btn btn-navbar">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
          <a href="#" class="brand">Project name</a>
          <div class="nav-collapse">
            <ul class="nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li><a href="#">Link</a></li>
              <li class="dropdown">
                <a data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown <b class="caret"></b></a>
                <ul class="dropdown-menu">
                  <li>
                    <a href="#">2-level Dropdown <i class="icon-arrow-right"></i></a>
                    <ul class="dropdown-menu sub-menu">
                      <li><a href="#">Action</a></li>
                      <li><a href="#">Another action</a></li>
                      <li><a href="#">Something else here</a></li>
                      <li class="divider"></li>
                      <li class="nav-header">Nav header</li>
                      <li><a href="#">Separated link</a></li>
                      <li><a href="#">One more separated link</a></li>
                    </ul>
                  </li>
                  <li><a href="#">Another action</a></li>
                  <li><a href="#">Something else here</a></li>
                  <li class="divider"></li>
                  <li class="nav-header">Nav header</li>
                  <li><a href="#">Separated link</a></li>
                  <li><a href="#">One more separated link</a></li>
                </ul>
              </li>
            </ul>
            <form action="" class="navbar-search pull-left">
              <input type="text" placeholder="Search" class="search-query span2">
            </form>
            <ul class="nav pull-right">
              <li><a href="#">Link</a></li>
              <li class="divider-vertical"></li>
              <li class="dropdown">
                <a class="#" href="#">Menu</a>
              </li>
            </ul>
          </div>
          <!-- /.nav-collapse -->
        </div>
      </div>
    </div>
    
    <hr>
    
    <ul class="nav nav-pills">
      <li class="active"><a href="#">Regular link</a></li>
      <li class="dropdown">
        <a href="#" data-toggle="dropdown" class="dropdown-toggle">Dropdown <b class="caret"></b></a>
        <ul class="dropdown-menu" id="menu1">
          <li>
            <a href="#">2-level Menu <i class="icon-arrow-right"></i></a>
            <ul class="dropdown-menu sub-menu">
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
              <li><a href="#">Something else here</a></li>
              <li class="divider"></li>
              <li class="nav-header">Nav header</li>
              <li><a href="#">Separated link</a></li>
              <li><a href="#">One more separated link</a></li>
            </ul>
          </li>
          <li><a href="#">Another action</a></li>
          <li><a href="#">Something else here</a></li>
          <li class="divider"></li>
          <li><a href="#">Separated link</a></li>
        </ul>
      </li>
      <li class="dropdown">
        <a href="#">Menu</a>
      </li>
      <li class="dropdown">
        <a href="#">Menu</a>
      </li>
    </ul>

    Demo

    </div>
    
    点赞 20 评论 复制链接分享
  • weixin_41568183 零零乙 2012-02-24 18:00

    In addition to the answer from "My Head Hurts" (which was great):

    ul.nav li.dropdown:hover ul.dropdown-menu{
        display: block;    
    }
    

    There are 2 lingering issues:

    1. Clicking on the dropdown link will open the dropdown-menu. And it will stay open unless the user clicks somewhere else, or hovers back over it, creating an awkward UI.
    2. There is a 1px margin between the dropdown link, and dropdown-menu. This causes the dropdown-menu to become hidden if you move slowly between the dropdown and dropdown-menu.

    The solution to (1) is removing the "class" and "data-toggle" elements from the nav link

    <a href="#">
         Dropdown
         <b class="caret"></b>
    </a>
    

    This also gives you the ability to create a link to your parent page - which wasn't possible with the default implementation. You can just replace the "#" with whatever page you want to send the user.

    The solution to (2) is removing the margin-top on the .dropdown-menu selector

    .navbar .dropdown-menu {
     margin-top: 0px;
    }
    
    点赞 30 评论 复制链接分享
  • csdnceshi79 python小菜 2012-01-16 10:26

    To get the menu to automatically drop on hover then this can achieved using basic CSS. You need to work out the selector to the hidden menu option and then set it to display as block when the appropriate li tag is hovered over. Taking the example from the twitter bootstrap page, the selector would be as follows:

    ul.nav li.dropdown:hover > ul.dropdown-menu {
        display: block;    
    }
    

    However, if you are using Bootstrap's responsive features, you will not want this functionality on a collapsed navbar (on smaller screens). To avoid this, wrap the code above in a media query:

    @media (min-width: 979px) {
      ul.nav li.dropdown:hover > ul.dropdown-menu {
        display: block;
      }
    }
    

    To hide the arrow (caret) this is done in different ways depending on whether you are using Twitter Bootstrap version 2 and lower or version 3:

    Bootstrap 3

    To remove the caret in version 3 you just need to remove the HTML <b class="caret"></b> from the .dropdown-toggle anchor element:

    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
        Dropdown
        <b class="caret"></b>    <-- remove this line
    </a>
    

    Bootstrap 2 & lower

    To remove the caret in version 2 you need a little more insight into CSS and I suggest looking at how the :after pseudo element works in more detail. To get you started on your way to understanding, to target and remove the arrows in the twitter bootstrap example, you would use the following CSS selector and code:

    a.menu:after, .dropdown-toggle:after {
        content: none;
    }
    

    It will work in your favour if you look further into how these work and not just use the answers that I have given you.

    Thanks to @CocaAkat for pointing out that we were missing the ">" child combinator to prevent sub menus being shown on the parent hover

    点赞 25 评论 复制链接分享
  • csdnceshi58 Didn"t forge 2012-05-22 17:49

    I've used a bit of jQuery:

    // Add hover effect to menus
    jQuery('ul.nav li.dropdown').hover(function() {
      jQuery(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn();
    }, function() {
      jQuery(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut();
    });
    
    点赞 24 评论 复制链接分享
  • csdnceshi61 derek5. 2012-10-12 03:02

    [Update] The plugin is on GitHub and I am working on some improvements (like use only with data-attributes (no JS necessary). I've leaving the code in below, but it's not the same as what's on GitHub.

    I liked the purely CSS version, but it's nice to have a delay before it closes, as it's usually a better user experience (i.e. not punished for a mouse slip that goes 1 px outside the dropdown, etc), and as mentioned in the comments, there's that 1px of margin you have to deal with or sometimes the nav closes unexpectedly when you're moving to the dropdown from the original button, etc.

    I created a quick little plugin that I've used on a couple sites and it's worked nicely. Each nav item is independently handled, so they have their own delay timers, etc.

    JS

    // outside the scope of the jQuery plugin to
    // keep track of all dropdowns
    var $allDropdowns = $();
    
    // if instantlyCloseOthers is true, then it will instantly
    // shut other nav items when a new one is hovered over
    $.fn.dropdownHover = function(options) {
    
        // the element we really care about
        // is the dropdown-toggle's parent
        $allDropdowns = $allDropdowns.add(this.parent());
    
        return this.each(function() {
            var $this = $(this).parent(),
                defaults = {
                    delay: 500,
                    instantlyCloseOthers: true
                },
                data = {
                    delay: $(this).data('delay'),
                    instantlyCloseOthers: $(this).data('close-others')
                },
                options = $.extend(true, {}, defaults, options, data),
                timeout;
    
            $this.hover(function() {
                if(options.instantlyCloseOthers === true)
                    $allDropdowns.removeClass('open');
    
                window.clearTimeout(timeout);
                $(this).addClass('open');
            }, function() {
                timeout = window.setTimeout(function() {
                    $this.removeClass('open');
                }, options.delay);
            });
        });
    };  
    

    The delay parameter is pretty self explanatory, and the instantlyCloseOthers will instantly close all other dropdowns that are open when you hover over a new one.

    Not pure CSS, but hopefully will help someone else at this late hour (i.e. this is an old thread).

    If you want, you can see the different processes I went through (in a discussion on the #concrete5 IRC) to get it to work via the different steps in this gist: https://gist.github.com/3876924

    The plugin pattern approach is much cleaner to support individual timers, etc.

    See the blog post for more.

    点赞 10 评论 复制链接分享
  • csdnceshi58 Didn"t forge 2014-04-05 06:24

    Simply customize your CSS style in three lines of code

    .dropdown:hover .dropdown-menu {
       display: block;
    }
    
    点赞 10 评论 复制链接分享
  • csdnceshi51 旧行李 2017-11-18 20:23

    This is built into Bootstrap 3. Just add this to your CSS:

    .dropdown:hover .dropdown-menu {
    display: block;
    }
    
    点赞 10 评论 复制链接分享
  • csdnceshi71 Memor.の 2013-01-11 07:01

    The jQuery solution is good, but it will need to either deal with on click events (for mobile or tablet) as hover won't work properly... Could maybe do some window re-size detection?

    Andres Ilich's answer seems to work well, but it should be wrapped in a media query:

    @media (min-width: 980px) {
    
        .dropdown-menu .sub-menu {
            left: 100%;
            position: absolute;
            top: 0;
            visibility: hidden;
            margin-top: -1px;
        }
    
        .dropdown-menu li:hover .sub-menu {
            visibility: visible;
        }
    
        .dropdown:hover .dropdown-menu {
            display: block;
        }
    
        .nav-tabs .dropdown-menu, .nav-pills .dropdown-menu, .navbar .dropdown-menu {
            margin-top: 0;
        }
    
        .navbar .sub-menu:before {
            border-bottom: 7px solid transparent;
            border-left: none;
            border-right: 7px solid rgba(0, 0, 0, 0.2);
            border-top: 7px solid transparent;
            left: -7px;
            top: 10px;
        }
        .navbar .sub-menu:after {
            border-top: 6px solid transparent;
            border-left: none;
            border-right: 6px solid #fff;
            border-bottom: 6px solid transparent;
            left: 10px;
            top: 11px;
            left: -6px;
        }
    }
    
    点赞 9 评论 复制链接分享
  • csdnceshi59 ℙℕℤℝ 2014-01-22 05:53

    Overwrite bootstrap.js with this script.

    jQuery(document).ready(function ($) {
    $('.navbar .dropdown').hover(function() {
        $(this).addClass('extra-nav-class').find('.dropdown-menu').first().stop(true, true).delay(250).slideDown();
    }, function() {
        var na = $(this)
        na.find('.dropdown-menu').first().stop(true, true).delay(100).slideUp('fast', function(){ na.removeClass('extra-nav-class') })
    });
    
    $('.dropdown-submenu').hover(function() {
        $(this).addClass('extra-nav-class').find('.dropdown-menu').first().stop(true, true).delay(250).slideDown();
    }, function() {
        var na = $(this)
        na.find('.dropdown-menu').first().stop(true, true).delay(100).slideUp('fast', function(){ na.removeClass('extra-nav-class') })
    });
    
    }); 
    
    点赞 9 评论 复制链接分享
  • csdnceshi64 游.程 2014-03-12 13:33

    So you have this code:

    <a class="dropdown-toggle" data-toggle="dropdown">Show menu</a>
    
    <ul class="dropdown-menu" role="menu">
        <li>Link 1</li>
        <li>Link 2</li> 
        <li>Link 3</li>                                             
    </ul>
    

    Normally it works on a click event, and you want it work on a hover event. This is very simple, just use this JavaScript/jQuery code:

    $(document).ready(function () {
        $('.dropdown-toggle').mouseover(function() {
            $('.dropdown-menu').show();
        })
    
        $('.dropdown-toggle').mouseout(function() {
            t = setTimeout(function() {
                $('.dropdown-menu').hide();
            }, 100);
    
            $('.dropdown-menu').on('mouseenter', function() {
                $('.dropdown-menu').show();
                clearTimeout(t);
            }).on('mouseleave', function() {
                $('.dropdown-menu').hide();
            })
        })
    })
    

    This works very well and here is the explanation: we have a button, and a menu. When we hover the button we display the menu, and when we mouseout of the button we hide the menu after 100 ms. If you wonder why I use that, is because you need time to drag the cursor from the button over the menu. When you are on the menu, the time is reset and you can stay there as many time as you want. When you exit the menu, we will hide the menu instantly without any timeout.

    I've used this code in many projects, if you encounter any problem using it, feel free to ask me questions.

    点赞 9 评论 复制链接分享
  • csdnceshi54 hurriedly% 2017-02-14 13:27
    $('.dropdown').hover(function(e){$(this).addClass('open')})
    
    点赞 9 评论 复制链接分享
  • weixin_41568134 MAO-EYE 2012-08-17 01:51

    Even better with jQuery:

    jQuery('ul.nav li.dropdown').hover(function() {
      jQuery(this).find('.dropdown-menu').stop(true, true).show();
      jQuery(this).addClass('open');
    }, function() {
      jQuery(this).find('.dropdown-menu').stop(true, true).hide();
      jQuery(this).removeClass('open');
    });
    
    点赞 8 评论 复制链接分享
  • csdnceshi54 hurriedly% 2014-09-27 21:13

    If you have an element with a dropdown class like this (for example):

    <ul class="list-unstyled list-inline">
        <li class="dropdown">
            <a data-toggle="dropdown" href="#"><i class="fa fa-bars"></i> Dropdown 1</a>
            <ul class="dropdown-menu">
                <li><a href="">Item 1</a></li>
                <li><a href="">Item 2</a></li>
                <li><a href="">Item 3</a></li>
                <li><a href="">Item 4</a></li>
                <li><a href="">Item 5</a></li>
            </ul>
        </li>
        <li class="dropdown">
            <a data-toggle="dropdown" href="#"><i class="fa fa-user"></i> Dropdown 2</a>
            <ul class="dropdown-menu">
                <li><a href="">Item A</a></li>
                <li><a href="">Item B</a></li>
                <li><a href="">Item C</a></li>
                <li><a href="">Item D</a></li>
                <li><a href="">Item E</a></li>
            </ul>
        </li>
    </ul>
    

    Then you can have the dropdown menu to be automatically drop down on hover over, rather than having to click its title, by using this snippet of jQuery code:

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

    Here is a demo

    This answer relied on @Michael answer, I have made some changes and added some additions to get the dropdown menu work properly

    点赞 8 评论 复制链接分享
  • csdnceshi51 旧行李 2012-10-31 20:22

    Just want to add, that if you have multiple dropdowns (as I do) you should write:

    ul.nav li.dropdown:hover > ul.dropdown-menu {
        display: block;    
    }
    

    And it'll work properly.

    点赞 7 评论 复制链接分享
  • weixin_41568126 乱世@小熊 2012-04-11 16:26

    This will hide the up ones

    .navbar .dropdown-menu:before {
       display:none;
    }
    .navbar .dropdown-menu:after {
       display:none;
    }
    
    点赞 6 评论 复制链接分享
  • csdnceshi78 程序go 2013-09-30 14:35

    I've managed it as follows:

    $('ul.nav li.dropdown').hover(function(){
           $(this).children('ul.dropdown-menu').slideDown(); 
        }, function(){
           $(this).children('ul.dropdown-menu').slideUp(); 
    });
    

    I hope this helps someone...

    点赞 6 评论 复制链接分享
  • weixin_41568174 from.. 2016-10-26 12:52

    Use this code to open the submenu on mousehover (desktop only):

    $('ul.nav li.dropdown').hover(function () {
        if ($(window).width() > 767) {
            $(this).find('.dropdown-menu').show();
        }
    }, function () {
        if ($(window).width() > 767) {
            $(this).find('.dropdown-menu').hide().css('display','');
        }
    });
    

    And if you want the first level menu to be clickable, even on mobile add this:

        $('.dropdown-toggle').click(function() {
        if ($(this).next('.dropdown-menu').is(':visible')) {
            window.location = $(this).attr('href');
        }
    });
    

    The submenu (dropdown-menu) will be opened with mousehover on desktop, and with click/touch on mobile and tablet. Once the submenu was open, a second click will let you open the link. Thanks to the if ($(window).width() > 767), the submenu will take the full screen width on mobile.

    点赞 6 评论 复制链接分享
  • weixin_41568208 北城已荒凉 2014-01-31 17:28

    There are a lot of really good solutions here. But I thought that I would go ahead and put mine in here as another alternative. It's just a simple jQuery snippet that does it the way bootstrap would if it supported hover for dropdowns instead of just click. I've only tested this with version 3 so I don't know if it would work with version 2. Save it as a snippet in your editor and have it at the stroke of a key.

    <script>
        $(function() {
            $(".dropdown").hover(
                function(){ $(this).addClass('open') },
                function(){ $(this).removeClass('open') }
            );
        });
    </script>
    

    Basically, It's just saying when you hover on the dropdown class, it will add the open class to it. Then it just works. When you stop hovering on either the parent li with the dropdown class or the child ul/li's, it removes the open class. Obviously, this is only one of many solutions, and you can add to it to make it work on only specific instances of .dropdown. Or add a transition to either parent or child.

    点赞 5 评论 复制链接分享
  • csdnceshi64 游.程 2014-02-18 19:43

    Here's my technique that adds a slight delay before the menu is closed after you stop hovering on the menu or the toggle button. The <button> that you would normally click to display the nav menu is #nav_dropdown.

    $(function() {
      var delay_close_it, nav_menu_timeout, open_it;
      nav_menu_timeout = void 0;
      open_it = function() {
        if (nav_menu_timeout) {
          clearTimeout(nav_menu_timeout);
          nav_menu_timeout = null;
        }
        return $('.navbar .dropdown').addClass('open');
      };
      delay_close_it = function() {
        var close_it;
        close_it = function() {
          return $('.navbar .dropdown').removeClass('open');
        };
        return nav_menu_timeout = setTimeout(close_it, 500);
      };
      $('body').on('mouseover', '#nav_dropdown, #nav_dropdown *', open_it).on('mouseout', '#nav_dropdown', delay_close_it);
      return $('body').on('mouseover', '.navbar .dropdown .dropdown-menu', open_it).on('mouseout', '.navbar .dropdown .dropdown-menu', delay_close_it);
    });
    
    点赞 5 评论 复制链接分享
  • weixin_41568134 MAO-EYE 2014-05-14 12:05

    Also added margin-top:0 to reset the bootstrap css margin for .dropdown-menu so the menu list dosen't dissapear when the user hovers slowly from drop down menu to the menu list.

    ul.nav li.dropdown:hover > ul.dropdown-menu {
        display: block;    
    }
    
    .nav .dropdown-menu {
        margin-top: 0;
    }
    
    点赞 5 评论 复制链接分享
  • weixin_41568184 叼花硬汉 2012-03-27 17:53

    This should hide the drop downs and their carets if they are smaller than a tablet.

    @media (max-width: 768px) {
        .navbar ul.dropdown-menu, .navbar li.dropdown b.caret {
            display: none;
        }
    }
    
    点赞 4 评论 复制链接分享
  • csdnceshi79 python小菜 2013-03-15 18:34

    This works for WordPress Bootstrap:

    .navbar .nav > li > .dropdown-menu:after,
    .navbar .nav > li > .dropdown-menu:before {
        content: none;
    }
    
    点赞 4 评论 复制链接分享
  • csdnceshi59 ℙℕℤℝ 2015-01-07 10:29

    For the caret... I haven't seen any one specifying simple CSS that totally blocks the caret.

    Here you go:

    .caret {
        display: none !important;
    }
    
    点赞 4 评论 复制链接分享
  • csdnceshi80 胖鸭 2015-02-26 03:30

    To enhance Sudharshan's answer, I wrap this in a media query to prevent the hover when on XS display widths...

    @media (min-width:768px)
    {
        ul.nav li.dropdown:hover > ul.dropdown-menu {
            display: block;    
        }
    
        .nav .dropdown-menu {
            margin-top: 0;
        }
    }
    

    Also the caret in the markup is not required, just the dropdown class for the li.

    点赞 4 评论 复制链接分享
  • csdnceshi56 lrony* 2015-06-27 03:13

    I have published a proper plugin for the Bootstrap 3 dropdown hover functionality, in which you can even define what happens when clicking on the dropdown-toggle element (the click can be disabled):

    https://github.com/istvan-ujjmeszaros/bootstrap-dropdown-hover


    Why I made it when there are many solutions already?

    I had issues with all the previously existing solutions. The simple CSS ones are not using the .open class on the .dropdown, so there will be no feedback on the drop-down toggle element when the drop-down is visible.

    The js ones are interfering with clicking on .dropdown-toggle, so the dropdown shows up on hover, then hides it when clicking on an opened drop-down, and moving out the mouse will trigger the drop-down to show up again. Some of the js solutions are breaking iOS compatibility, some plugins are not working on modern desktop browsers which are supporting the touch events.

    That's why I made the Bootstrap Dropdown Hover plugin which prevents all these issues by using only the standard Bootstrap javascript API, without any hack. Even the Aria attributes are working fine with this plugin.

    点赞 4 评论 复制链接分享
  • csdnceshi77 狐狸.fox 2015-10-06 11:47

    This worked for me:

    .dropdown:hover .dropdown-menu {
        display: block;
    }
    
    点赞 3 评论 复制链接分享
  • csdnceshi73 喵-见缝插针 2014-03-01 08:52

    The best way of doing it is to just trigger Bootstrap's click event with a hover. This way, it should still remain touch device friendly.

    $('.dropdown').hover(function(){ 
      $('.dropdown-toggle', this).trigger('click'); 
    });
    
    点赞 2 评论 复制链接分享
  • csdnceshi63 elliott.david 2012-02-27 18:01

    This is probably a stupid idea, but to just remove the arrow pointing down, you can delete the

    <b class="caret"></b>
    

    This does nothing for the up pointing one, though...

    点赞 1 评论 复制链接分享
  • csdnceshi79 python小菜 2013-09-10 09:53

    In my opinion the best way is like this:

    ;(function($, window, undefined) {
        // Outside the scope of the jQuery plugin to
        // keep track of all dropdowns
        var $allDropdowns = $();
    
        // If instantlyCloseOthers is true, then it will instantly
        // shut other nav items when a new one is hovered over
        $.fn.dropdownHover = function(options) {
    
            // The element we really care about
            // is the dropdown-toggle's parent
            $allDropdowns = $allDropdowns.add(this.parent());
    
            return this.each(function() {
                var $this = $(this),
                    $parent = $this.parent(),
                    defaults = {
                        delay: 500,
                        instantlyCloseOthers: true
                    },
                    data = {
                        delay: $(this).data('delay'),
                        instantlyCloseOthers: $(this).data('close-others')
                    },
                    settings = $.extend(true, {}, defaults, options, data),
                    timeout;
    
                $parent.hover(function(event) {
    
                    // So a neighbor can't open the dropdown
                    if(!$parent.hasClass('open') && !$this.is(event.target)) {
                        return true;
                    }
    
                    if(settings.instantlyCloseOthers === true)
                        $allDropdowns.removeClass('open');
    
                    window.clearTimeout(timeout);
                    $parent.addClass('open');
                }, function() {
                    timeout = window.setTimeout(function() {
                        $parent.removeClass('open');
                    }, settings.delay);
                });
    
                // This helps with button groups!
                $this.hover(function() {
                    if(settings.instantlyCloseOthers === true)
                        $allDropdowns.removeClass('open');
    
                    window.clearTimeout(timeout);
                    $parent.addClass('open');
                });
    
                // Handle submenus
                $parent.find('.dropdown-submenu').each(function(){
                    var $this = $(this);
                    var subTimeout;
                    $this.hover(function() {
                        window.clearTimeout(subTimeout);
                        $this.children('.dropdown-menu').show();
    
                        // Always close submenu siblings instantly
                        $this.siblings().children('.dropdown-menu').hide();
                    }, function() {
                        var $submenu = $this.children('.dropdown-menu');
                        subTimeout = window.setTimeout(function() {
                            $submenu.hide();
                        }, settings.delay);
                    });
                });
            });
        };
    
        $(document).ready(function() {
            // apply dropdownHover to all elements with the data-hover="dropdown" attribute
            $('[data-hover="dropdown"]').dropdownHover();
        });
    })(jQuery, this);
    

    Sample markup:

    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="false">
            Account <b class="caret"></b>
        </a>
        <ul class="dropdown-menu">
            <li><a tabindex="-1" href="#">My Account</a></li>
            <li class="divider"></li>
            <li><a tabindex="-1" href="#">Change Email</a></li>
            <li><a tabindex="-1" href="#">Change Password</a></li>
            <li class="divider"></li>
            <li><a tabindex="-1" href="#">Logout</a></li>
        </ul>
    </li>
    
    点赞 1 评论 复制链接分享
  • csdnceshi64 游.程 2015-09-23 08:47

    You can use the default $().dropdown('toggle') method to toggle the dropdown menu on hover:

    $(".nav .dropdown").hover(function() {
      $(this).find(".dropdown-toggle").dropdown("toggle");
    });
    
    点赞 1 评论 复制链接分享

相关推荐