一側具有滑鼠跟隨效果的垂直導航選單

antzone發表於2017-04-10

本章節分享一段程式碼例項,它實現了垂直導航選單效果。

當滑鼠懸浮於導航欄目的時候,在左側會有動畫方式跟隨的豎條,這也是常見的特效。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
html {
  font-size: 100%;
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
  *overflow: auto;
  _overflow-x: hidden;
}
body {
  margin: 0;
}
p, ul, ol, form, dl, h1, h2, h3, h4, h5, h6 {
  margin-top: 0;
  margin-bottom: 0;
}
dl, dd {
  margin-left: 0;
}
ul, ol {
  padding-left: 0;
  *margin-left: 0;
  list-style-type: none;
}
ul li {
  *zoom: 1;
  *vertical-align: bottom;
}
em, var, cite, i {
  font-style: normal;
}
small {
  font-size: inherit;
}
s, a {
  text-decoration: none;
}
:focus {
  outline: none;
}
blockquote, q {
  quotes: none;
  margin: 0;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
  empty-cells: show; /*table-layout:fixed;*/
}
img, iframe {
  border: none;
}
img {
  vertical-align: top;
  -ms-interpolation-mode: bicubic; /*ie7防止圖片縮小失真的*/
}
/*---------豎向選單(非必需)---------*/
.bl-vernav li {
  border-bottom: 1px solid #ddd;
  margin-bottom: -1px;
  padding-top: 1px;
}
.bl-vernav a {
  display: block;
  height: 20px;
  line-height: 20px;
  padding: 10px 16px;
}
.bl-vernav a:hover {
  background: #F8F8F8;
  text-decoration: none;
}
/*.bl-vernav .cur a{ background:#428BCA; color:#fff;}*/
.bl-vernav-ord {
  border: 1px solid #ddd;
}
.vernav-level li li {
  border-left: none;
  border-right: none;
}
.vernav-level li li a {
  padding-left: 40px;
}
.vernav-level .cur .one {
  background: #F8F8F8;
}
.vernav-level li .cur a {
  background: #F8F8F8;
}
/*diy*/
.bl-vernav-wrap {
  position: relative;
}
.bl-vernav-wrap .sideLine {
  position: absolute;
  left: -6px;
  top: 0;
  z-index: 1;
  line-height: 0;
  font-size: 0;
  border-left: #FF5F3E solid 7px;
  background-color: #F8F8F8;
}
.bl-vernav {
  position: relative;
  z-index: 2;
}
.bl-vernav a:hover {
  background-color: transparent;
}
.demobox {
  width: 460px;
  font-size: 14px;
  color: #333;
}
a {
  color: #333;
  text-decoration: none;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
$(function () {
  var verNav = $(".bl-vernav"),  //第一層ul.
      line = verNav.siblings(".sideLine"), //左邊條紋線
      verNavFirst = verNav.children("li:first"),  //第一層ul中第一個li
      curY = verNav.children("li.cur").position().top;
 
  //設定輔助線初始化高度和寬度,也可以直接用css設定好,這裡就不用js計算了
  line.height(verNavFirst.outerHeight() - 1).width(verNavFirst.outerWidth());
 
  verNav.find("li").mouseenter(function () {
    var thisY = $(this).position().top;
    line.stop(true, true).animate({ top: thisY }, 200);
    return false
  }).end().mouseleave(function () {
    line.stop(true, true).animate({ top: curY }, 300)
  }).trigger("mouseleave");
});
</script>
</head>
 
<body style="padding:50px;">
<div class="demobox">
  <h1 class="h3">左邊條紋跟隨滑鼠滑動導航</h1><br />
  <div class="bl-vernav-wrap">
    <div class="sideLine"></div>
    <ul class="bl-vernav vernav-level">
      <li><a href="#" class="one">導航選單</a></li>
      <li class="cur">
        <a href="#" class="one">前端專區</a>
        <ul>
          <li><a href="#">div教程</a></li>
          <li><a href="#">css教程</a></li>
          <li><a href="#">js教程</a></li>
        </ul>
      </li>
      <li><a href="#" class="one">螞蟻部落一</a></li>
      <li><a href="#" class="one">螞蟻部落二</a></li>
      <li><a href="#" class="one">螞蟻部落三</a></li>
    </ul>
  </div>
</div>
</body>
</html>

相關文章