作者的blog:http://www.iwcn.net
论坛经常有人会问到用CSS如何美化Select标签,其实但凡你看到很酷的都是用javascript来实现的。昨天试着做了一下,基本实现的初级功能。拿出来和大家一起分享一下。
先可以看一下预览效果:http://www.iwcn.net/demo/select
功能需求
1、调用要方便,做好之后应该像这样:
function loadSelect(selectobj){
//传入一个select对象就能将他的样式美化
}
2、不改变原有表单项,表单的页面代码不去破坏:
<form name="f" οnsubmit="getResult();">
<fieldset>
<legend>用户注册</legend>
<div>
<label for="username">帐号</label>
<input type="text" id="username" name="username" />
</div>
<div>
<label for="pwd">密码</label>
<input type="password" name="pwd" id="pwd" />
</div>
<div>
<label for="province">省份</label>
<select id="province" name="province">
<option value="10">江西</option>
<option value="11">福建</option>
<option value="12">广东</option>
<option value="13">浙江</option>
</select>
</div>
</fieldset>
<input type="submit" value="提交" name="btnSub" />
</form>
实现思路
- 第一步:将表单中的select隐藏起来。
为什么?很简单,因为这家伙太顽固了,用css根本搞不出来你想要的。所以把它杀掉。 - 第二步:用脚本找到select标签在网页上的绝对位置。
我们在那个位置上用DIV标签做个假的、好看点的来当他的替身。 - 第三步:用脚本把select标签中的值读出来。
虽然藏起来了,但它里边的options我们还有用呢,统统取过来。 - 第四步:当用户点击select标签的替身,也就是div的时候。我们再用一个div浮在上一个div的下边,这个就是options的替身了。
大致上就是这样了,接下来我们一步一步去实现它!
准备工作
- 1、想好你要把select美化成什么样子,并准备好相应的图片。我准备了两张小图,就是下拉箭头1和下拉箭头2,1是默认样式,2是鼠标移过来的样式。
- 2、写好一个普通的表单递交页面,比如下边这个。注意我给select定义了基本的CSS样式、在头部添加了调用js文件的代码、在body中添加了调用函数的脚本。
运行代码框
2 < html xmlns ="http://www.w3.org/1999/xhtml" lang ="zh-CN" >
3 < head >
4 < meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
5 < meta http-equiv ="Content-Language" content ="zh-CN" />
6 < title > 用javascript美化Select </ title >
7 < script type ="text/javascript" src ="select.js" ></ script >
8 < style type ="text/css" >
9 select{}{width:200px;height:20px;}
10 </ style >
11 </ head >
12 < body >
13 < h1 > 用javascript模拟select达到美化效果 </ h1 >
14 < form name ="f" onsubmit ="getResult();" >
15 < fieldset >
16 < legend > 用户注册 </ legend >
17 < div >
18 < label for ="username" > 帐号 </ label >
19 < input type ="text" id ="username" name ="username" />
20 </ div >
21 < div >
22 < label for ="pwd" > 密码 </ label >
23 < input type ="password" name ="pwd" id ="pwd" />
24 </ div >
25 < div >
26 < label for ="province" > 省份 </ label >
27 < select id ="province" name ="province" >
28 < option value ="10" > 江西 </ option >
29 < option value ="11" > 福建 </ option >
30 < option value ="12" > 广东 </ option >
31 < option value ="13" > 浙江 </ option >
32 </ select >
33 </ div >
34 </ fieldset >
35 < input type ="submit" value ="提交" name ="btnSub" />
36 </ form >
37 < script type ="text/javascript" >
38 loadSelect(document.f.province);
39 </ script >
40 < p >
41 < a href ="http://www.iwcn.net" > 作者博客 </ a >
42 </ p >
43 </ body >
44 </ html >
编写javascript
<script type="text/javascript" src="select.js"></script>
新建一个js文件并保存为select.js,剩下的工作我们全部在这个js中去完成。
函数名:loadSelect(obj);
参数:select对象
相关函数:
//取标签的绝对位置
1{
2 var t = e.offsetTop;
3 var l = e.offsetLeft;
4 var w = e.offsetWidth;
5 var h = e.offsetHeight-2;
6
7
8while(e=e.offsetParent)
9 {
10 t+=e.offsetTop;
11 l+=e.offsetLeft;
12 }
13 return {
14 top : t,
15 left : l,
16 width : w,
17 height : h
18 }
19}
第一步:把select的绝对位置记录下来。一会替身上来就知道应该站那里了。
var offset=Offset(obj);
//这里解释一下Offset是一个函数,用来获取对象的绝对位置。写在loadSelect()函数外边的。他有四个属性分别是top/left/width/height。
第二步:将select隐藏。
obj.style.display="none";
第三步:虚拟一个div出来代替select
1 var iDiv = document.createElement("div");
2 iDiv.id="selectof" + obj.name;
3 iDiv.style.position = "absolute";
4 iDiv.style.width=offset.width + "px";
5 iDiv.style.height=offset.height + "px";
6 iDiv.style.top=offset.top + "px";
7 iDiv.style.left=offset.left + "px";
8 iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
9 iDiv.style.border="1px solid #3366ff";
10 iDiv.style.fontSize="12px";
11 iDiv.style.lineHeight=offset.height + "px";
12 iDiv.style.textIndent="4px";
13 document.body.appendChild(iDiv);
第四步:把原始select没人选中的值给它。
第五步:为替身添加鼠标移过样式。
1 iDiv.onmouseover=function(){//鼠标移到
2 iDiv.style.background="url(icon_select_focus.gif) no-repeat right 4px";
3 }
4 iDiv.onmouseout=function(){//鼠标移走
5 iDiv.style.background="url(icon_select.gif) no-repeat right 4px";
6 }
第六步:添加关键的鼠标点击事件。
1iDiv.onclick=function(){//鼠标点击
2 if (document.getElementById("selectchild" + obj.name)){
3 //判断是否创建过div
4 if (childCreate){
5 //判断当前的下拉是不是打开状态,如果是打开的就关闭掉。是关闭的就打开。
6 document.getElementById("selectchild"+ obj.name).style.display="none";
7 childCreate=false;
8 }else{
9 document.getElementById("selectchild" + obj.name).style.display="";
10 childCreate=true;
11 }
12 }else{
13 //初始一个div放在上一个div下边,当options的替身。
14
15 var cDiv = document.createElement("div");
16 cDiv.id="selectchild" + obj.name;
17 cDiv.style.position = "absolute";
18 cDiv.style.width=offset.width + "px";
19 cDiv.style.height=obj.options.length *20 + "px";
20 cDiv.style.top=(offset.top+offset.height+2) + "px";
21 cDiv.style.left=offset.left + "px";
22 cDiv.style.background="#f7f7f7";
23 cDiv.style.border="1px solid silver";
24 var uUl = document.createElement("ul");
25 uUl.id="uUlchild" + obj.name;
26 uUl.style.listStyle="none";
27 uUl.style.margin="0";
28 uUl.style.padding="0";
29 uUl.style.fontSize="12px";
30 cDiv.appendChild(uUl);
31 document.body.appendChild(cDiv);
32 childCreate=true;
33 for (var i=0;i<obj.options.length;i++){
34 //将原始的select标签中的options添加到li中
35 var lLi=document.createElement("li");
36 lLi.id=obj.options[i].value;
37 lLi.style.textIndent="4px";
38 lLi.style.height="20px";
39 lLi.style.lineHeight="20px";
40 lLi.innerHTML=obj.options[i].innerHTML;
41 uUl.appendChild(lLi);
42 }
43 var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
44 for (var j=0;j<obj.options.length;j++){
45 //为li标签添加鼠标事件
46 liObj[j].onmouseover=function(){
47 this.style.background="gray";
48 this.style.color="white";
49 }
50 liObj[j].onmouseout=function(){
51 this.style.background="white";
52 this.style.color="black";
53 }
54 liObj[j].onclick=function(){
55 //做两件事情,一是将用户选择的保存到原始select标签中,要不做的再好看表单递交后也获取不到select的值了。
56 obj.options.length=0;
57 obj.options[0]=new Option(this.innerHTML,this.id);
58 //同时我们把下拉的关闭掉。
59 document.getElementById("selectchild" + obj.name).style.display="none";
60 childCreate=false;
61 iDiv.innerHTML=this.innerHTML;
62 }
63 }
64 }
65 }
最后这个比较复杂一点,再解释一下,我们在做这一步之前,select的样子已经出来了,下一步就是再加一个div去模仿select被点击之后出现的下拉选项了。我们可以讲select标签的options通过javascript提取出来,把它写成这样:
2 <ul>
3 <li>optionName</li>
4 <li>optionName</li>
5 <li>optionName</li>
6 </ul>
7 </div>
基本上就这样了。还有些缺陷,有时间大家可以一起补充!