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

后端 未结 30 2636
小鲜肉
小鲜肉 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:30
    $('.dropdown').hover(function(e){$(this).addClass('open')})
    
    0 讨论(0)
  • 2020-11-22 02:32

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

    Simply customize your CSS style in three lines of code

    .dropdown:hover .dropdown-menu {
       display: block;
    }
    
    0 讨论(0)
  • 2020-11-22 02:35

    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...

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

    [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.

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

    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.

    0 讨论(0)
提交回复
热议问题