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

后端 未结 30 3056
小鲜肉
小鲜肉 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: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.

提交回复
热议问题