Tab选项卡
采用两种方法实现选项卡切换功能,目前只提供了最基本的切换效果,后期增加jquery版和渐变切换效果。
效果图:
纯JS简化版:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<html> <head> <title>Tab选项卡 - 纯JS简化版</title> <meta http-equiv= "Content-Type"
content= "text/html; charset=utf-8" >
</head> <style type= "text/css" >
#btn { width: 500px; height: 30px; margin:0;padding: 0; list-style: none;} #btn li { cursor: pointer; float: left; width: 100px; height: 30px; margin-right: 2px; background: #666; color:#fff; text-align: center; line-height: 30px;} #btn li.onbtn {background: #f00;} #tabs div {display: none; width: 500px; height: 100px; border: 1px solid #ccc;} </style> <script type= "text/javascript" >
window.onload = function () {
var
btn = document.getElementById( ‘btn‘ );
var
btnli = btn.getElementsByTagName( ‘li‘ );
var
tabs = document.getElementById( ‘tabs‘ );
var
tabsdiv = tabs.getElementsByTagName( ‘div‘ );
for
( var
a=0; a<btnli.length; a++) {
btnli[a].index=a;
btnli[a].onmouseover = function () {
for
( var
b=0; b<tabsdiv.length; b++) {
btnli[b].className = "" ;
tabsdiv[b].style.display = "none" ;
}
this .className = "onbtn" ;
tabsdiv[ this .index].style.display = "block" ;
}
}
btnli[0].className = "onbtn" ;
tabsdiv[0].style.display = "block" ;
} </script> <body> <div> <ul id= "btn" >
<li>选项一</li>
<li>选项二</li>
<li>选项三</li>
</ul> <div id= "tabs" >
<div>选项一内容</div>
<div>选项二内容</div>
<div>选项三内容</div>
</div> </div> </body> </html> |
纯JS面向对象版:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
<html> <head> <title>纯JS - 面向对象版</title> <meta http-equiv= "Content-Type"
content= "text/html; charset=utf-8" ></head>
<style type= "text/css" >
#btn, #btn2 { width: 500px; height: 30px; margin:0;padding: 0; list-style: none;} #btn li, #btn2 li { cursor: pointer; float: left; width: 100px; height: 30px; margin-right: 2px; background: #666; color:#fff; text-align: center; line-height: 30px;} #btn li.onbtn, #btn2 li.onbtn {background: #f00;} #tabs div, #tabs2 div {display: none; width: 500px; height: 100px; border: 1px solid #ccc; margin-bottom:10px;} </style> <script type= "text/javascript" >
function
tab(btn, tabs) {
var
_this = this ;
var
btn = document.getElementById(btn);
var
tabs = document.getElementById(tabs);
this .btnli = btn.getElementsByTagName( ‘li‘ );
this .tabsdiv = tabs.getElementsByTagName( ‘div‘ );
for
( var
a=0; a< this .btnli.length; a++) {
this .btnli[a].index = a;
this .btnli[a].onmouseover = function () {
_this.onmouseoverfn( this );
}
}
this .tabsdiv[0].style.display = "block" ;
this .btnli[0].className = "onbtn" ;
} tab.prototype.onmouseoverfn = function (obj) {
for
( var
b=0; b< this .btnli.length; b++) {
this .tabsdiv[b].style.display = "none" ;
this .btnli[b].className = "" ;
}
this .tabsdiv[obj.index].style.display = "block" ;
this .btnli[obj.index].className = "onbtn" ;
} window.onload = function () {
new
tab( ‘btn‘ , ‘tabs‘ );
new
tab( ‘btn2‘ , ‘tabs2‘ );
} </script> <body> <div> <ul id= "btn" >
<li>选项一</li>
<li>选项二</li>
<li>选项三</li>
</ul> <div id= "tabs" >
<div>选项一内容</div>
<div>选项二内容</div>
<div>选项三内容</div>
</div> </div> <div> <ul id= "btn2" >
<li>选项一</li>
<li>选项二</li>
<li>选项三</li>
</ul> <div id= "tabs2" >
<div>选项一内容</div>
<div>选项二内容</div>
<div>选项三内容</div>
</div> </div> </body> </html> |