css滑鼠懸浮下拉選單效果

admin發表於2018-08-13

分享一段程式碼例項,它實現了滑鼠懸浮出現二級下拉選單效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
* {
  margin: 0;
  padding: 0;
}
.menu-bar {
  margin:100px;
}
.menu-bar ul {
  list-style-type:none;
}
.menu-bar a {
  text-decoration: none;
  color: black;
  font-size: 12px;
  font-family: "宋體";
}
.btn {
  width: 50px;
  height: 28px;
  line-height: 28px;
  text-align: center;
  border: 1px solid #ddd;
  background-color: #eee;
  display: block;
}
.menu {
  width: 82px;
  height: 125px;
  padding-top: 1px;
  display: none;
}
.btn:hover + .menu, .menu:hover {
  display: block;
}
/* 顯示下拉選單 */
.menu-item {
  width: 80px;
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  background-color: white;
  border-bottom: 1px solid #ddd;
  border-right: 1px solid #ddd;
  border-left: 1px solid #ddd;
  cursor: pointer;
}
.menu-item:first-child {
  border-top: 1px solid #ddd;
}
.menu-item:hover {
  background-color: #ddd;
}
</style>
</head>
<body>
<div class="menu-bar">
  <a class="btn" href="#">螞蟻部落</a>
  <ul class="menu">
    <li class="menu-item">css教程</li>
    <li class="menu-item">div教程</li>
    <li class="menu-item">js教程</li>
    <li class="menu-item">softwhy.com</li>
  </ul>
</div>
</body>
</html>

相關文章