Welcome, Old Sport!

纯CSS3动画下拉导航

by ilikecss ON 2012/07/11 8449

纯CSS3动画下拉导航

之前在文章中已经介绍过CSS属性选择器,由于最近一直在学习CSS3中,接触到不少新的知识,有的非常搞脑子,比如父子选择器,伪类,一些复杂的动画效果,甚至还有CSS4的东西让我头脑很混乱。所以趁热打铁分享一款纯CSS3打造的下拉导航。在这款导航中运用到许多父子选择,gradient(渐变)以及transition(过度动画),许多样式都要写好几套兼容模式,请使用支持CSS3浏览器查看Demo

Step1. 创建HTML

<ul id="navi" class="clearfix">
	<li><a href="#">Home</a></li>
    <li><a href="#">About Me</a></li>
	<li>
    <a href="#">Portfolio</a>
    <ul>
        <li>
        <a href="#">Web Design</a>
        // 子集元素 
        <ul>
        <li><a href="#">2010</a></li>
        <li><a href="#">2011</a></li>
        <li><a href="#">2012</a></li>
        </ul>
        </li>
        <li>
        <a href="#">Graphic Design</a>
        <ul>
        <li><a href="#">2010</a></li>
        <li><a href="#">2011</a></li>
        <li><a href="#">2012</a></li>
        </ul>
        </li>
        <li><a href="#">Development</a></li>
    </ul>
    </li>
	<li><a href="#">Contact Me</a></li>
</ul>

Step2. 创建CSS

#navi ul {
	margin: 20px 0 0 0;
	_margin: 0; /*IE6 only*/
	opacity: 0;
	visibility: hidden;
	position: absolute;
	top: 40px;
	left: 20px;
	z-index: 1;    
	background: #444;	
	background: -moz-linear-gradient(#444, #111);
	background-image: -webkit-gradient(linear, left top, left bottom, from(#444), to(#111));
	background: -webkit-linear-gradient(#444, #111);    
	background: -o-linear-gradient(#444, #111);	
	background: -ms-linear-gradient(#444, #111);	
	background: linear-gradient(#444, #111);
	-webkit-box-shadow: 0 -1px 0 rgba(255,255,255,.3);
	box-shadow: 0 -1px 0 rgba(255,255,255,.3);	
	border-radius: 3px;
	-webkit-transition: all .2s ease-in-out;
	-moz-transition: all .2s ease-in-out;
	-ms-transition: all .2s ease-in-out;
	-o-transition: all .2s ease-in-out;
	transition: all .2s ease-in-out;  
}
#navi li:hover > ul {
	opacity: 1;
	visibility: visible;
	margin: 0;
}
#navi ul ul {
	top: 0;
	left: 150px;
	margin: 0 0 0 20px;
	_margin: 0; /*IE6 only*/
	box-shadow: -1px 0 0 rgba(255,255,255,.3);		
}
#navi ul li {
	float: none;
	display: block;
	border: 0;
	_line-height: 0; /*IE6 only*/
	box-shadow: 0 1px 0 #111, 0 2px 0 #666;
}
#navi ul li:last-child {   
	box-shadow: none;    
}
#navi ul a {    
	padding: 10px;
	width: 130px;
	_height: 10px; /*IE6 only*/
	display: block;
	white-space: nowrap;
	float: none;
	text-transform: none;
}
#navi ul a:hover {
	background-color: #507596;
	background-image: linear-gradient(#04acec, #0186ba);
}
#navi ul li:first-child > a {
	border-radius: 3px 3px 0 0;
}
#navi ul li:first-child > a:after {
	content: '';
	position: absolute;
	left: 40px;
	top: -6px;
	border-left: 4px solid transparent;
	border-right: 4px solid transparent;
	border-bottom: 4px solid #444;
}
#navi ul ul li:first-child a:after {
	left: -6px;
	top: 50%;
	margin-top: -6px;
	border-left: 0;	
	border-bottom: 4px solid transparent;
	border-top: 4px solid transparent;
	border-right: 4px solid #3b3b3b;
}
#navi ul li:first-child a:hover:after {
	border-bottom-color: #507596; 
}
#navi ul ul li:first-child a:hover:after {
	border-right-color: #507596; 
	border-bottom-color: transparent; 	
}
#navi ul li:last-child > a {
	border-radius: 0 0 3px 3px;
}

关于介绍CSS伪类的一片文章

14条评论
  1. #1

    体验盒子说道:

    效果很好,清爽~

  2. #2

    Kayo说道:

    很赞呢!等到CSS3获得了多浏览器支持,web肯定会更精彩。

  3. #3

    啄木游弋说道:

    纯css3打造成这个效果,真的很赞。不过总体上感觉,还是有一点欠缺,就是子菜单有一种突然跳出来的感觉,从无到有的速度太快,有一点突兀。不知道技术上能不能实现呢?要是更平滑更舒缓,效果会更perfect

  4. #4

    Prashant说道:

    Dude, right on there borhter.

  5. #5

    aksoft说道:

    鼠标离开 win8 ie10,投影会消失

  6. 不错,挺实用的

  7. #7

    丿EM说道:

    不错。只是不支持IE,有点可惜




TOP