How to make a Bootstrap 3 dropdown menu open by default when it is inside a collapsed navbar

后端 未结 4 2039
挽巷
挽巷 2020-12-31 02:55

I\'m trying to make my dropdown menu open by default when my collapsed navbar is opened. Here is a fiddle to show my code so far.

Currently, if you

相关标签:
4条回答
  • 2020-12-31 02:57

    You can add a javascript function to your code:

    <script>
    	$(document).ready(function(){
    		$(".dropdown").hover(            
    		function() {
    			$('.dropdown-menu', this).stop( true, true ).slideDown("fast");
    			$(this).toggleClass('open');        
    		},
    		function() {
    			$('.dropdown-menu', this).stop( true, true ).slideUp("fast");
    			$(this).toggleClass('open');       
    		}
    		);
    	});
    </script>

    you can see the example and demo in dtc-eng

    0 讨论(0)
  • 2020-12-31 02:59

    Just remove data-toggle="dropdown"

    0 讨论(0)
  • 2020-12-31 03:18

    According to the docs for Bootstrap v3, you can use Javascript to hook on the collapse events:

    $(function () {
      $('#bs-example-navbar-collapse-1').on('shown.bs.collapse', function(e) {
        $('#my_dropdown').dropdown('toggle', 'open').hide();
        console.log('shown:', e);
      });
    });
    

    I'm not sure the result is what you want, because while the "toggle" button is hidden, the submenu is still indented. Instead of trying to bend Bootstrap that much, perhaps a better solution is having 2 menus with the desired structure for each screen size and show one or another based on media queries.

    Fiddle: http://jsfiddle.net/scardine/tw82o88y/

    0 讨论(0)
  • 2020-12-31 03:24

    You can add css style for responsive mobile display. paste it into your css file:

    @media only screen and (max-width:480px){
        .dropdown-menu{
            display: block;
            position: static;
            background-color:transparent;
            border:0 none;
            box-shadow:none;
            margin-top:0;
            position:static;
            width:100%;
        }
        .navbar-nav .dropdown-menu > li > a, 
        .navbar-nav .dropdown-menu .dropdown-header {
            padding:5px 15px 5px 25px;
        }
        .navbar-nav .dropdown-menu > li > a{
            line-height:20px;
        }
        .navbar-default .navbar-nav .dropdown-menu > li > a{    
            color:#777;
        }
    }
    

    you can see it in its entirety here : Demo jsfiddle

    hope that helps.

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