怎么使用CSS3创建动态菜单

html-css012

怎么使用CSS3创建动态菜单,第1张

方法1: 用Css实现

<html>

<head>

<title>css菜单显示效果</title>

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

<script type="text/javascript">

/**

*菜单的构造,需要绑定到onload

*/

startList = function() {

if (document.all&&document.getElementById) {

navRoot = document.getElementById("nav")

for (i=0i<navRoot.childNodes.lengthi++) {

node = navRoot.childNodes[i]

if (node.nodeName=="LI") {

node.onmouseover=function() {

this.className+=" over"

}

node.onmouseout=function() {

this.className=this.className.replace(" over", "")

}

}

}

}

}

window.onload=startList

</script>

<style type="text/css" media="all">

/**

子乌的叶子css:主样式表

这个样式表的结构参考了<a href="http://www.zeldman.com">zeldman</a>的样式表结构

update:05.11.18

author:sheneyan

*/

html {min-width: 742px}

/*css基本规则*/

img{border:0}

p.access {display:none}

div#counters{display:none}/*计数器暂时不显示*/

a{text-decoration: none}

/* 主要布局 */

body{

overflow:auto

text-align: center

margin: 0 auto

padding: 0

border: 0

background: #fff

color: #000

font: small/18px "宋体", Verdana, Helvetica, sans-serif

}

/**菜单的控制,根据<a href="http://www.alistapart.com/d/horizdropdowns/horizontal2.htm">css 菜单</a>修改而来*/

ul#nav,ul#nav ul{

margin: 0 auto

text-align:left

padding: 0

list-style: none

background:#fff

z-index:99

}

ul#nav {

width:732px

display:block

height:24px

clear:both

}

ul#nav li {

position: relative

z-index:999

float:left

}

ul#nav ul li{

display:block

}

ul#nav ul {

width:100px

height:auto

position: absolute

text-align:left

left: 0px

display: none

border:solid 1px #697210

}

/*当鼠标在子菜单和父菜单上时,父菜单的样式*/

ul#nav li.over a,ul#nav li:hover a{

border-color:#E2144A

background: #fdd

font-weight:bold

color: #E2144A

}

/*将子菜单的样式清除*/

ul#nav li.over ul a,ul#nav li:hover ul a{

background:#fff

font-weight:normal

color:#777

}

/*子菜单的hover样式*/

ul#nav li.over ul a:hover,ul#nav li:hover ul a:hover{

background:#fff

font-weight:normal

color: #E2144A

background: #fdd

border-color:#E2144A

font-weight:bold

}

/* Styles for Menu Items */

ul#nav a {

font-size:14px

line-height:17px

display: block

padding:0 0 0 10px

width:78px

color: #777

height:17px

background: #fff

border-left:solid 1px #fff

border-top:solid 1px #fff

border-right:solid 1px #fff

border-bottom:solid 5px #697210

}

ul#nav ul li{

width:100px

border:0

}

/* Fix IE. Hide from IE Mac \*/

* html ul#nav li { float: leftheight: 17px}

* html ul#nav li a { height: 17px}

/* End */

ul#nav ul a { padding: 2px 0px 2px 10pxborder:0width:90px} /* Sub Menu Styles */

ul#nav li:hover ul,ul#nav li.over ul { display: block} /* The magic */

</style>

<body>

测试css菜单显示效果<br/>

<!--菜单开始-->

<ul id='nav'>

<li><a href='/g.php/working.html'>文章</a>

<ul>

<li><a href='/g.php/working/29.html'>随笔</a></li>

<li><a href='/g.php/working/30.html'>小说</a></li>

<li><a href='/g.php/working/31.html'>诗</a></li>

<li><a href='/g.php/working/32.html'>文摘</a></li>

</ul>

</li>

<li><a href='/g.php/pic.html'>美图</a>

<ul>

<li><a href='/g.php/pic/19.html'>漫画</a></li>

<li><a href='/g.php/pic/20.html'>摄影</a></li>

<li><a href='/g.php/pic/33.html'>CG作品</a></li>

<li><a href='/g.php/pic/41.html'>图+文</a></li>

</ul>

</li>

<li><a href='#'>技术</a>

<ul>

<li><a href='/g.php/tech/tool/37.html'>在线工具</a></li>

<li><a href='/g.php/tech/collect/43.html'>代码收集</a></li>

<li><a href='/g.php/tech/lab/44.html'>实验室</a></li>

<li><a href='/g.php/tech/article/45.html'>技术文章</a></li>

</ul>

</li>

<li><a href='/g.php/fav.html'>网站收藏夹</a></li>

<li><a href='/g.php/working.html'>读书笔记</a>

<ul>

<li><a href='/g.php/working/22.html'>哲学</a></li>

<li><a href='/g.php/working/23.html'>小说</a></li>

</ul>

</li>

<li><a href='/g.php/working.html'>日记</a>

<ul>

<li><a href='/g.php/working/27.html'>生活</a></li>

<li><a href='/g.php/working/28.html'>工作</a></li>

</ul>

</li>

<li><a href='#'>计划</a>

<ul>

<li><a href='/g.php/todo/39.html'>计划要做的事</a></li>

<li><a href='/g.php/tobuy/40.html'>计划要买的</a></li>

</ul>

</li>

<li><a href='/g.php/working.html'>其他</a>

<ul>

<li><a href='/g.php/working/34.html'>关于本站</a></li>

<li><a href='/g.php/working/35.html'>留言</a></li>

<li><a href='/g.php/working/36.html'>友情链接</a></li>

</ul>

</li>

</ul><!--菜单结束-->

</Html>

将animation2改为

@-webkit-keyframes animation2{

    from{margin-left:0}

    to{margin-left:185pxopacity:1}

}

@-moz-keyframes animation2{

    from{margin-left:0}

    to{margin-left:185pxopacity:1}

}

1.boder-radis圆角的制作

2.linear-gradient渐变背景的制作

3.box-shadow阴影效果的制作

4.transition转换效果的制作

5.transparent透明效果

6.CCS3中before的作用

实现的效果图如下:

搭建HTML容器

一级菜单ul的样式修饰:

设置一级菜单的宽度、整体居中显示、用border设置颜色为#222的1像素宽的边框、用background-color设置背景颜色为#111、用background-image和linear-gradient设置背景图片为#444和#111的渐变效果、用border-radius设置圆角边框为6px、用before和after的方式给一级ul填充内容是清除二级浮动的一种方式保证背景能够包裹所有ul内的元素

.top-nav{width:969pxmargin:60px autoborder:1px solid #222background-color:#111               background-image:linear-gradient(#444,#111)border-radius:6pxbox-shadow:0 1px 1px #777               padding:0list-style:none} .top-nav:before,.top-nav:after{content: ""display: table} .top-nav:after{clear: both}

一级菜单内li的样式修饰:

用float将所有的li左浮动、用boder-right给每一个li制作一个左边框效果、用box-shadow制作阴影效果、position:relative是为了下面二级菜单的显示二级菜单会以absolute的方式定位

.top-nav li{float: leftborder-right:1px solid #222box-shadow:1px 0 0 #444position: relative}

一级菜单内li中a元素以及鼠标移动到a元素上之后的样式修饰

这些属性比较简单,就不作一一介绍了

.top-nav li a{float:lefttext-decoration: nonepadding:12px 30pxfont-size:12pxfont-weight:boldtext-shadow: 0 2px 0 #000color: #999} .top-nav li a:hover{color: #fafafa}

二级菜单ul的样式修饰

二级菜单默认是隐藏状态,用绝对定位的方式(相对于一级菜单)将二级菜单定位在top:38pxleft:0的位置上、同样给二级菜单设置渐变背景色和阴影的效果box-shadow中的rgba属性可以制作透明度的效果、用opacity这是透明级别为0、用transition制作一个过度显示和隐藏的效果

.top-nav li ul{visibility: hiddenposition: absolutelist-style: nonetop:38pxleft: 0z-index: 1padding: 0                     background-color: #444background-image: linear-gradient(#444,#111)box-shadow: 0 -1px 0                     rgba(255,255,255,.3)border-radius: 3pxopacity: 0margin: 20px 0 0 0transition: all .2s ease-in-out                    }

当鼠标移动到一级菜单栏的时候二级菜单栏显示

hover属性的应用

.top-nav li:hover >ul{opacity: 1visibility: visiblemargin: 0}

设置二级菜单li和a的样式

方法和一级菜单的li和a的修饰差不多,不做过多的解释

.top-nav ul li{float: nonedisplay: blockborder: 0box-shadow: 0 1px 0 #111,0 2px 0 #666} .top-nav ul a{padding: 10pxwidth: 130pxdisplay: blockfloat: none} .top-nav ul a:hover{background-color: #0186babackground-image: linear-gradient(#04acec,#0186ba)} .top-nav ul li:first-child >a{border-radius: 3px 3px 0 0} .top-nav ul li:last-child >a{border-radius: 0 0 3px 3px}

三角形的制作

用css3制作三角形的的方法就是四边设置同样宽度的border,将要保留的那一边的border设置颜色,其他三边的颜色设置为透明(transparent),则保留颜色的那一边的三角形就制作出来了

.top-nav ul li:first-child >a:before{  content: ""position: absoluteleft:40pxtop: -6px          display: blockwidth: 0border-bottom: 6px solid #444border-left: 6px solid transparent border-right: 6px solid transparent } .top-nav ul li:first-child >a:hover:before{border-bottom-color:#04acec}

三级菜单位置的变换

将三级菜单定位到二级菜单的右边显示

.top-nav ul ul{top:0left: 150pxmargin: 0 0 0 20pxbox-shadow: -1px 0 0 rgba(255,255,255,.3)} .top-nav ul ul li:first-child a:before{left: -6pxtop: 50%margin-top: -6pxborder-left: 0border-bottom: 6px solid transparent                     border-top: 6px solid transparentborder-right: 6px solid #3b3b3b} .top-nav ul ul li:first-child a:hover:before{border-right-color:#0299d3border-bottom-color:transparent}

完整代码(其中有针对IE6的兼容性处理)

<!DOCTYPE html><html><head>  <meta charset="UTF-8">  <!--<script src="http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>-->  <title>用CSS3实现动画效果的制作</title>  <style type="text/css">      .top-nav{width:969pxmargin:60px autoborder:1px solid #222background-color:#111               background-image:linear-gradient(#444,#111)border-radius:6pxbox-shadow:0 1px 1px #777               padding:0list-style:none}       .top-nav:before,.top-nav:after{content: ""display: table}       .top-nav:after{clear: both}       /*针对IE6,_margin,_height是针对IE6的,其他浏览器中不需要*/       .top-nav{z-index: 1}       .top-nav li{float: leftborder-right:1px solid #222box-shadow:1px 0 0 #444position: relative}       .top-nav li a{float:lefttext-decoration: nonepadding:12px 30pxfont-size:12pxfont-weight:bold          text-shadow: 0 2px 0 #000color: #999}       .top-nav li a:hover{color: #fafafa}       .top-nav li ul{visibility: hiddenposition: absolutelist-style: nonetop:38pxleft: 0z-index: 1padding: 0                     background-color: #444background-image: linear-gradient(#444,#111)box-shadow: 0 -1px 0 rgba(255,255,255,.3)                     border-radius: 3pxopacity: 0margin: 20px 0 0 0transition: all .2s ease-in-out                     _margin:0 }       .top-nav li:hover >ul{opacity: 1visibility: visiblemargin: 0}       .top-nav ul li{float: nonedisplay: blockborder: 0box-shadow: 0 1px 0 #111,0 2px 0 #666}       .top-nav ul a{padding: 10pxwidth: 130pxdisplay: blockfloat: none_height:10px}       .top-nav ul a:hover{background-color: #0186babackground-image: linear-gradient(#04acec,#0186ba)}       .top-nav ul li:first-child >a{border-radius: 3px 3px 0 0}       .top-nav ul li:last-child >a{border-radius: 0 0 3px 3px}       .top-nav ul li:first-child >a:before{  content: ""position: absoluteleft:40pxtop: -6px          display: blockwidth: 0border-bottom: 6px solid #444border-left: 6px solid transparent border-right: 6px solid transparent }       .top-nav ul li:first-child >a:hover:before{border-bottom-color:#04acec}       .top-nav ul ul{top:0left: 150pxmargin: 0 0 0 20px_margin:0box-shadow: -1px 0 0 rgba(255,255,255,.3)}       .top-nav ul ul li:first-child a:before{left: -6pxtop: 50%margin-top: -6pxborder-left: 0border-bottom: 6px solid transparent                     border-top: 6px solid transparentborder-right: 6px solid #3b3b3b}       .top-nav ul ul li:first-child a:hover:before{border-right-color:#0299d3border-bottom-color:transparent}   </style>  <script type="text/javascript">      <!--针对IE6处理hover的问题-->//        $(function () { //            if($.browser.msie &&$.broswer.version.substr(0,1)<7){ //            $("li").has("ul").mouseover(function () { //                $(this).children("ul").css("visibility","visible")//            }).mouseout(function () { //                $(this).children("ul").css("visibility","hidden")//            })//            } //        })   </script></head><body><ul>  <li><a href="#">首页</a></li>  <li><a href="#">课程大厅</a></li>  <li><a href="#">学习中心 +</a>      <ul>          <li><a href="#">视频学习</a>              <ul>                  <li><a href="#">css</a></li>                  <li><a href="#">js</a></li>                  <li><a href="#">jquery</a></li>              </ul>          </li>          <li><a href="#">案例学习</a></li>          <li><a href="#">交流平台</a></li>      </ul>  </li>  <li><a href="#">关于我们</a></li></ul></body>