css3滑鼠懸浮背景滑動導航選單

admin發表於2017-02-22

分享一段程式碼例項,它實現了滑鼠懸浮導航選單背景滑動效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
* {
  padding: 0;
  margin: 0;
}
#nav {
  margin-top: 40px;
  width: 800px;
  position: relative;
}
#nav a {
  color: #666;
  text-decoration: none;
  display: block;
  float: left;
  width: 100px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
  font-family: "微軟雅黑";
  position: relative;
  -webkit-transition: all .5s ease;
  -moz-transition: all .5s ease;
  -o-transition: all .5s ease;
  transition: all .5s ease;
}
.active {
  width: 100px;
  height: 40px;
  background: #ffb61a;
  position: absolute;
  left: 0px;
  top: 0px;
  z-index: 0;
  -webkit-transition: all .5s ease;
  -moz-transition: all .5s ease;
  -o-transition: all .5s ease;
  transition: all .5s ease;
  z-index: -1;
}
#nav a:nth-of-type(1):hover ~ .active {
  left: 0px;
}
#nav a:nth-of-type(2):hover ~ .active {
  left: 100px;
}
#nav a:nth-of-type(3):hover ~ .active {
  left: 200px;
}
#nav a:nth-of-type(4):hover ~ .active {
  left: 300px;
}
#nav a:nth-of-type(5):hover ~ .active {
  left: 400px;
}
#nav a:nth-of-type(6):hover ~ .active {
  left: 500px;
}
#nav a:nth-of-type(1):hover, #nav > a:nth-of-type(2):hover, 
#nav > a:nth-of-type(3):hover, #nav > a:nth-of-type(4):hover, 
#nav > a:nth-of-type(5):hover, #nav > a:nth-of-type(6):hover {
  color: #fff;
}
</style>
</head>
<body>
  <div id="nav">
    <a href="javascript:;">首頁</a>
    <a href="javascript:;">螞蟻部落一</a>
    <a href="javascript:;">螞蟻部落二</a>
    <a href="javascript:;">螞蟻部落三</a>
    <a href="javascript:;">螞蟻部落四</a>
    <a href="javascript:;">螞蟻部落五</a>
    <div class="active"></div>
  </div>
</body>
</html>

相關文章