Angular 6 Bootstrap 4中的多层NavBar

时间:2018-11-01 20:21:49

标签: css angular bootstrap-4 navbar

更新:已解决-我不得不在$(document).ready(function())中添加JavaScript,但我缺少了。下面的示例对我来说很完美。)

我正在尝试使多级导航栏与Angular 6和Bootstrap 4配合使用。

如何在单击或悬停时展开和折叠子菜单?我希望菜单扩展到右侧...

例如:我在此链接上找到了一个解决方案-但我不确定如何在Angular组件中注入Javascript。 https://codepen.io/surjithctly/pen/PJqKzQ

这是我的版本详细信息

+-bootstrap@4.1.3

+-jquery@3.3.1

+-popper.js@1.14.4

下面是我的导航栏代码

<nav class="navbar navbar-expand-lg navbar-dark">
    <a class="navbar-brand" href="#" style="color: red;">      
      My NAVBAR
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>        
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownHI" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Menu
                </a>
                <ul class="dropdown-menu" aria-labelledby="navbarDropdownHI">
                    <li><a class="dropdown-item" href="#">Action</a></li>
                  <li class="nav-item dropdown"><a class="dropdown-item dropdown-toggle" href="#">Submenu</a>
                    <ul class="dropdown-menu">
                      <li><a class="dropdown-item" href="#">Submenu action</a></li>
                      <li><a class="dropdown-item" href="#">Another submenu action</a></li>
                    </ul>
                  </li>                
                  <li class="dropdown-divider"></li>
                  <li><a class="dropdown-item" href="#">View All</a></li>
                </ul>
              </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
        </li>
      </ul>     
    </div>
  </nav>

1 个答案:

答案 0 :(得分:1)

您需要添加自定义样式以及以下html

.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;
    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: #ccc;
    margin-top: 5px;
    margin-right: -10px;
}

.dropdown-submenu:hover>a:after {
    border-left-color: #fff;
}

.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;
}
<div class="container">
 <div class="row">
  <div class="dropdown">
   <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Dropdown
    </button>
     <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
       <li class="dropdown-item"><a href="#">Some action</a></li>
       <li class="dropdown-item"><a href="#">Some other action</a></li>
       <li class="dropdown-divider"></li>
       <li class="dropdown-submenu">
        <a  class="dropdown-item" tabindex="-1" href="#">Hover me for more options</a>
         <ul class="dropdown-menu">
           <li class="dropdown-item"><a tabindex="-1" href="#">Second level</a></li>
           <li class="dropdown-submenu">
             <a class="dropdown-item" href="#">Even More..</a>
              <ul class="dropdown-menu">
               <li class="dropdown-item"><a href="#">3rd level</a></li>
               <li class="dropdown-submenu"><a class="dropdown-item" href="#">another level</a>
                 <ul class="dropdown-menu">
                   <li class="dropdown-item"><a href="#">4th level</a></li>
                   <li class="dropdown-item"><a href="#">4th level</a></li>
                   <li class="dropdown-item"><a href="#">4th level</a></li>
                  </ul>
                 </li>
                 <li class="dropdown-item"><a href="#">3rd level</a></li>
                 </ul>
                 </li>
               <li class="dropdown-item"><a href="#">Second level</a></li>
               <li class="dropdown-item"><a href="#">Second level</a></li>
                 </ul>
                </li>
              </ul>
        </div>
    </div>
</div>