How to make Twitter Bootstrap menu dropdown on hover rather than click

后端 未结 30 2634
小鲜肉
小鲜肉 2020-11-22 02:08

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 t

相关标签:
30条回答
  • 2020-11-22 02:26

    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.

    0 讨论(0)
  • 2020-11-22 02:26

    This will hide the up ones

    .navbar .dropdown-menu:before {
       display:none;
    }
    .navbar .dropdown-menu:after {
       display:none;
    }
    
    0 讨论(0)
  • 2020-11-22 02:26

    The very simple solution for version 2, only CSS. Keeps the same friendly functionality for mobile and tablet.

    @media (min-width: 980px) {
        .dropdown:hover .dropdown-menu {
           display: block;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:27

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:29

    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.

    0 讨论(0)
  • 2020-11-22 02:29

    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>
    
    0 讨论(0)
提交回复
热议问题