CSS 動態導航選單

antzone發表於2020-02-19

本章節分享一段程式碼例項,它利用CSS3實現了動態導航選單效果。

最終生成了一個環形導航選單,更多導航選單效果可以參閱特效程式碼導航選單分類。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
* {
  margin: 0;
  padding: 0;
}
li, ul {
  list-style: none;
}
.fir_li {
  width: 80px;
  height: 40px;
  background: #000;
  color: #fff;
  margin: 5px;
  text-align: center;
  line-height: 40px;
  border-radius: 5px;
  position: absolute;
  left: 300px;
  top: 300px;
}
.sec_ul {
  width: auto;
  height: auto;
}
.sec_li {
  width: 80px;
  height: 40px;
  position: absolute;
  background: #000;
  color: #fff;
  text-align: center;
  line-height: 40px;
  border-radius: 5px;
  left: -1000px;
  top: -10000px;
}
.fir_li:hover .sec_ul > li:nth-child(1) {
  animation: secLi1 5s;
  animation-delay: 0.5s;
  animation-fill-mode: forwards;
}
.fir_li:hover .sec_ul > li:nth-child(2) {
  animation: secLi2 5s;
  animation-delay: 1s;
  animation-fill-mode: forwards;
}
.fir_li:hover .sec_ul > li:nth-child(3) {
  animation: secLi3 5s;
  animation-delay: 1.5s;
  animation-fill-mode: forwards;
}
.fir_li:hover .sec_ul > li:nth-child(4) {
  animation: secLi4 5s;
  animation-delay: 2s;
  animation-fill-mode: forwards;
}
.fir_li:hover .sec_ul > li:nth-child(5) {
  animation: secLi5 5s;
  animation-delay: 2.5s;
  animation-fill-mode: forwards;
}
@keyframes secLi1 {
  0% {
    left: 0;
    top: 0;
  }
  50% {
    left: 0px;
    top: 200px;
    transform: rotate(0deg);
    transform-origin: center -200px;
  }
  90% {
    left: 0px;
    top: 200px;
    transform: rotate(-180deg);
    transform-origin: center -200px;
  }
  100% {
    left: 0px;
    top: 200px;
    transform: rotate(0deg);
  }
}
@keyframes secLi2 {
  0% {
    left: 0;
    top: 0;
  }
  50% {
    left: 0px;
    top: 200px;
    transform: rotate(0deg);
    transform-origin: center -200px;
  }
  90% {
    transform: rotate(-180deg);
    transform-origin: center -200px;
  }
  100% {
    left: 140px;
    top: 140px;
    transform: rotate(0deg);
  }
}
@keyframes secLi3 {
  0% {
    left: 0;
    top: 0;
  }
  50% {
    left: 0px;
    top: 200px;
    transform: rotate(0deg);
    transform-origin: center -200px;
  }
  90% {
    transform: rotate(-180deg);
    transform-origin: center -200px;
  }
  100% {
    left: 200px;
    top: 0px;
    transform: rotate(0deg);
  }
}
@keyframes secLi4 {
  0% {
    left: 0;
    top: 0;
  }
  50% {
    left: 0px;
    top: 200px;
    transform: rotate(0deg);
    transform-origin: center -200px;
  }
  90% {
    transform: rotate(-180deg);
    transform-origin: center -200px;
  }
  100% {
    left: 140px;
    top: -140px;
    transform: rotate(0deg);
  }
}
@keyframes secLi5 {
  0% {
    left: 0;
    top: 0;
  }
  50% {
    left: 0px;
    top: 200px;
    transform: rotate(0deg);
    transform-origin: center -200px;
  }
  90% {
    transform: rotate(-180deg);
    transform-origin: center -200px;
  }
  100% {
    left: 0px;
    top: -200px;
    transform: rotate(0deg);
  }
}
</style>
</head>
<body>
  <ul id="fir_ul">
    <li class="fir_li">
      選單
      <ul class="sec_ul">
        <li class="sec_li">螞蟻部落一</li>
        <li class="sec_li">螞蟻部落二</li>
        <li class="sec_li">螞蟻部落三</li>
        <li class="sec_li">螞蟻部落四</li>
        <li class="sec_li">螞蟻部落五</li>
      </ul>
    </li>
  </ul>
</body>
</html>

上述程式碼實現了比較好的動態導航效果,可以借鑑參考。

相關文章