如何使导航菜单动态化?

时间:2016-02-05 13:59:41

标签: jquery html css

如何使以下导航动态化,因此如果我添加另一个导航元素,它不会中断。

nav {
  position: relative;
  height: 57px;
  line-height: 57px;
  width: 100%;
  background-color: #fff;
  font-weight: 500;
  text-transform: uppercase;
  font-size: 14px;
  overflow: hidden;
}
nav ul {
  position: relative;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: -moz-flex;
  display: -ms-flex;
  display: flex;
  margin: 0 auto;
  padding: 0;
  list-style: none;
  -ms-box-orient: horizontal;
  -ms-box-pack: center;
  -webkit-flex-flow: row wrap;
  -moz-flex-flow: row wrap;
  -ms-flex-flow: row wrap;
  flex-flow: row wrap;
  -webkit-justify-content: center;
  -moz-justify-content: center;
  -ms-justify-content: center;
  justify-content: center;
  text-align: center;
  width: 100%;
  height: 100%;
}
nav ul li {
  position: relative;
  margin: 0;
  text-align: center;
  -webkit-flex: 1;
  -moz-flex: 1;
  -ms-flex: 1;
  flex: 1;
  height: 100%;
}
nav li:last-child::before {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 8%;
  content: '';
  background-color: #9c27b0;
  transition: transform 0.3s;
}
nav li:first-child:active ~ li:last-child::before {
  transform: translate3d(-200%, 0, 0);
}
nav li:nth-child(2):active ~ li:last-child::before {
  transform: translate3d(-100%, 0, 0);
}
nav li:nth-child(3):active ~ li:last-child::before {
  transform: translate3d(0, 0, 0);
}
<nav>
  <ul>
    <li><a href="#tab1"><span class="title">TAB 1</span></a>
    </li>
    <li><a href="#tab2"><span class="title">TAB 2</span></a>
    </li>
    <li><a href="#tab3"><span class="title">TAB 3</span></a>
    </li>
  </ul>
</nav>

在CSS结束时,我有:

nav li:first-child.navigation-current ~ li:last-child::before {
  transform: translate3d(-200%, 0, 0);
}

nav li:nth-child(2).navigation-current ~ li:last-child::before {
  transform: translate3d(-100%, 0, 0);
}

nav li:nth-child(3).navigation-current ~ li:last-child::before {
  transform: translate3d(0, 0, 0);
}

所以,如果我添加了第四个标签,它必须看起来像这样吗?

nav li:first-child.navigation-current ~ li:last-child::before {
  transform: translate3d(-300%, 0, 0);
}

nav li:nth-child(2).navigation-current ~ li:last-child::before {
  transform: translate3d(-200%, 0, 0);
}

nav li:nth-child(3).navigation-current ~ li:last-child::before {
  transform: translate3d(-100%, 0, 0);
}

nav li:nth-child(4).navigation-current ~ li:last-child::before {
  transform: translate3d(0, 0, 0);
}

有没有办法让CSS不必更改以添加或删除标签?

(也可用a jsfiddle

0 个答案:

没有答案