简体   繁体   English

多级引导程序下拉菜单出现问题

[英]Problems with multi level bootstrap dropdown

As you can see in my snippet; 如您在我的片段中所见; the dropdowns show vertically downwards. 下拉列表垂直向下显示。 Here are my queries. 这是我的查询。

  1. How do I make my sub menu show to left or right rather than vertically downwards? 如何使子菜单向左或向右而不是垂直向下显示?

  2. As of now this menu is disappear only if I click on the caret sign again. 截至目前,仅当我再次单击插入符号时,此菜单才会消失。 This can be tiresome from UX perspective. 从UX角度来看,这可能很烦人。 So I added the three commented lines that will make it dropdown disappear when clicked anywhere in documented; 因此,我添加了三行注释行,当单击记录中的任何位置时,它将使下拉列表消失。 But on subsequent clicks the dropdown wont work. 但是,在随后的点击中,下拉菜单将不起作用。 I know why it wont work but I cant find a way around. 我知道为什么它不起作用,但是我找不到解决方法。

  3. Also multi level dropdowns are bringing an extra layer of complication ie if it was just one dropdown we can show and hide as we wish. 多级下拉菜单也带来了一层额外的复杂性,即如果只是一个下拉菜单,我们可以根据需要显示和隐藏。 Since there are multiple dropdowns, the code just hides/unhides haphazardly. 由于存在多个下拉列表,因此该代码只是随意地隐藏/取消隐藏。

Please suggest a way forward. 请提出前进的方向。

PS: Kindly provide a solution with bootstrap 3 and not 4. PS:请提供引导程序3而不是4的解决方案。

 $('.dropdown-submenu a.test').on("click", function(e){ $(this).next('ul').toggle(); e.stopPropagation(); e.preventDefault(); }); //document.addEventListener("click", function () { // $('.dropdown-menu:visible').addClass('hide'); // }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <nav class="navbar navbar-default"> <ul class="nav navbar-nav"> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">Dropdown 1 <span class="caret"></span></a> <ul class="dropdown-menu multi-level"> <li><a tabindex="-1" href="/#">Laughing</a></li> <li><a tabindex="-1" href="/#">Out</a></li> <li><a tabindex="-1" href="/#">Loud</a></li> <li class="divider"></li> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">*********<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a tabindex="-1" href="/#">Just</a></li> <li><a tabindex="-1" href="/#/1">Another</a></li> <li><a tabindex="-1" href="/#/1">Sub</a></li> <li><a tabindex="-1" href="/#/1">Menu</a></li> </ul> </ul> </li> </ul> <ul class="nav navbar-nav"> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">Dropdown 2 <span class="caret"></span></a> <ul class="dropdown-menu multi-level"> <li><a tabindex="-1" href="/#">Laughing</a></li> <li><a tabindex="-1" href="/#">Out</a></li> <li><a tabindex="-1" href="/#">Loud</a></li> <li class="divider"></li> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">*********<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a tabindex="-1" href="/#">Just</a></li> <li><a tabindex="-1" href="/#/1">Another</a></li> <li><a tabindex="-1" href="/#/1">Sub</a></li> <li><a tabindex="-1" href="/#/1">Menu</a></li> </ul> </ul> </li> </ul> </nav> 

I modified a few css properties on your dropdown menu, first I set top:0; 我在下拉菜单中修改了一些CSS属性,首先设置top:0; so that the dropdown would appear at the top of the parent element. 这样下拉列表将显示在父元素的顶部。 Then I set left:25%; 然后我离开:25%; this would make the dropdown menu appear a few spaces on the left of the parent element. 这会使下拉菜单在父元素的左侧出现一些空格。

For the subsequent dropdowns, I set left:100%; 对于随后的下拉菜单, 我设置left:100%; so that they would appear on the right of the parent dropdown. 这样它们就会显示在父级下拉列表的右侧。

Instead of adding the class 'hide' everytime you want to close it, modify its css instead; 与其在每次要关闭类时都添加“ hide”类,不如修改其css。

$('.dropdown-menu:visible').css('display','none');

I also added a code wherein it would hide all the other submenus if the clicked element isn't inside a multilevel dropdown. 我还添加了一个代码,如果单击的元素不在多级下拉菜单中,它将隐藏所有其他子菜单。

Run the snippet; 运行代码段;

 $('.dropdown-submenu a.test').on("click", function(e) { $(this).next('ul').toggle(); // the parent dropdown-submenu which is a li tag var clickedDropdown = $(this).parent(); // the parent of the li tag which is a ul tag var parentDropdown = $(this).parent().parent(); if (!parentDropdown.hasClass("multi-level")) { // if the clicked element has a parent dropdown, hide all other submenus $(".dropdown-menu").each(function() { if ($(this).parent()[0] != clickedDropdown[0]) { $(this).css("display", "none"); } }); } e.stopPropagation(); e.preventDefault(); }); document.addEventListener("click", function() { $('.dropdown-menu:visible').css('display', 'none'); }); 
 .dropdown-menu { left: 25% !important; top: 0 !important; } .dropdown-menu .dropdown-menu { left: 100% !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <nav class="navbar navbar-default"> <ul class="nav navbar-nav"> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">Dropdown 1 <span class="caret"></span></a> <ul class="dropdown-menu multi-level"> <li><a tabindex="-1" href="/#">Laughing</a></li> <li><a tabindex="-1" href="/#">Out</a></li> <li><a tabindex="-1" href="/#">Loud</a></li> <li class="divider"></li> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">*********<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a tabindex="-1" href="/#">Just</a></li> <li><a tabindex="-1" href="/#/1">Another</a></li> <li><a tabindex="-1" href="/#/1">Sub</a></li> <li><a tabindex="-1" href="/#/1">Menu</a></li> </ul> </ul> </li> </ul> <ul class="nav navbar-nav"> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">Dropdown 2 <span class="caret"></span></a> <ul class="dropdown-menu multi-level"> <li><a tabindex="-1" href="/#">Laughing</a></li> <li><a tabindex="-1" href="/#">Out</a></li> <li><a tabindex="-1" href="/#">Loud</a></li> <li class="divider"></li> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">*********<span class="caret"></span></a> <ul class="dropdown-menu"> <li><a tabindex="-1" href="/#">Just</a></li> <li><a tabindex="-1" href="/#/1">Another</a></li> <li><a tabindex="-1" href="/#/1">Sub</a></li> <li><a tabindex="-1" href="/#/1">Menu</a></li> </ul> </ul> </li> </ul> </nav> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM