悬停时的全角Bootstrap下拉菜单消失

时间:2019-12-06 19:51:26

标签: html css twitter-bootstrap bootstrap-4 dropdown

我有一个全角Bootstrap下拉菜单,该按钮不能正常工作,因为按钮和菜单之间有很多空间。

到目前为止,我只为此使用CSS,没有javascript。这是代码:

HTML:

<nav class="p-4 navbar navbar-expand-lg navbar-light bg-light">
      <a class="navbar-brand" href="#">
        <img src="http://lorempixel.com/30/30/abstract" width="30" height="30" class="d-inline-block align-top" alt="">
        Shopstrap
      </a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
        <div class="collapse navbar-collapse" id="navbarNavDropdown">
            <ul class="navbar-nav">
              <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Features</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Pricing</a>
              </li>
              <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Dropdown link
                  <i class="fas fa-angle-up"></i>
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                  <a class="dropdown-item" href="#">Action</a>
                  <a class="dropdown-item" href="#">Another action</a>
                  <a class="dropdown-item" href="#">Something else here</a>
                </div>
              </li>
               <li class="nav-item dropdown catalog">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Catalog
                  <i class="fas fa-angle-up"></i>
                </a>
                <div class="dropdown-menu catalog-menu" aria-labelledby="navbarDropdownMenuLink">
                  <a class="dropdown-item" href="#">Action</a>
                  <a class="dropdown-item" href="#">Another action</a>
                  <a class="dropdown-item" href="#">Something else here</a>
                </div>
              </li>

            </ul>
          </div>
</nav>

CSS:

.navbar-default .navbar-nav > li.dropdown:hover > a, 
.navbar-default .navbar-nav > li.dropdown:hover > a:hover,
.navbar-default .navbar-nav > li.dropdown:hover > a:focus {
    background-color: rgb(231, 231, 231);
    color: rgb(85, 85, 85);
}

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


.navbar .dropdown.catalog {
    position: static;
    float: none;
}

.navbar .dropdown .dropdown-menu.catalog-menu{
    width: 100%;

}

提琴: https://jsfiddle.net/m4Lxugea/1/

我希望你们中的任何人都能帮助我。我在想也许我不能纯粹是CSS,但是我需要为悬停添加JavaScript。

亚历杭德罗。

2 个答案:

答案 0 :(得分:0)

适合您的关键CSS是:

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

这意味着:当您将鼠标悬停在导航栏(在桌面上)的“目录”链接上时,显示子项.dropdown-menu,当您将鼠标移开时,.dropdown-menu会转到其默认状态为display:none;

使用JS,您可以添加一个类,以确保.dropdown-menu保持打开状态,除非您单击其他位置以撤消此操作

答案 1 :(得分:0)

这是解决方案:Bootstrap为每个下拉菜单类添加margin-top。 设为0px; Bootstrap在navbar类上具有填充,也可以将其覆盖为0px;

这里是有效的示例https://bootstrap-menu.com/detail-megamenu.html

 navbar{ padding-top: 0; padding-bottom: 0; }
 navbar .has-megamenu{position:static!important;}
 navbar .megamenu{left:0; right:0; width:100%; padding:20px;  }
 navbar .nav-link{ padding-top:1rem; padding-bottom:1rem;  }