Bootstrap 3下拉子菜单丢失。

时间:2021-10-01 19:57:31

Bootstrap 3 is still at RC, but I was just trying to implement it. I couldn't figure out how to put a sub menu class. Even there is no class in css and even the new docs don't say anything about it

Bootstrap 3仍然在RC中,但我只是想实现它。我不知道如何设置子菜单类。即使在css中也没有类,甚至新文档也没有提到它。

It was there in 2.x with class name as dropdown-submenu

它在那里。带有类名的x作为下拉子菜单。

7 个解决方案

#1


482  

Updated 2018

更新2018

The dropdown-submenu has been removed in Bootstrap 3 RC. In the words of Bootstrap author Mark Otto..

在Bootstrap 3 RC中删除了下拉子菜单。用Bootstrap作者Mark Otto的话说。

"Submenus just don't have much of a place on the web right now, especially the mobile web. They will be removed with 3.0" - https://github.com/twbs/bootstrap/pull/6342

子菜单现在在网络上没有太多的位置,尤其是移动网络。他们将被删除3.0”——https://github.com/twbs/bootstrap/pull/6342。

But, with a little extra CSS you can get the same functionality.

但是,加上一点额外的CSS,你就可以得到相同的功能。

Bootstrap 4 (navbar submenu on hover)

Bootstrap 4(悬停的导航栏子菜单)

.navbar-nav li:hover > ul.dropdown-menu {
    display: block;
}
.dropdown-submenu {
    position:relative;
}
.dropdown-submenu>.dropdown-menu {
    top:0;
    left:100%;
    margin-top:-6px;
}

https://www.codeply.com/go/ji5ijk6yJ4

https://www.codeply.com/go/ji5ijk6yJ4


Bootstrap 3

引导3

Here is an example that uses 3.0 RC 1: http://bootply.com/71520

这里有一个使用3.0 RC 1的例子:http://bootply.com/71520。

Here is an example that uses Bootstrap 3.0.0 (final): http://bootply.com/86684

下面是一个使用Bootstrap 3.0.0 (final)的示例:http://bootply.com/86684。

CSS

CSS

.dropdown-submenu {
    position:relative;
}
.dropdown-submenu>.dropdown-menu {
    top:0;
    left:100%;
    margin-top:-6px;
    margin-left:-1px;
    -webkit-border-radius:0 6px 6px 6px;
    -moz-border-radius:0 6px 6px 6px;
    border-radius:0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
    display:block;
}
.dropdown-submenu>a:after {
    display:block;
    content:" ";
    float:right;
    width:0;
    height:0;
    border-color:transparent;
    border-style:solid;
    border-width:5px 0 5px 5px;
    border-left-color:#cccccc;
    margin-top:5px;
    margin-right:-10px;
}
.dropdown-submenu:hover>a:after {
    border-left-color:#ffffff;
}
.dropdown-submenu.pull-left {
    float:none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
    left:-100%;
    margin-left:10px;
    -webkit-border-radius:6px 0 6px 6px;
    -moz-border-radius:6px 0 6px 6px;
    border-radius:6px 0 6px 6px;
}

Sample Markup

样品的标记

<div class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">  
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>
        <div class="collapse navbar-collapse navbar-ex1-collapse">
            <ul class="nav navbar-nav">
                <li class="menu-item dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Drop Down<b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li class="menu-item dropdown dropdown-submenu">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Level 1</a>
                            <ul class="dropdown-menu">
                                <li class="menu-item ">
                                    <a href="#">Link 1</a>
                                </li>
                                <li class="menu-item dropdown dropdown-submenu">
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Level 2</a>
                                    <ul class="dropdown-menu">
                                        <li>
                                            <a href="#">Link 3</a>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</div>

P.S. - Example in navbar that adjusts left position: http://bootply.com/92442

在导航栏中调整左位置的例子:http://bootply.com/92442。

#2


59  

@skelly solution is good but will not work on mobile devices as the hover state won't work.

@skelly解决方案很好,但是不能在移动设备上工作,因为悬浮状态不会起作用。

I have added a little bit of JS to get the BS 2.3.2 behavior back.

我添加了一些JS,以使BS 2.3.2的行为恢复正常。

PS: it will work with the CSS you get there: http://bootply.com/71520 though you can comment the following part:

PS:它将与CSS一起工作:http://bootply.com/71520,你可以评论以下部分:

CSS:

CSS:

/*.dropdown-submenu:hover>.dropdown-menu{display:block;}*/

JS:

JS:

$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
  // Avoid following the href location when clicking
  event.preventDefault();
  // Avoid having the menu to close when clicking
  event.stopPropagation();
  // If a menu is already open we close it
  $('ul.dropdown-menu [data-toggle=dropdown]').parent().removeClass('open');
  // opening the one you clicked on
  $(this).parent().addClass('open');
});

The result can be found on my WordPress theme (Top of the page): http://shprinkone.julienrenaux.fr/

结果可以在我的WordPress主题(页面顶部)找到:http://shprinkone.julienrenaux.fr/。

#3


41  

Until today (9 jan 2014) the Bootstrap 3 still not support sub menu dropdown.

直到今天(2014年1月9日),Bootstrap 3仍然不支持子菜单下拉。

I searched Google about responsive navigation menu and found this is the best i though.

我搜索了谷歌的响应式导航菜单,发现这是最好的。

It is Smart menus http://www.smartmenus.org/

它是智能菜单http://www.smartmenus.org/。

I hope this is the way out for anyone who want navigation menu with multilevel sub menu.

我希望这是为任何想要导航菜单和多级子菜单的人的出路。

update 2015-02-17 Smart menus are now fully support Bootstrap element style for submenu. For more information please look at Smart menus website.

更新2015-02-17智能菜单现在完全支持子菜单的引导元素样式。如需更多信息,请浏览智能菜单网站。

#4


5  

Shprink's code helped me the most, but to avoid the dropdown to go off-screen i updated it to:

Shprink的代码帮了我最大的忙,但为了避免下拉菜单,我将它更新为:

JS:

JS:

$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
    // Avoid following the href location when clicking
    event.preventDefault(); 
    // Avoid having the menu to close when clicking
    event.stopPropagation(); 
    // If a menu is already open we close it
    $('ul.dropdown-menu [data-toggle=dropdown]').parent().removeClass('open');
    // opening the one you clicked on
    $(this).parent().addClass('open');

    var menu = $(this).parent().find("ul");
    var menupos = $(menu).offset();

    if (menupos.left + menu.width() > $(window).width()) {
        var newpos = -$(menu).width();
        menu.css({ left: newpos });    
    } else {
        var newpos = $(this).parent().width();
        menu.css({ left: newpos });
    }

});

CSS: FROM background-color: #eeeeee TO background-color: #c5c5c5 - white font & light background wasn't looking good.

CSS:从背景色:#eeeeee到背景色:# c5c5c5c5 -白色字体和浅色背景看起来不太好。

.nav .open > a,
.nav .open > a:hover,
.nav .open > a:focus {
  background-color: #c5c5c5;
  border-color: #428bca;
}

I hope this helps people as much as it did for me!

我希望这能像对我一样帮助人们!

But i hope Bootstrap add the subs feature back ASAP.

但是我希望Bootstrap能够尽快地添加这些子特性。

#5


3  

Comment to Skelly's really helpful workaround: in Bootstrap-sass 3.3.6, utilities.scss, line 19: .pull-left has float:left !important. Since that, I recommend to use !important in his CSS as well:

对Skelly真正有用的解决方法:在Bootstrap-sass 3.3.6中,实用工具。scss,第19行:.pull-left有float:left !重要。既然这样,我推荐使用!在他的CSS中也很重要:

.dropdown-submenu.pull-left {
    float:none !important;
}

#6


2  

I bumped with this issue a few days ago. I tried many solutions and none really worked for me on the end i ended up creating an extenion/override of the dropdown code of bootstrap. It is a copy of the original code with changes to the closeMenus function.

几天前我碰到了这个问题。我尝试了许多解决方案,但没有一个真正为我工作,最后我创建了一个extenion/重写了bootstrap的下拉代码。它是对closeMenus函数的更改的原始代码的副本。

I think it is a good solution since it doesn't affects the core classes of bootstrap js.

我认为这是一个很好的解决方案,因为它不会影响bootstrap js的核心类。

You can check it out on gihub: https://github.com/djokodonev/bootstrap-multilevel-dropdown

您可以在gihub上查看它:https://github.com/djokodonev/bootstrap-multilevel-dropdown。

#7


-2  

I make another solution for dropdown. Hope this is helpfull Just add this js script

我为下拉做了另一个解决方案。希望这是helpfull只是添加这个js脚本。

<script type="text/javascript"> jQuery("document").ready(function() {
  jQuery("ul.dropdown-menu > .dropdown.parent").click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    if (jQuery(this).hasClass('open2'))
      jQuery(this).removeClass('open2');
    else {
      jQuery(this).addClass('open2');
    }

  });
}); < /script>

<style type="text/css">.open2{display:block; position:relative;}</style>

#1


482  

Updated 2018

更新2018

The dropdown-submenu has been removed in Bootstrap 3 RC. In the words of Bootstrap author Mark Otto..

在Bootstrap 3 RC中删除了下拉子菜单。用Bootstrap作者Mark Otto的话说。

"Submenus just don't have much of a place on the web right now, especially the mobile web. They will be removed with 3.0" - https://github.com/twbs/bootstrap/pull/6342

子菜单现在在网络上没有太多的位置,尤其是移动网络。他们将被删除3.0”——https://github.com/twbs/bootstrap/pull/6342。

But, with a little extra CSS you can get the same functionality.

但是,加上一点额外的CSS,你就可以得到相同的功能。

Bootstrap 4 (navbar submenu on hover)

Bootstrap 4(悬停的导航栏子菜单)

.navbar-nav li:hover > ul.dropdown-menu {
    display: block;
}
.dropdown-submenu {
    position:relative;
}
.dropdown-submenu>.dropdown-menu {
    top:0;
    left:100%;
    margin-top:-6px;
}

https://www.codeply.com/go/ji5ijk6yJ4

https://www.codeply.com/go/ji5ijk6yJ4


Bootstrap 3

引导3

Here is an example that uses 3.0 RC 1: http://bootply.com/71520

这里有一个使用3.0 RC 1的例子:http://bootply.com/71520。

Here is an example that uses Bootstrap 3.0.0 (final): http://bootply.com/86684

下面是一个使用Bootstrap 3.0.0 (final)的示例:http://bootply.com/86684。

CSS

CSS

.dropdown-submenu {
    position:relative;
}
.dropdown-submenu>.dropdown-menu {
    top:0;
    left:100%;
    margin-top:-6px;
    margin-left:-1px;
    -webkit-border-radius:0 6px 6px 6px;
    -moz-border-radius:0 6px 6px 6px;
    border-radius:0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
    display:block;
}
.dropdown-submenu>a:after {
    display:block;
    content:" ";
    float:right;
    width:0;
    height:0;
    border-color:transparent;
    border-style:solid;
    border-width:5px 0 5px 5px;
    border-left-color:#cccccc;
    margin-top:5px;
    margin-right:-10px;
}
.dropdown-submenu:hover>a:after {
    border-left-color:#ffffff;
}
.dropdown-submenu.pull-left {
    float:none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
    left:-100%;
    margin-left:10px;
    -webkit-border-radius:6px 0 6px 6px;
    -moz-border-radius:6px 0 6px 6px;
    border-radius:6px 0 6px 6px;
}

Sample Markup

样品的标记

<div class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">  
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>
        <div class="collapse navbar-collapse navbar-ex1-collapse">
            <ul class="nav navbar-nav">
                <li class="menu-item dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Drop Down<b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li class="menu-item dropdown dropdown-submenu">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Level 1</a>
                            <ul class="dropdown-menu">
                                <li class="menu-item ">
                                    <a href="#">Link 1</a>
                                </li>
                                <li class="menu-item dropdown dropdown-submenu">
                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Level 2</a>
                                    <ul class="dropdown-menu">
                                        <li>
                                            <a href="#">Link 3</a>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</div>

P.S. - Example in navbar that adjusts left position: http://bootply.com/92442

在导航栏中调整左位置的例子:http://bootply.com/92442。

#2


59  

@skelly solution is good but will not work on mobile devices as the hover state won't work.

@skelly解决方案很好,但是不能在移动设备上工作,因为悬浮状态不会起作用。

I have added a little bit of JS to get the BS 2.3.2 behavior back.

我添加了一些JS,以使BS 2.3.2的行为恢复正常。

PS: it will work with the CSS you get there: http://bootply.com/71520 though you can comment the following part:

PS:它将与CSS一起工作:http://bootply.com/71520,你可以评论以下部分:

CSS:

CSS:

/*.dropdown-submenu:hover>.dropdown-menu{display:block;}*/

JS:

JS:

$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
  // Avoid following the href location when clicking
  event.preventDefault();
  // Avoid having the menu to close when clicking
  event.stopPropagation();
  // If a menu is already open we close it
  $('ul.dropdown-menu [data-toggle=dropdown]').parent().removeClass('open');
  // opening the one you clicked on
  $(this).parent().addClass('open');
});

The result can be found on my WordPress theme (Top of the page): http://shprinkone.julienrenaux.fr/

结果可以在我的WordPress主题(页面顶部)找到:http://shprinkone.julienrenaux.fr/。

#3


41  

Until today (9 jan 2014) the Bootstrap 3 still not support sub menu dropdown.

直到今天(2014年1月9日),Bootstrap 3仍然不支持子菜单下拉。

I searched Google about responsive navigation menu and found this is the best i though.

我搜索了谷歌的响应式导航菜单,发现这是最好的。

It is Smart menus http://www.smartmenus.org/

它是智能菜单http://www.smartmenus.org/。

I hope this is the way out for anyone who want navigation menu with multilevel sub menu.

我希望这是为任何想要导航菜单和多级子菜单的人的出路。

update 2015-02-17 Smart menus are now fully support Bootstrap element style for submenu. For more information please look at Smart menus website.

更新2015-02-17智能菜单现在完全支持子菜单的引导元素样式。如需更多信息,请浏览智能菜单网站。

#4


5  

Shprink's code helped me the most, but to avoid the dropdown to go off-screen i updated it to:

Shprink的代码帮了我最大的忙,但为了避免下拉菜单,我将它更新为:

JS:

JS:

$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
    // Avoid following the href location when clicking
    event.preventDefault(); 
    // Avoid having the menu to close when clicking
    event.stopPropagation(); 
    // If a menu is already open we close it
    $('ul.dropdown-menu [data-toggle=dropdown]').parent().removeClass('open');
    // opening the one you clicked on
    $(this).parent().addClass('open');

    var menu = $(this).parent().find("ul");
    var menupos = $(menu).offset();

    if (menupos.left + menu.width() > $(window).width()) {
        var newpos = -$(menu).width();
        menu.css({ left: newpos });    
    } else {
        var newpos = $(this).parent().width();
        menu.css({ left: newpos });
    }

});

CSS: FROM background-color: #eeeeee TO background-color: #c5c5c5 - white font & light background wasn't looking good.

CSS:从背景色:#eeeeee到背景色:# c5c5c5c5 -白色字体和浅色背景看起来不太好。

.nav .open > a,
.nav .open > a:hover,
.nav .open > a:focus {
  background-color: #c5c5c5;
  border-color: #428bca;
}

I hope this helps people as much as it did for me!

我希望这能像对我一样帮助人们!

But i hope Bootstrap add the subs feature back ASAP.

但是我希望Bootstrap能够尽快地添加这些子特性。

#5


3  

Comment to Skelly's really helpful workaround: in Bootstrap-sass 3.3.6, utilities.scss, line 19: .pull-left has float:left !important. Since that, I recommend to use !important in his CSS as well:

对Skelly真正有用的解决方法:在Bootstrap-sass 3.3.6中,实用工具。scss,第19行:.pull-left有float:left !重要。既然这样,我推荐使用!在他的CSS中也很重要:

.dropdown-submenu.pull-left {
    float:none !important;
}

#6


2  

I bumped with this issue a few days ago. I tried many solutions and none really worked for me on the end i ended up creating an extenion/override of the dropdown code of bootstrap. It is a copy of the original code with changes to the closeMenus function.

几天前我碰到了这个问题。我尝试了许多解决方案,但没有一个真正为我工作,最后我创建了一个extenion/重写了bootstrap的下拉代码。它是对closeMenus函数的更改的原始代码的副本。

I think it is a good solution since it doesn't affects the core classes of bootstrap js.

我认为这是一个很好的解决方案,因为它不会影响bootstrap js的核心类。

You can check it out on gihub: https://github.com/djokodonev/bootstrap-multilevel-dropdown

您可以在gihub上查看它:https://github.com/djokodonev/bootstrap-multilevel-dropdown。

#7


-2  

I make another solution for dropdown. Hope this is helpfull Just add this js script

我为下拉做了另一个解决方案。希望这是helpfull只是添加这个js脚本。

<script type="text/javascript"> jQuery("document").ready(function() {
  jQuery("ul.dropdown-menu > .dropdown.parent").click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    if (jQuery(this).hasClass('open2'))
      jQuery(this).removeClass('open2');
    else {
      jQuery(this).addClass('open2');
    }

  });
}); < /script>

<style type="text/css">.open2{display:block; position:relative;}</style>