欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

bootstrap dropdown

shiping1 的头像

OK, so what I need is fairly straightforward.

I have set up a navbar with some dropdown menus in it (using class="dropdown-toggle" data-toggle="dropdown"), and it works fine.

The thing is it works "onClick", while I would prefer if it worked "onHover".

Is there any built-in way to do this?

share|improve this question
 

正确答案  The easiest solution would be in CSS. Add something like...
好像不对父元素是li 子元素是ul的元素起作用

.dropdown:hover .dropdown-menu {
    display: block;
 }

Working Fiddle

share|improve this answer
 
3 
So much better than long 'full-built' solutions. Just add this and the default 'click' dropdown works on hover without extra changes. –  IamFace Apr 13 at 19:59
7 
This will conflict with the built in collapsable mobile menu, so you should wrap it in a break point @media (min-width:480px) –  Billy Shih May 29 at 19:56 
   
Also noteworthy: if you have a dropdown menu with parent-child relationships, for instance in a wordpress theme, you need to remove the data-toggle attribute to make the parent item click-able...Just watch out for collateral damage on mobile screens. –  KJ Prince Jun 4 at 20:07 

you can use jquery hover function

you just need to add class 'open' when mouse hover and remove the 'open' class when mouse leave the dropdown

here's my code

$(function(){
    $('.dropdown').hover(function() {
         $(this).toggleClass('open');
    });
});
share|improve this answer
 

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

$('.dropdown').hover(function(){ 
  $('.dropdown-toggle', this).trigger('click'); 
});
share|improve this answer
 

Hover over the nav items to see that they activate on hover. http://cameronspear.com/demos/twitter-bootstrap-hover-dropdown/#

share|improve this answer
 

In Twitter Bootstrap is not implemented but you can use the this plugin

Update 1:

Sames question here

share|improve this answer
 

An easy way, using jQuery, is this:

$(document).ready(function(){
    $('ul.nav li.dropdown').hover(function() {
      $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn(200);
    }, function() {
      $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut(200);
    });  
});
share|improve this answer
 
   
Simply Cool Answer. +1 –  Pendlimarri's Oct 1 at 12:07

This only hovers the navbar when you are not on a mobile device, because I find that hovering the navigation does not work well on mobile divices:

    $( document ).ready(function() {

    $( 'ul.nav li.dropdown' ).hover(function() {
        // you could also use this condition: $( window ).width() >= 768
        if ($('.navbar-toggle').css('display') === 'none' 
            && false === ('ontouchstart' in document)) {

            $( '.dropdown-toggle', this ).trigger( 'click' );
        }
    }, function() {
        if ($('.navbar-toggle').css('display') === 'none'
            && false === ('ontouchstart' in document)) {

            $( '.dropdown-toggle', this ).trigger( 'click' );
        }
    });

});
share|improve this answer
 

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 click event, and you want it work on 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 100ms. 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.

share|improve this answer
 

This will help you make your own hover class for bootstrap:

CSS:

/* Hover dropdown */
.hover_drop_down.input-group-btn ul.dropdown-menu{margin-top: 0px;}/*To avoid unwanted close*/
.hover_drop_down.btn-group ul.dropdown-menu{margin-top:2px;}/*To avoid unwanted close*/
.hover_drop_down:hover ul.dropdown-menu{
   display: block;
}

Margins are set to avoid unwanted close and they are optional.

HTML:

<div class="btn-group hover_drop_down">
  <button type="button" class="btn btn-default" data-toggle="dropdown"></button>
  <ul class="dropdown-menu" role="menu">
    ...
  </ul>
</div>

Don't forget to remove the button attribute data-toggle="dropdown" if you want to remove onclick open, and this also will work when input is append with dropdown.

share|improve this answer
 

This is what I use to make it dropdown on hover with some jQuery

$(document).ready(function () {
    $('.navbar-default .navbar-nav > li.dropdown').hover(function () {
        $('ul.dropdown-menu', this).stop(true, true).slideDown('fast');
        $(this).addClass('open');
    }, function () {
        $('ul.dropdown-menu', this).stop(true, true).slideUp('fast');
        $(this).removeClass('open');
    });
});
share|improve this answer
 

I try other solutions, i'm using bootstrap 3, but dropdown menu closes too quickly to move over it

supposed that you add class="dropdown" to li, i added a timeout

var hoverTimeout;
$('.dropdown').hover(function() {
    clearTimeout(hoverTimeout);
    $(this).addClass('open');
}, function() {
    var $self = $(this);
    hoverTimeout = setTimeout(function() {
        $self.removeClass('open');
    }, 150);
});

来自 http://stackoverflow.com/questions/16214326/bootstrap-dropdown-with-hover
普通分类: