CSS特效之:让select框"隐身"

本想做个select option框完全隐身的效果,查找了诸多资料,结果发现《网页设计专家门诊》上的方法最为可行,也就是做两个层,下面的为设定的select选项框,而上面的层设定width和height小于select框,相当于作图中的遮罩,则可以做到将select框的上下左右边框隐藏,但是下拉框的箭头就不行了--即如要达到"隐身"的效果几乎是不可能的。无奈,CSS一旦碰到win的组建,简直就是秀才与兵~

问题还是需要解决,设计效果需要尽最大的力量来实现,如果不行则修改设计方案,这是后话了。重新审视问题后发现,需求是实现"下拉框隐身的效果",并非是"让下拉框隐身",那么同样的效果则可以有不同的方式,其中使用CSS下拉菜单是最简单和最可行的。

常用下拉方式有横向导航形式Ⅰ(横+横),横向下拉菜单形式Ⅱ(横+纵),和纵向下拉菜单形式Ⅲ(纵+纵),分别如下(其中括号中的横,纵为子菜单的扩展方式):

横向导航形式Ⅰ(横+横):

<!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-CN" />
<title>横向导航菜单</title>
<style type="text/css">
<!--
* {margin:0;padding:0;border:0;}
body {
font-family: arial, 宋体, serif;
font-size:12px;
}
#nav {
height: 24px; list-style-type: none; padding-left:200px;
line-height:24px;overflow:hidden;background:#999;
}
#nav a {
display: block; width: 80px; text-align:center;
}
#nav a:link {
color:#EEE; text-decoration:none;
}
#nav a:visited {
color:#EEE;text-decoration:none;
}
#nav a:hover {
color:#FFF;text-decoration:none;font-weight:bold;background:#CCC;
}
#nav li {
float: left; width: 80px;
}
#nav li ul {
line-height: 24px;
list-style-type: none;
text-align:left;
left: -999px;
width: 520px; //注意,这里一定要设置宽度;
position: absolute;
background:#CCC;
}
#nav li ul li{
float: left;width: 80px; //float就是子菜单横向的关键,可以继承父菜单而省略float
}
#nav li ul a{
display: block; width: 65px;text-align:left;padding-left:15px;
}
#nav li ul a:link {
color:#F1F1F1; text-decoration:none;
}
#nav li ul a:visited {
color:#F1F1F1;text-decoration:none;
}
#nav li ul a:hover {
color:#FFF;text-decoration:none;font-weight:normal;background:#C00;
}
#nav li:hover ul {
left:25%;
}
#nav li.sfhover ul {
left:25%;
}
#content {
clear: left;
}
-->
</style>
<script type=text/javascript><!--//--><![CDATA[//><!--
function menuFix() {
var sfEls = document.getElementById("nav").getElementsByTagName("li");
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
sfEls[i].onMouseDown=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
sfEls[i].onMouseUp=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
sfEls[i].onmouseout=function() {
this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"),
"");
}
}
}
window.onload=menuFix;
//--><!]]></script>
</head>
<body>
<ul id="nav">
<li><a href="#">菜单一</a>
<ul>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
<li><a href="#">菜单一</a></li>
</ul>
</li>
<li><a href="#">菜单二</a>
<ul>
<li><a href="#">菜单二</a></li>
<li><a href="#">菜单二</a></li>
<li><a href="#">菜单二</a></li>
<li><a href="#">菜单二</a></li>
<li><a href="#">菜单二</a></li>
</ul>
</li>
<li><a href="#">菜单三</a>
<ul>
<li><a href="#">菜单三</a></li>
<li><a href="#">菜单三</a></li>
<li><a href="#">菜单三</a></li>
<li><a href="#">菜单三</a></li>
<li><a href="#">菜单三</a></li>
</ul>
</li>
<li><a href="#">菜单四</a>
<ul>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
</ul>
</li>
<li><a href="#">菜单五</a>
<ul>
<li><a href="#">菜单五</a></li>
<li><a href="#">菜单五</a></li>
<li><a href="#">菜单五</a></li>
<li><a href="#">菜单五</a></li>
<li><a href="#">菜单五</a></li>
</ul>
</li>
<li><a href="#">菜单六</a>
<ul>
<li><a href="#">菜单六</a></li>
<li><a href="#">菜单六</a></li>
<li><a href="#">菜单六</a></li>
<li><a href="#">菜单六</a></li>
<li><a href="#">菜单六</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>

横向下拉菜单形式Ⅱ(横+纵):

<!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>CSS横向下拉菜单演示</title>
<style type="text/css">
<!--
*{margin:0;padding:0;border:0;}
body {
font-family: arial, 宋体, serif;
font-size:12px;
}
#nav {
line-height: 24px; list-style-type: none; background:#666;
}
#nav a {
display: block; width: 80px; text-align:center;
}
#nav a:link {
color:#666; text-decoration:none;
}
#nav a:visited {
color:#666;text-decoration:none;
}
#nav a:hover {
color:#FFF;text-decoration:none;font-weight:bold;
}
#nav li {
float: left; width: 80px; background:#CCC;
}
#nav li a:hover{
background:#999;
}
#nav li ul {
line-height: 27px; list-style-type: none;text-align:left;
left: -999em; width: 180px; position: absolute;
}
#nav li ul li{
float: left; width: 180px;
background: #F6F6F6;
}
#nav li ul a{ wedisplay: block; width: 156px;text-align:left;padding-left:24px;
}
#nav li ul a:link {
color:#666; text-decoration:none;
}
#nav li ul a:visited {
color:#666;text-decoration:none;
}
#nav li ul a:hover {
color:#F3F3F3;text-decoration:none;font-weight:normal;
background:#C00;
}
#nav li:hover ul {
left: auto;
}
#nav li.sfhover ul {
left: auto;
}
#content {
clear: left;
}
-->
</style>
<script type=text/javascript>
<!--//--><![CDATA[//><!--
function menuFix() {
var sfEls = document.getElementById("nav").getElementsByTagName("li");
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
sfEls[i].onMouseDown=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
sfEls[i].onMouseUp=function() {
this.className+=(this.className.length>0? " ": "") + "sfhover";
}
sfEls[i].onmouseout=function() {
this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"),
"");
}
}
}
window.onload=menuFix;
//--><!]]>
</script>
</head>
<body>
<ul id="nav">
<li><a href="#">新闻</a>
<ul>
<li><a href="#">新闻1</a></li>
<li><a href="#">新闻2</a></li>
<li><a href="#">新闻3</a></li>
<li><a href="#">新闻4</a></li>
<li><a href="#">新闻5</a></li>
<li><a href="#">新闻6</a></li>
</ul>
</li>
<li><a href="#">介绍</a>
<ul>
<li><a href="#">介绍1</a></li>
<li><a href="#">介绍2</a></li>
<li><a href="#">介绍3</a></li>
<li><a href="#">介绍4</a></li>
<li><a href="#">介绍5</a></li>
<li><a href="#">介绍6</a></li>
</ul>
</li>
<li><a href="#">示例</a>
<ul>
<li><a href="#">示例1</a></li>
<li><a href="#">示例2</a></li>
<li><a href="#">示例3</a></li>
<li><a href="#">示例4</a></li>
</ul>
</li>
<li><a href="#">关于</a>
<ul>
<li><a href="#">关于1</a></li>
<li><a href="#">关于2</a></li>
<li><a href="#">关于3</a></li>
<li><a href="#">关于4</a></li>
</ul>
</li>
<li><a href="#">演示</a>
<ul>
<li><a href="#">演示0</a></li>
<li><a href="#">演示6</a></li>
<li><a href="#">演示7</a></li>
<li><a href="#">演示1</a></li>
<li><a href="#">演示2</a></li>
<li><a href="#">演示3</a></li>
<li><a href="#">演示4</a></li>
<li><a href="#">演示5</a></li>
</ul>
</li>
<li><a href="#">联系</a>
<ul>
<li><a href="#">联系1</a></li>
<li><a href="#">联系2</a></li>
<li><a href="#">联系3</a></li>
<li><a href="#">联系4</a></li>
<li><a href="#">联系5</a></li>
<li><a href="#">联系6</a></li>
<li><a href="#">联系7</a></li>
</ul>
</li>
</ul>
</body>
</html>

纵向下拉菜单形式Ⅲ(纵+纵):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSS纵向下拉菜单形式</title>
<style type="text/css">
body {
margin: 0;
padding: 30px;
background: #FFF;
color: #666;
}
h1 {
font: bold 16px Arial, Helvetica, sans-serif;
}
p {
font: 11px Arial, Helvetica, sans-serif;
}
a {
color: #900;
text-decoration: none;
}
a:hover {
background: #900;
color: #FFF;
}
/*CSS Code for Menu Begin:*/
/* Root = Vertical, Secondary = Vertical */
ul#navmenu,
ul#navmenu li,
ul#navmenu ul {
margin: 0;
border: 0 none;
padding: 0;
width: 160px; /*For KHTML*/
list-style: none;
}
ul#navmenu li {
display: block !important; /*For GOOD browsers*/
display: inline; /*For IE*/
position: relative;
}
/* Root Menu */
ul#navmenu a {
border: 1px solid #FFF;
border-right-color: #CCC;
border-bottom-color: #CCC;
padding: 0 6px;
display: block;
background: #EEE;
color: #666;
font: bold 10px/22px Verdana, Arial, Helvetica, sans-serif;
text-decoration: none;
height: auto !important;
height: 1%; /*For IE*/
}
/* Root Menu Hover Persistence */
ul#navmenu a:hover,
ul#navmenu li:hover a,
ul#navmenu li.iehover a {
background: #CCC;
color: #FFF;
}
/* 2nd Menu */
ul#navmenu li:hover li a,
ul#navmenu li.iehover li a {
background: #EEE;
color: #666;
}
/* 2nd Menu Hover Persistence */
ul#navmenu li:hover li a:hover,
ul#navmenu li:hover li:hover a,
ul#navmenu li.iehover li a:hover,
ul#navmenu li.iehover li.iehover a {
background: #CCC;
color: #FFF;
}
/* 3rd Menu */
ul#navmenu li:hover li:hover li a,
ul#navmenu li.iehover li.iehover li a {
background: #EEE;
color: #666;
}
/* 3rd Menu Hover Persistence */
ul#navmenu li:hover li:hover li a:hover,
ul#navmenu li:hover li:hover li:hover a,
ul#navmenu li.iehover li.iehover li a:hover,
ul#navmenu li.iehover li.iehover li.iehover a {
background: #CCC;
color: #FFF;
}
/* 4th Menu */
ul#navmenu li:hover li:hover li:hover li a,
ul#navmenu li.iehover li.iehover li.iehover li a {
background: #EEE;
color: #666;
}
/* 4th Menu Hover */
ul#navmenu li:hover li:hover li:hover li a:hover,
ul#navmenu li.iehover li.iehover li.iehover li a:hover {
background: #CCC;
color: #FFF;
}
ul#navmenu ul,
ul#navmenu ul ul,
ul#navmenu ul ul ul {
display: none;
position: absolute;
top: 0;
left: 160px;
}
/* Do Not Move - Must Come Before display:block for Gecko */
ul#navmenu li:hover ul ul,
ul#navmenu li:hover ul ul ul,
ul#navmenu li.iehover ul ul,
ul#navmenu li.iehover ul ul ul {
display: none;
}
ul#navmenu li:hover ul,
ul#navmenu ul li:hover ul,
ul#navmenu ul ul li:hover ul,
ul#navmenu li.iehover ul,
ul#navmenu ul li.iehover ul,
ul#navmenu ul ul li.iehover ul {
display: block;
}
</style>
<script type="text/javascript">
<!--
navHover = function() {
var lis = document.getElementById("navmenu").getElementsByTagName("LI");
for (var i=0; i<lis.length; i++) {
lis[i].onmouseover=function() {
this.className+=" iehover";
}
lis[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" iehover\\b"), "");
}
}
}
if (window.attachEvent) window.attachEvent("onload", navHover);
-->
</script>
</head>
<body>
<h1>CSS纵向下拉菜单</h1>
<hr />
<ul id="navmenu">
<li><a href="#">菜单1</a></li>
<li><a href="#">菜单2+</a>
<ul>
<li><a href="#">菜单2.1 +</a>
<ul>
<li><a href="#">菜单2.1.1</a></li>
<li><a href="#">菜单2.1.2</a></li>
<li><a href="#">菜单2.1.3</a></li>
<li><a href="#">菜单2.1.4</a></li>
</ul>
</li>
<li><a href="#">菜单2.2</a></li>
<li><a href="#">菜单2.3</a></li>
</ul>
</li>
<li><a href="#">菜单3 +</a>
<ul>
<li><a href="#"><a href="#" target="_blank"><font color=red>菜单3.1</font></a> +</a>
<ul>
<li><a href="#">菜单3.1.1</a></li>
<li><a href="#">菜单3.1.2</a></li>
<li><a href="#">菜单3.1.3</a></li>
<li><a href="#">菜单3.1.4</a></li>
<li><a href="#">菜单3.1.5</a></li>
</ul>
</li>
<li><a href="#">菜单3.2 +</a>
<ul>
<li><a href="#">菜单3.2.1</a></li>
<li><a href="#">菜单3.2.2</a></li>
<li><a href="#">菜单3.2.3</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">菜单4</a></li>
<li><a href="#">菜单5
</a></li>
</ul>
</body>
</html>