带有Z-Index和下拉列表的CSS垂直导航问题

时间:2017-05-25 07:40:06

标签: html css twitter-bootstrap angular ng-bootstrap

@import "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"

@import "https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css";

@import "https://daneden.github.io/animate.css/animate.min.css";
  
@media (min-width: 768px) {
  .bootstrap-vertical-nav .collapse {
    display: block;
  }
}

/*-------------------------------*/
/*     Sidebar nav styles        */
/*-------------------------------*/
.sidebar-nav {
  list-style: none;
  margin: 0;
  padding: 0;
  position: absolute;
  top: 0;
  width: 220px;
}
.sidebar-nav li {
  display: inline-block;
  line-height: 20px;
  position: relative;
  width: 100%;
}
/* this background color doesn't trigger */
.sidebar-nav li:before {
  content: '';
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  transition: width 0.2s ease-in;
  width: 3px;
  z-index: 0;
}
.sidebar-nav li:first-child a {
  background-color: #1a1a1a;
  color: 555;
}
.sidebar-nav li:nth-child(2):before {
  background-color: #d12525;
}
.sidebar-nav li:nth-child(3):before {
  background-color: #4c366d;
}
.sidebar-nav li:nth-child(4):before {
  background-color: #583e7e;
}
.sidebar-nav li:nth-child(5):before {
  background-color: #64468f;
}
.sidebar-nav li:nth-child(6):before {
  background-color: #704fa0;
}
.sidebar-nav li:nth-child(7):before {
  background-color: #7c5aae;
}
.sidebar-nav li:nth-child(8):before {
  background-color: #8a6cb6;
}
.sidebar-nav li:nth-child(9):before {
  background-color: #987dbf;
}
.sidebar-nav li:hover:before {
  transition: width 0.2s ease-in;
  width: 100%;
}
.sidebar-nav li a {
  background-color: #1a1a1a;
  color: #fff;
  display: block;
  padding: 10px 15px 10px 30px;
  text-decoration: none;
  z-index: -2;
}
.sidebar-nav li.open:hover before {
  transition: width 0.2s ease-in;
  width: 100%;
}
.sidebar-nav .dropdown-menu {
  background-color: #222222;
  border-radius: 0;
  border: none;
  box-shadow: none;
  margin: 0;
  padding: 0;
  position: relative;
  width: 100%;
}
.sidebar-nav li a:hover,
.sidebar-nav li a:active,
.sidebar-nav li a:focus,
.sidebar-nav li.open a:hover,
.sidebar-nav li.open a:active,
.sidebar-nav li.open a:focus {
  background-color: transparent;
  color: #fff;
  text-decoration: none;
}
.sidebar-nav > .sidebar-brand {
  font-size: 20px;
  height: 65px;
  line-height: 44px;
}
       <div class="collapse" id="collapseExample">
          <ul class="nav flex-column sidebar-nav" id="exCollapsingNavbar3">
            <li class="nav-item sidebar-brand">
              <a nav-link href="#">
                Bootstrap 4
              </a>
            </li>
            <li class="nav-item">
              <a class="nav-link active" href="#"><i class="fa fa-fw fa-home"></i>&nbsp;Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#"><i class="fa fa-fw fa-folder"></i>&nbsp;Page one</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#"><i class="fa fa-fw fa-file-o"></i>&nbsp;Second page</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#"><i class="fa fa-fw fa-cog"></i>&nbsp;Third page</a>
            </li>
            <li class="nav-item dropdown">
              <a class="nav-item" href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-fw fa-plus"></i>&nbsp;Dropdown&nbsp;<span class="caret"></span></a>
              <ul class="dropdown-menu" role="menu">
                <li class="dropdown-header">Dropdown heading</li>
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li><a href="#">Separated link</a></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#"><i class="fa fa-fw fa-bank"></i>&nbsp;Page four</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#"><i class="fa fa-fw fa-bank"></i>&nbsp;Page 5</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#"><i class="fa fa-fw fa-twitter"></i>&nbsp;Last page</a>
            </li>
            <li class="nav-item">
              <a class="nav-link disabled" href="#">Disabled</a>
            </li>

          </ul>
        </div>

问题1)我正在开发一个HTML + CSS sidenav,我在使用Z-Index时遇到了一些问题,如果你将鼠标悬停在侧面导航中的一个项目上,就会有从左到右的颜色过渡。我希望文本保持最佳状态。

问题2)我的下拉列表根本不起作用,我以为我跟着bootstrap tutorial on it

整个项目是一个angular2项目。如果你可以使用ng-bootstrap任何功能,太棒了!如果没有,我感谢任何有关如何解决这两个问题的反馈。

更新解决方案,您需要ng-bootstrap才能使其正常运行。不确定如何将库添加到代码段工具。

@import "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css";
@import "https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css";
@import "https://daneden.github.io/animate.css/animate.min.css";
@media (min-width: 768px) {
  .bootstrap-vertical-nav .collapse {
    display: block;
  }
}


/*-------------------------------*/


/*     Sidebar nav styles        */


/*-------------------------------*/

.sidebar-nav {
  list-style: none;
  margin: 0;
  padding: 0;
  position: absolute;
  top: 0;
  width: 220px;
}

.sidebar-nav li {
  display: inline-block;
  line-height: 20px;
  position: relative;
  width: 100%;
}

.sidebar-nav div {
  display: inline-block;
  line-height: 20px;
  position: relative;
  width: 100%;
}


/* this background color doesn't trigger */

.sidebar-nav li:before {
  content: '';
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  transition: width 0.2s ease-in;
  width: 3px;
  z-index: -2;
}

.sidebar-nav div:before {
  content: '';
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  transition: width 0.2s ease-in;
  width: 3px;
  z-index: -2;
}

.sidebar-nav li:first-child a {
  background-color: #1a1a1a;
  color: 555;
}

.sidebar-nav li:nth-child(n):before {
  z-index: 1;
  background-color: #b7e7ff;
}

.sidebar-nav li:nth-child(1):before {
  background-color: transparent;
}


/*
BLUE: 0085c6 and darker is: 0c78ad
YELLOW: f3c300
BLACK: 000
GREEN: 009f3d
RED: e00024
*/

.sidebar-nav li:nth-child(n) div:before {
  background-color: #0085c6;
  z-index: 1;
}

.sidebar-nav li:hover:before {
  transition: width 0.2s ease-in;
  width: 100%;
  z-index: -1;
}

.sidebar-nav li a {
  background-color: #1a1a1a;
  color: #fff;
  display: block;
  padding: 10px 15px 10px 30px;
  text-decoration: none;
}

.sidebar-nav li.open:hover before {
  transition: width 0.2s ease-in;
  width: 100%;
}

.sidebar-nav .dropdown-menu {
  background-color: #1a1a1a;
  border-radius: 0;
  border: none;
  box-shadow: none;
  margin: 0;
  padding: 0 0 0 15%;
  position: relative;
  width: 100%;
}

.sidebar-nav .dropdown-menu li:nth-child(1):before {
  background-color: #b7e7ff;
}

.sidebar-nav li a:hover,
.sidebar-nav li a:active,
.sidebar-nav li a:focus,
.sidebar-nav li.open a:hover,
.sidebar-nav li.open a:active,
.sidebar-nav li.open a:focus {
  background-color: transparent;
  color: #fff;
  text-decoration: none;
}

.sidebar-nav>.sidebar-brand {
  font-size: 20px;
  height: 65px;
  line-height: 44px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-md-4 col-lg-3">
      <!--<div id="collapseExample" [ngbCollapse]="isCollapsed">
        <div class="card">
          <div class="card-block">
            You can collapse this card by clicking Toggle
          </div>
        </div>
      </div>-->
      <div id="collapseExample" [ngbCollapse]="isCollapsed" class="bootstrap-vertical-nav">
        <div class="collapse" id="collapseExample">
          <ul class="nav flex-column sidebar-nav" id="exCollapsingNavbar3">
            <li class="nav-item sidebar-brand">
              <a nav-link href="#">
                Bootstrap 4
              </a>
            </li>
            <li class="nav-item">
              <div>
                <a class="nav-link active" href="#"><i class="fa fa-fw fa-home"></i>&nbsp;Home</a>
              </div>
            </li>
            <li class="nav-item">
              <div>
                <a class="nav-link" href="#"><i class="fa fa-fw fa-folder"></i>&nbsp;Page one</a>
              </div>
            </li>
            <li class="nav-item">
              <div>
                <a class="nav-link" href="#"><i class="fa fa-fw fa-file-o"></i>&nbsp;Second page</a>
              </div>
            </li>
            <li class="nav-item">
              <div>
                <a class="nav-link" href="#"><i class="fa fa-fw fa-cog"></i>&nbsp;Third page</a>
              </div>
            </li>
            <li class="nav-item dropdown">
              <div ngbDropdown>
                <a class="nav-item" href="#" class="dropdown-toggle" data-toggle="dropdown">
                  <i class="fa fa-fw fa-plus"></i>&nbsp;Dropdown&nbsp;<span class="caret"></span>
                </a>
                <ul class="dropdown-menu" role="menu">
                  <li><a href="#">a</a></li>
                  <li><a href="#">b</a></li>
                  <li><a href="#">c</a></li>
                  <li><a href="#">d</a></li>
                  <li><a href="#">e</a></li>
                </ul>
              </div>
            </li>
            <li class="nav-item">
              <div>
                <a class="nav-link disabled" href="#">Disabled</a>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="col-md-8 col-lg-9">
      <p>
        <button type="button" class="btn btn-outline-primary" (click)="isCollapsed = !isCollapsed" [attr.aria-expanded]="!isCollapsed" aria-controls="collapseExample">
          Toggle
        </button>
      </p>
      <hr />
      <div>
        <h1>Right Pane - Bootstrap 4.0 Vertical Nav Example</h1>
      </div>
    </div>
  </div>
</div>

3 个答案:

答案 0 :(得分:2)

问题是你不能使用那个li:之前既有左侧彩色条也有颜色过渡,但仍然有文字在上面。

我建议在列表项中添加span元素,该跨度应代表应始终存在的彩色条:

<li class="nav-item">
   <span class="before-bar"></span>
   <a class="nav-link active" href="#"><i class="fa fa-fw fa-home"></i>&nbsp;Home</a>
</li>

.before-bar {
    position: absolute;
    height: 100%;
    width: 3px;
}

.sidebar-nav li:nth-child(2):before, 
.sidebar-nav li:nth-child(2) .before-bar {
    background-color: #d12525;
}

请参阅http://plnkr.co/edit/AHaTOQmBwllZV5bATFtJ?p=preview(仅针对&#34; Home&#34;链接进行了修改)

可能更好的解决方案是保持li:之前为3px颜色条,并且具有颜色过渡的跨度(具有较低的z-index)。

答案 1 :(得分:0)

要将文本保持在最顶层,请更改li:之前的z-index

.sidebar-nav li:before {
  content: '';
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  transition: width 0.2s ease-in;
  width: 3px;
  z-index: -1;
}

答案 2 :(得分:0)

问题1)我正在开发一个HTML + CSS sidenav,我在使用Z-Index时遇到了一些问题,如果你将鼠标悬停在侧面导航中的一个项目上,就会有从左到右的颜色过渡。我希望文本保持最佳状态。

.sidebar-nav li a{
position:relative;
z-index:0;
}

问题2)我的下拉列表根本不起作用,我以为我按照它上面的bootstrap教程。

整个项目是一个angular2项目。如果你可以使用ng-bootstrap任何功能,太棒了!如果没有,我感谢任何有关如何解决这两个问题的反馈。

  

尝试添加bootstrape.min.js