滑动单级菜单

Menu is one of the important components of any site. Today it is difficult to find a website without the navigation menu. Today we have prepared a modern looking single-level menu. You might think – again, but the menu looks really interesting, its feature is a sliding panel which highlights the active elements (something like a lavalamp menu). Previously, we have already seen the creation of lavalamp menu (one of our tutorials), but it had a slight drawback – it was not possible to set the active element. We have overcome this obstacle, and in today’s menu, you can initially indicate active elements (as it is seen in the demo). Now we can start looking at the order of its creation.

菜单是任何站点的重要组成部分之一。 今天,没有导航菜单很难找到一个网站。 今天,我们准备了外观现代的单层菜单。 您可能会再想一想,但是菜单看起来确实很有趣,它的功能是滑动面板,突出显示了活动元素(类似于lavalamp菜单)。 以前,我们已经看到了lavalamp菜单的创建(我们的教程之一),但是它有一个小缺点-无法设置活动元素。 我们已经克服了这一障碍,在今天的菜单中,您可以最初指示活动元素(如演示中所示)。 现在,我们可以开始查看其创建顺序。

现场演示

HTML (HTML)

We did not invent a special html menu structure, as usual, we decided to use the usual UL-LI elements to build the menu. Here is the html of our menu:

像往常一样,我们没有发明特殊的html菜单结构,我们决定使用通常的UL-LI元素来构建菜单。 这是菜单的html:


<nav id="menu">
  <ul>
    <li><a href="">HTML/CSS</a></li>
    <li><a href="">HTML5</a></li>
    <li><a href="">jQuery</a></li>
    <li><a href="">PHP</a></li>
    <li><a href="">Javascript</a></li>
    <li><a href="">Design</a></li>
    <li><a href="">Other</a></li>
    <div class="current">
      <div class="ctoparr"></div>
      <div class="cback"></div>
      <div class="cbotarr"></div>
    </div>
  </ul>
</nav>

<nav id="menu">
  <ul>
    <li><a href="">HTML/CSS</a></li>
    <li><a href="">HTML5</a></li>
    <li><a href="">jQuery</a></li>
    <li><a href="">PHP</a></li>
    <li><a href="">Javascript</a></li>
    <li><a href="">Design</a></li>
    <li><a href="">Other</a></li>
    <div class="current">
      <div class="ctoparr"></div>
      <div class="cback"></div>
      <div class="cbotarr"></div>
    </div>
  </ul>
</nav>

As I noted before, nowe we have the opportunity to set active elements – simply add ‘selected’ class for a necessary LI element, example:

如前所述,现在我们有机会设置活动元素–只需为必要的LI元素添加“ selected”类,例如:


<nav id="menu">
  <ul>
    <li><a href="">HTML/CSS</a></li>
    <li><a href="">HTML5</a></li>
    <li><a href="">jQuery</a></li>
    <li><a href="">PHP</a></li>
    <li class="selected"><a href="">Javascript</a></li>
    <li><a href="">Design</a></li>
    <li><a href="">Other</a></li>
    <div class="current">
      <div class="ctoparr"></div>
      <div class="cback"></div>
      <div class="cbotarr"></div>
    </div>
  </ul>
</nav>

<nav id="menu">
  <ul>
    <li><a href="">HTML/CSS</a></li>
    <li><a href="">HTML5</a></li>
    <li><a href="">jQuery</a></li>
    <li><a href="">PHP</a></li>
    <li class="selected"><a href="">Javascript</a></li>
    <li><a href="">Design</a></li>
    <li><a href="">Other</a></li>
    <div class="current">
      <div class="ctoparr"></div>
      <div class="cback"></div>
      <div class="cbotarr"></div>
    </div>
  </ul>
</nav>

Besides of UL-LI elements, you can notice another added element – DIV with three children:

除了UL-LI元素之外,您还可以注意到另一个添加的元素– DIV包含三个子元素:


<div class="current">
  <div class="ctoparr"></div>
  <div class="cback"></div>
  <div class="cbotarr"></div>
</div>

<div class="current">
  <div class="ctoparr"></div>
  <div class="cback"></div>
  <div class="cbotarr"></div>
</div>

This is the sliding element that moves behind active (and hover elements). Now we can start styling our menu.

这是在活动(和悬停元素)后面移动的滑动元素。 现在,我们可以开始设计菜单样式。

CSS (CSS)

The menu is aligned to center, with 90% in width and 128px in height.

菜单对齐中心,宽度为90%,高度为128px。


#menu {
  display: inline-block;
  height: 128px;
  margin: 100px 5% 0;
  text-align: center;
  white-space: nowrap;
  width: 90%;
}
#menu ul {
  margin: 0;
  padding: 0;
  position: relative;
}
#menu ul:after {
  clear: both;
  content: "";
  display: block;
}

#menu {
  display: inline-block;
  height: 128px;
  margin: 100px 5% 0;
  text-align: center;
  white-space: nowrap;
  width: 90%;
}
#menu ul {
  margin: 0;
  padding: 0;
  position: relative;
}
#menu ul:after {
  clear: both;
  content: "";
  display: block;
}

Menu elements are aligned from left to right, all elements are of the same width, with different icons at background:

菜单元素从左到右对齐,所有元素的宽度相同,背景不同。


#menu li {
  background-position: 50% center;
  display: block;
  float: left;
  font-size: 18px;
  font-weight: bold;
  height: 128px;
  line-height: 210px;
  margin-right: 1%;
  position: relative;
  white-space: nowrap;
  width: 13%;
  z-index: 2;
}
#menu li:after {
  background: url("../images/bg.png") repeat scroll 0 0;
  content: "";
  height: 100%;
  position: absolute;
  right: -10%;
  top: 0;
  width: 10%;
}
#menu li:nth-child(1):before {
  background: url("../images/bg.png") repeat scroll 0 0;
  content: "";
  height: 100%;
  left: -10%;
  position: absolute;
  top: 0;
  width: 10%;
}
#menu li:nth-child(1) {
  background: url("../images/1.png") no-repeat center;
  margin-left: 1%;
}
#menu li:nth-child(2) {
  background: url("../images/2.png") no-repeat center;
}
#menu li:nth-child(3) {
  background: url("../images/3.png") no-repeat center;
}
#menu li:nth-child(4) {
  background: url("../images/4.png") no-repeat center;
}
#menu li:nth-child(5) {
  background: url("../images/5.png") no-repeat center;
}
#menu li:nth-child(6) {
  background: url("../images/1.png") no-repeat center;
}
#menu li:nth-child(7) {
  background: url("../images/2.png") no-repeat center;
}
#menu a {
  color: #eee;
  display: block;
  height: 100%;
  text-decoration: none;
}

#menu li {
  background-position: 50% center;
  display: block;
  float: left;
  font-size: 18px;
  font-weight: bold;
  height: 128px;
  line-height: 210px;
  margin-right: 1%;
  position: relative;
  white-space: nowrap;
  width: 13%;
  z-index: 2;
}
#menu li:after {
  background: url("../images/bg.png") repeat scroll 0 0;
  content: "";
  height: 100%;
  position: absolute;
  right: -10%;
  top: 0;
  width: 10%;
}
#menu li:nth-child(1):before {
  background: url("../images/bg.png") repeat scroll 0 0;
  content: "";
  height: 100%;
  left: -10%;
  position: absolute;
  top: 0;
  width: 10%;
}
#menu li:nth-child(1) {
  background: url("../images/1.png") no-repeat center;
  margin-left: 1%;
}
#menu li:nth-child(2) {
  background: url("../images/2.png") no-repeat center;
}
#menu li:nth-child(3) {
  background: url("../images/3.png") no-repeat center;
}
#menu li:nth-child(4) {
  background: url("../images/4.png") no-repeat center;
}
#menu li:nth-child(5) {
  background: url("../images/5.png") no-repeat center;
}
#menu li:nth-child(6) {
  background: url("../images/1.png") no-repeat center;
}
#menu li:nth-child(7) {
  background: url("../images/2.png") no-repeat center;
}
#menu a {
  color: #eee;
  display: block;
  height: 100%;
  text-decoration: none;
}

The slider is a small element with smooth transition effect (to move it). It contains of the background and two additional elements (at top and bottom) – little triangles with additional shadows:

滑块是一个具有平滑过渡效果(移动它)的小元素。 它包含背景和两个附加元素(在顶部和底部)–带有附加阴影的小三角形:


.current{
  height: 158px;
  left: 7.5%;
  margin-left: -50px;
  position: absolute;
  top: -13px;
  width: 100px;
  -webkit-transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
  -moz-transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
  -o-transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
  -ms-transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
  transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
}
.cback {
  background-color: #aadd33;
  border-bottom: 2px solid rgba(0, 0, 0, 0.5);
  border-radius: 10px;
  height: 100%;
  position: absolute;
  width: 100%;
}
.ctoparr {
  height: 12px;
  left: 0;
  overflow: hidden;
  position: absolute;
  top: 13px;
  width: 100%;
  z-index: 2;
}
.ctoparr:before {
  border-radius: 20%;
  box-shadow: 0 0 15px #000;
  content: "";
  height: 10px;
  left: 5%;
  position: absolute;
  top: -10px;
  width: 90%;
}
.ctoparr:after{
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 12px solid #aadd33;
  content: "";
  height: 0;
  left: 50%;
  margin-left: -8px;
  position: absolute;
  top: 0;
  width: 0;
}
.cbotarr {
  bottom: 17px;
  height: 12px;
  left: 0;
  overflow: hidden;
  position: absolute;
  width: 100%;
  z-index: 2;
}
.cbotarr:before {
  border-radius: 20%;
  bottom: -10px;
  box-shadow: 0 0 15px #000;
  content: "";
  height: 10px;
  left: 5%;
  position: absolute;
  width: 90%;
}
.cbotarr:after {
  border-bottom: 12px solid #aadd33;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  bottom: 0;
  content: "";
  height: 0;
  left: 50%;
  margin-left: -8px;
  position: absolute;
  width: 0;
}

.current{
  height: 158px;
  left: 7.5%;
  margin-left: -50px;
  position: absolute;
  top: -13px;
  width: 100px;
  -webkit-transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
  -moz-transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
  -o-transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
  -ms-transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
  transition: all 400ms cubic-bezier(0, 1.1, 0.5, 1.1);
}
.cback {
  background-color: #aadd33;
  border-bottom: 2px solid rgba(0, 0, 0, 0.5);
  border-radius: 10px;
  height: 100%;
  position: absolute;
  width: 100%;
}
.ctoparr {
  height: 12px;
  left: 0;
  overflow: hidden;
  position: absolute;
  top: 13px;
  width: 100%;
  z-index: 2;
}
.ctoparr:before {
  border-radius: 20%;
  box-shadow: 0 0 15px #000;
  content: "";
  height: 10px;
  left: 5%;
  position: absolute;
  top: -10px;
  width: 90%;
}
.ctoparr:after{
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 12px solid #aadd33;
  content: "";
  height: 0;
  left: 50%;
  margin-left: -8px;
  position: absolute;
  top: 0;
  width: 0;
}
.cbotarr {
  bottom: 17px;
  height: 12px;
  left: 0;
  overflow: hidden;
  position: absolute;
  width: 100%;
  z-index: 2;
}
.cbotarr:before {
  border-radius: 20%;
  bottom: -10px;
  box-shadow: 0 0 15px #000;
  content: "";
  height: 10px;
  left: 5%;
  position: absolute;
  width: 90%;
}
.cbotarr:after {
  border-bottom: 12px solid #aadd33;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  bottom: 0;
  content: "";
  height: 0;
  left: 50%;
  margin-left: -8px;
  position: absolute;
  width: 0;
}

Finally, in order to move the slider like in the lavalamp menu, we need to add the following styles:

最后,为了像lavalamp菜单中一样移动滑块,我们需要添加以下样式:


#menu li.selected:nth-child(1) ~ .current {
  left: 7.5%;
}
#menu li.selected:nth-child(2) ~ .current {
  left: 21.5%;
}
#menu li.selected:nth-child(3) ~ .current {
  left: 35.5%;
}
#menu li.selected:nth-child(4) ~ .current {
  left: 49.5%;
}
#menu li.selected:nth-child(5) ~ .current {
  left: 63.5%;
}
#menu li.selected:nth-child(6) ~ .current {
  left: 77.5%;
}
#menu li.selected:nth-child(7) ~ .current {
  left: 91.5%;
}
#menu li:nth-child(1):hover ~ .current {
  left: 7.5%;
}
#menu li:nth-child(2):hover ~ .current {
  left: 21.5%;
}
#menu li:nth-child(3):hover ~ .current {
  left: 35.5%;
}
#menu li:nth-child(4):hover ~ .current {
  left: 49.5%;
}
#menu li:nth-child(5):hover ~ .current {
  left: 63.5%;
}
#menu li:nth-child(6):hover ~ .current {
  left: 77.5%;
}
#menu li:nth-child(7):hover ~ .current {
  left: 91.5%;
}

#menu li.selected:nth-child(1) ~ .current {
  left: 7.5%;
}
#menu li.selected:nth-child(2) ~ .current {
  left: 21.5%;
}
#menu li.selected:nth-child(3) ~ .current {
  left: 35.5%;
}
#menu li.selected:nth-child(4) ~ .current {
  left: 49.5%;
}
#menu li.selected:nth-child(5) ~ .current {
  left: 63.5%;
}
#menu li.selected:nth-child(6) ~ .current {
  left: 77.5%;
}
#menu li.selected:nth-child(7) ~ .current {
  left: 91.5%;
}
#menu li:nth-child(1):hover ~ .current {
  left: 7.5%;
}
#menu li:nth-child(2):hover ~ .current {
  left: 21.5%;
}
#menu li:nth-child(3):hover ~ .current {
  left: 35.5%;
}
#menu li:nth-child(4):hover ~ .current {
  left: 49.5%;
}
#menu li:nth-child(5):hover ~ .current {
  left: 63.5%;
}
#menu li:nth-child(6):hover ~ .current {
  left: 77.5%;
}
#menu li:nth-child(7):hover ~ .current {
  left: 91.5%;
}

Thus, as you can see, when we hover our child elements, it moves the slider toward the hover element position (in it’s middle). Besides of that, it will keep the slider at the position of pre-selected element (.selected class). That’s all. We have just completed creating our new menu with the slider.

因此,如您所见,当我们将子元素悬停时,它会将滑块移向悬停元素位置(在中间)。 除此之外,它将使滑块保持在预选元素(.selected类)的位置。 就这样。 我们刚刚完成了使用滑块创建新菜单的操作。

Come back to our website next time for new tutorials

下次返回我们的网站以获取新教程

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

翻译自: https://www.script-tutorials.com/sliding-single-level-menu/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值