分享几种CSS制作菜单列表的方法

html-css018

分享几种CSS制作菜单列表的方法,第1张

<!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Document</title> 6     <style> 7         ul{ 8             list-style: none 9         }10         .nav>li{11             float: left12         }13         ul a{14             display: block15             text-decoration: none16             width: 100px17             height: 50px18             text-align: center19             line-height: 50px20             color: white21             background-color: #2f3e4522         }23         .nav>li:first-child a{24             border-radius: 10px 0 0 10px25         }26         .nav>li:last-child a{27             border-radius: 0 10px 10px 028         }29         .drop-down{30             /*position: relative*/31         }32         .drop-down-content{33             padding: 034             display: none35             /*position: absolute*/36         }37         38         h3{39             font-size: 30px40             clear: both41         }42         .drop-down-content li:hover a{43             background-color:red44         }45         .nav .drop-down:hover .drop-down-content{46             display: block47         }48 </style>49 </head>50 <body>51     <ul class="nav">52         <li><a href="#">下拉菜单</a></li>53         <li class="drop-down"><a href="#">下拉菜单</a>54             <ul class="drop-down-content">55                 <li><a href="#">我是1</a></li>56                 <li><a href="#">我是2</a></li>57                 <li><a href="#">我是3</a></li>58             </ul>59             </li>60         <li><a href="#">下拉菜单</a></li>61         <li><a href="#">下拉菜单</a></li>62         <li><a href="#">下拉菜单</a></li>63     </ul>64     <h3>我是测试文字</h3>65 </body>66 </html>

实现标题栏鼠标滑过点击过后保持原色,不用像你那样写得这么复杂,按照你说的,只需要写一次样式就够了,写法如下: #dh ul li a { display:blockbackground:#006eadwidth:125pxheight:43px} 现在我无论是在默认状态下、鼠标移上去时、鼠标点击时,或是点击过后,a标签背景颜色都会保持在初始状态,不会改变。 如果需要鼠标移上去时,背景变色,那就加多一条a:hover,写法如下: #dh ul li a:hover{ display:blockbackground:#0193dewidth:125pxheight:43px} 现在就只有鼠标移上去时背景会变色,其他情况颜色都不会变化。 提示:如果需要文字一直保持原色,只需要在第一条#dh ul li a 的代码里加上一条“color:颜色属性”即可。例如:#dh ul li a {background:#006eadcolor:#fff} 试一试吧!

单纯用css肯定不能实现这种效果,需要用到jQuery,代码如下:

然后再用css去修改样式就好。

<!DOCTYPE html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />

<title>jquery下拉菜单</title>

<script type="text/javascript" src="jquery-1.6.2.min.js"></script>

<style>

* { margin: 0 padding: 0 }

body {font-family: Arial, Tahoma, sans-serif font-size: 11px color: #232323 }

.wrap { width: 800px margin: 0 auto }

#nav { margin: 0 padding: 0 list-style: none border-left: 1px solid #d5dce8 border-right: 1px solid #d5dce8 border-bottom: 1px solid #d5dce8 border-bottom-left-radius: 4px -moz-border-radius-bottomleft: 4px -webkit-border-bottom-left-radius: 4px border-bottom-right-radius: 4px -moz-border-radius-bottomright: 4px -webkit-border-bottom-right-radius: 4px height: 50px padding-left: 15px padding-right: 15px background: #edf3f7 }

#nav li { float: left display: block background: none position: relative z-index: 999 margin: 0 1px }

#nav li a { display: block padding: 0 font-weight: 700 line-height: 50px text-decoration: none color: #818ba3 zoom: 1 border-left: 1px solid transparent border-right: 1px solid transparent padding: 0px 12px }

#nav li a:hover, #nav li a.hov { background-color: #fff border-left: 1px solid #d5dce8 border-right: 1px solid #d5dce8 color: #576482 }

#nav ul { position: absolute left: 1px display: none margin: 0 padding: 0 list-style: none -moz-box-shadow: 0 1px 3px rgba(0,0,0,0.2) -o-box-shadow: 0 1px 3px rgba(0,0,0,0.2) box-shadow: 0 1px 3px rgba(0,0,0,0.2) -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.2) padding-bottom: 3px }

#nav ul li { width: 180px float: left border-top: 1px solid #fff text-align: left }

#nav ul li:hover { border-left: 0px solid transparent border-right: 0px solid transparent }

#nav ul a { display: block height: 20px line-height: 20px padding: 8px 5px color: #666 border-bottom: 1px solid transparent text-transform: uppercase color: #797979 font-weight: normal }

#nav ul a:hover { text-decoration: none border-right-color: transparent border-left-color: transparent background: transparent color: #4e4e4e }

* html #nav ul { margin: 0 0 0 -2px }

.clearfix:after { content: "." display: block clear: both visibility: hidden line-height: 0 height: 0 }

.clearfix { display: inline-block }

html[xmlns] .clearfix { display: block }

* html .clearfix { height: 1% }

</style>

</head>

<body>

<div class="wrap">

 <ul id="nav">

  <li><a href="#">网站主页</a></li>

  <li><a href="#">关于我</a>

   <ul>

    <li><a href="#">Company</a></li>

    <li><a href="#">Authors</a></li>

    <li><a href="#">Advertising</a></li>

   </ul>

  </li>

  <li><a href="#">RSS订阅</a>

   <ul>

    <li><a href="#">PSD</a></li>

    <li><a href="#">Patterns</a></li>

    <li><a href="#">Icons</a></li>

   </ul>   

  </li>

  <li><a href="#">精美博文</a>

   <ul>

    <li><a href="#">HTML5</a></li>

    <li><a href="#">CSS3</a></li>

    <li><a href="#">jQuery</a></li>

    <li><a href="#">MySQL</a></li>

   </ul>

  </li>

  <li><a href="#">Web工具</a>

   <ul>

    <li><a href="#">Performance</a></li>

    <li><a href="#">CMS Plugins</a></li>

    <li><a href="#">Cheat Sheets</a></li>

   </ul>

  </li>

  <li><a href="#">Originals</a>

   <ul>

    <li><a href="#">Website Design</a></li>

    <li><a href="#">Mobile</a></li>

   </ul>

  </li>

 </ul>

</div>

<script type="text/javascript">

$(document).ready(function() { 

 $('#nav li').hover(function() {

  $('ul', this).slideDown(200)

  $(this).children('a:first').addClass("hov")

 }, function() {

  $('ul', this).slideUp(100)

  $(this).children('a:first').removeClass("hov")  

 })

})

</script>

</body>

</html>