采用CSS和JS的下拉菜单的制作

html-css017

采用CSS和JS的下拉菜单的制作,第1张

<script language="javascript">// JavaScript DocumentstartList = 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><!--body { font: normal 11px verdana}ul { margin: 0padding: 0list-style: nonewidth: 150px/* Width of Menu Items */ border-bottom: 1px solid #ccc} ul li { position: relative} li ul { position: absoluteleft: 149px/* Set 1px less than menu width */ top: 0display: none}/* Styles for Menu Items */ul li a { display: blocktext-decoration: nonecolor: #777background: #fff/* IE6 Bug */ padding: 5pxborder: 1px solid #ccc/* IE6 Bug */ border-bottom: 0} /* Holly Hack. IE Requirement */* ul li { float: leftheight: 1%}* ul li a { height: 1%}/* End */li:hover ul, li.over ul { display: block} /* The magic */--></style><ul id="nav"><li><a #">Home</a></li><li><a #">About</a><ul><li><a #">History</a></li><li><a #">Team</a></li><li><a #">Offices</a></li></ul></li><li><a #">Services</a><ul><li><a #">Web Design</a></li><li><a #">Internet Marketing</a></li><li><a #">Hosting</a></li><li><a #">Domain Names</a></li><li><a #">Broadband</a></li></ul></li><li><a #">Contact Us</a><ul><li><a #">United Kingdom</a></li><li><a #">France</a></li><li><a #">USA</a></li><li><a #">Australia</a></li></ul></li></ul>        lishixinzhi/Article/program/Java/Javascript/201311/25433

这个可以换种方式实现,首先select的样式每个浏览器都有其默认的样式,需要先去除这些默认样式,其次,select里面的样式诸如箭头,下拉框等等的样式,这里提供一种思路,就是在select的外层添加一个div,对这个div元素设置样式,select元素则是没样式,从而达到一种掩眼法的效果,实现方式如下:

<!-- html 布局 -->

<div id="selectStyle">

 <select id="select">

  <option>option 1</option>

  <option>option 2</option>

  <option>option 3</option>

  <option>option 4</option>

  <option>option 5</option>

 </select>

</div>

首先要去掉 #select 的默认样式:

/* 去掉默认样式,设置新的样式 */

#select{

display:block

width:100%

height:100%

box-sizing:border-box

background:none

border:1px solid #222

outline:none

-webkit-appearance:none

padding:0 5px

line-height:inherit

color:inherit

cursor:default

font-size:14px

position:relative

z-index:3

}

#select option{

color:#222

}

#select option:hover{

color:#fff

}

#select option:checked{

background:#535353

color:#fff 

}

然后在外层div#selectStyle设置自定义样式

#selectStyle{

display:block

margin:0 auto

overflow:hidden

height:30px

width:240px

border-radius:0

background:#535353 url("箭头图片地址") right center no-repeat

background-size:auto 80%

color:#fff

line-height:2

/* 如果不想加图片,

   则可以设置一个自己的三角形样式,

   如下的自定义方式,

   见代码1 */

position:relative

z-index:1

}

/* 代码1 */

#selectStyle:before{

position:absolute

z-index:1

top:50%

right:10px

margin-top:-2.5px

display:block

width:0

height:0

border-style:solid

border-width:5px 5px 0 5px

border-color:#fff transparent transparent transparent

content:""

}

/* 代码1 */

#selectStyle:after{

position:absolute

z-index:1

top:50%

right:10px

margin-top:-3.5px

display:block

width:0

height:0

border-style:solid

border-width:5px 5px 0 5px

border-color:#535353 transparent transparent transparent

content:""

}

以上就是自定义select样式的方法;

同时也可以完全不要select这个元素使用div+css来自定义一个跟select一样效果的下拉框(需要Javascript辅助)。

<html>

<head>

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

<meta name="keywords" content="">

<style type=text/css>

table { border:0px}

td { font:normal 12px 宋体}

img { vertical-align:bottomborder:0px}

a { font:normal 12px 宋体color:#215DC6text-decoration:none}

a:hover { color:#428EFF }

/*

展开层的样式

**/

.sec_menu { border-left:1px solid #000000border-right:1px solid whiteborder-bottom:1px solid whiteoverflow:hiddenbackground:#D6DFF7}

/*

根节点字体样式控制

**/

.menu_title span { position:relativetop:2pxleft:8pxcolor:#215DC6font-weight:bold}

.menu_title2 span { position:relativetop:2pxleft:8pxcolor:#428EFFfont-weight:bold}

</style>

<script language=javascript>

function menuShow(obj,maxh,obj2)

{

if(obj.style.pixelHeight<maxh)

{

obj.style.pixelHeight+=maxh/20

obj.filters.alpha.opacity+=5

obj2.background="image/menudown.gif"

if(obj.style.pixelHeight==maxh/10)

obj.style.display='block'

myObj=obj

myMaxh=maxh

myObj2=obj2

setTimeout('menuShow(myObj,myMaxh,myObj2)','5')

}

}

function menuHide(obj,maxh,obj2)

{

if(obj.style.pixelHeight>0)

{

if(obj.style.pixelHeight==maxh/20)

obj.style.display='none'

obj.style.pixelHeight-=maxh/20

obj.filters.alpha.opacity-=5

obj2.background="image/menuup.gif"

myObj=obj

myMaxh=maxh

myObj2=obj2

setTimeout('menuHide(myObj,myMaxh,myObj2)','5')

}

else

if(whichContinue)

whichContinue.click()

}

function menuChange(obj,maxh,obj2)

{

if(obj.style.pixelHeight)

{

menuHide(obj,maxh,obj2)

whichOpen=''

whichcontinue=''

}

else

if(whichOpen)

{

whichContinue=obj2

whichOpen.click()

}

else

{

menuShow(obj,maxh,obj2)

whichOpen=obj2

whichContinue=''

}

}

</script>

<base target=main>

</head>

<body onselectstart="return false" ondragstart="return false" oncontextmenu="return false">

<table width=158 border="0" align=center cellpadding=0 cellspacing=0>

<tr style="cursor:hand">

<td height=25 background=image/menudown.gif class=menu_title id=menuTitle1 onClick="menuChange(menu1,80,menuTitle1)" onmouseover=this.className='menu_title2'onmouseout=this.className='menu_title'>

<span>系统选项</span>

</td>

</tr>

<tr>

<td>

<div class=sec_menu style="width:158pxheight:80pxfilter:alpha(Opacity=100)overflow:hidden" id=menu1>

<table cellpadding=0 cellspacing=0 align=center width=135 style="position:relativetop:10px">

<tr>

<td height=20>

<img src="image/nofollow.gif" width="15" height="15"><a href=../../3our.com/index.html onfocus=this.blur()>安全选项</a>

</td>

</tr>

<tr>

<td height=20>

<img src="image/nofollow.gif" width="15" height="15"><a href=../../3our.com/index.html onfocus=this.blur()>垃圾邮件选项</a>

</td>

</tr>

<tr>

<td height=20>

<img src="image/nofollow.gif" width="15" height="15"><a href=../../3our.com/index.html onfocus=this.blur()>

动态规则选项</a>

</td>

</tr>

</table>

</div>

</td>

</tr>

</table>

<script language=javascript>

var whichOpen=menuTitle1

var whichContinue=''

</script>

<table width=158 border="0" align=center cellpadding=0 cellspacing=0>

<tr style="cursor:hand">

<td height=25 background=image/menuup.gif class=menu_title id=menuTitle2 onClick="menuChange(menu2,120,menuTitle2)" onmouseover=this.className='menu_title2'onmouseout=this.className='menu_title'>

<span>系统规则</span>

</td>

</tr>

<tr>

<td>

<div class=sec_menu style="width:158height:0filter:alpha(Opacity=0)display:none" id=menu2>

<table cellpadding=0 cellspacing=0 align=center width=135 style="position:relativetop:10px">

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

IP过滤规则</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

信封过滤规则</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

信头过滤规则</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

信体过滤规则</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

附件过滤规则</a>

</td>

</tr>

</table>

</div>

</td>

</tr>

</table>

<table width=158 border="0" align=center cellpadding=0 cellspacing=0>

<tr style="cursor:hand">

<td height=25 background=image/menuup.gif class=menu_title id=menuTitle3 onClick="menuChange(menu3,120,menuTitle3)" onmouseover=this.className='menu_title2'onmouseout=this.className='menu_title'>

<span>系统管理</span>

</td>

</tr>

<tr>

<td>

<div class=sec_menu style="width:158height:0filter:alpha(Opacity=0)display:none" id=menu3>

<table cellpadding=0 cellspacing=0 align=center width=135 style="position:relativetop:10px">

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

用户管理</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

进程管理</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

主机运行状态</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

数据备份</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

垃圾目录管理</a>

</td>

</tr>

</table>

</div>

</td>

</tr>

</table>

<table width=158 border="0" align=center cellpadding=0 cellspacing=0>

<tr style="cursor:hand">

<td height=25 background=image/menuup.gif class=menu_title id=menuTitle4 onClick="menuChange(menu4,140,menuTitle4)" onmouseover=this.className='menu_title2'onmouseout=this.className='menu_title'>

<span>系统统计</span>

</td>

</tr>

<tr>

<td>

<div class=sec_menu style="width:158height:0filter:alpha(Opacity=0)display:none" id=menu4>

<table cellpadding=0 cellspacing=0 align=center width=135 style="position:relativetop:10px">

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

IP过滤统计</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

信封过滤统计</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

信头过滤统计</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

信体过滤统计</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

附件过滤统计</a>

</td>

</tr>

<tr>

<td height=20>

<img src=image/nofollow.gif><a href=../../3our.com/index.html onfocus=this.blur()>

动态规则统计</a>

</td>

</tr>

</table>

</div>

</td>

</tr>

</table>

</body>

</html>