WordPress主題的jQuery應(yīng)用之TAB切換效果

首先在header.php的head標(biāo)簽中加載jQuery庫(kù)

1
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>

新建一個(gè)JS文件,在header.php的head標(biāo)簽中加載,JS文件中加入下例代碼:

1
2
3
4
5
6
7
8
9
$(document).ready(function(){
$(".entry-foot span:first").addClass("current");  // 為第一個(gè)span添加 .current 的樣式,默認(rèn)選中
$(".entry-foot ul:not(:first)").hide();  //ul 不是第一個(gè)時(shí)隱藏
$(".entry-foot span").mouseover(function(){  // 鼠標(biāo)移到 span 上時(shí)觸發(fā)函數(shù)
$(".entry-foot span").removeClass("current");  //為第一個(gè) span 移除 .current 樣式
$(this).addClass("current");  //為觸發(fā)的 span 添加樣式
$(".entry-foot ul").hide();  // 隱藏 ul
$("."+$(this).attr("id")).fadeIn("slow");  // 這句是核心,class(.) 和觸發(fā) span 的ID 一致的 fadeIn(漸顯)
});});

HTML代碼如下:

1
2
3
4
5
6
7
8
9
<div class="tab">
<p>
<span ID="tab1">tab1</span>
<span ID="tab2">tab2</span>
<span ID="tab3">tab3</span></p>
<ul class="tab1">以 LI 形式呈現(xiàn)的 tab1 的內(nèi)容</ul>
<ul class="tab2">以 LI 形式呈現(xiàn)的 tab2 的內(nèi)容</ul>
<ul class="tab3">以 LI 形式呈現(xiàn)的 tab3 的內(nèi)容</ul>
</div>

添加CSS代碼如下:

1
2
3
4
5
6
7
.entry-foot{background-color:#FAFAFA;margin:5px 8px;padding:5px 10px;}
.entry-foot p span{background-color:#EFEFEF;border:1px solid
#CCCCCC;cursor:pointer;margin-right:6px;padding:2px 5px;}
.entry-foot p span.current{background-color:#FAFAFA; border-bottom-color:#fafafa;}
.entry-foot p{border-bottom:1px solid #CCCCCC;font-weight:bold;padding:0 10px 2px;}
.entry-foot li{border-bottom:1px dotted #CCCCCC;padding-bottom:3px;margin:5px 0;}
.entry-foot .mhot,.entry-foot.allhot{display:none;}

原文:http://www.xiaorsz.com/jquery-realize-tab-switch-effect/

簡(jiǎn)潔純CSS的TAB

前幾天為做江陰印刷網(wǎng)的側(cè)邊TAB,找到了下面的代碼

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>簡(jiǎn)潔Tab</title>
<style TYPE="text/css">
<!--
body,div,ul,li{
margin:0 auto;
padding:0;
}
body{
font:12px "宋體";
text-align:center;
}
a:link{
color:#00F;
text-decoration:none;
}
a:visited {
color: #00F;
text-decoration:none;
}
a:hover {
color: #c00;
text-decoration:underline;
}
ul{
list-style:none;
}
.main{
clear:both;
padding:8px;
text-align:center;
}
/*第一種形式*/
#tabs0 {
height: 200px;
width: 400px;
border: 1px solid #cbcbcb;
background-color: #f2f6fb;
}
.menu0{
width: 400px;
}
.menu0 li{
display:block;
float: left;
padding: 4px 0;
width:100px;
text-align: center;
cursor:POINTER;
background: #FFFFff;
}
.menu0 li.hover{
background: #f2f6fb;
}
#main0 ul{
display: none;
}
#main0 ul.block{
display: block;
}
/*第二種形式*/
#tabs1{
text-align:left;
width:400px;
}
.menu1box{
position:relative;
overflow:hidden;
height:22px;
width:400px;
text-align:left;
}
#menu1{
position:absolute;
top:0;
left:0;
z-index:1;
}
#menu1 li{
float:left;
display:block;
cursor:POINTER;
width:72px;
text-align:center;
line-height:21px;
height:21px;
}
#menu1 li.hover{
background:#fff;
border-left:1px solid #333;
border-top:1px solid #333;
border-right:1px solid #333;
}
.main1box{
clear:both;
margin-top:-1px;
border:1px solid #333;
height:181px;
width:400px;
}
#main1 ul{
display: none;
}
#main1 ul.block{
display: block;
}
/*第三種形式*/
.menu2box{
position:relative;
overflow:hidden;
height:22px;
width:400px;
text-align:left;
background: #FFFFff;
}
#tabs2 {
height: 200px;
width: 400px;
border: 1px solid #cbcbcb;
background-color: #f2f6fb;
}
#tip2{
position:absolute;
top:0;
left:0;
height:22px;
line-height:22px;
z-index:0;
width:100px;
background: #f2f6fb;
}
#menu2{
position:absolute;
top:0;
left:0;
z-index:1;
}
#menu2 li{
display:block;
float: left;
padding: 4px 0;
width:100px;
text-align: center;
cursor:POINTER;
}
-->
</style>
<script>
<!--
/*第一種形式 第二種形式 更換顯示樣式*/
FUNCTION setTab(m,n){
var tli=document.getElementById("menu"+m).getElementsByTagName("li");
var mli=document.getElementById("main"+m).getElementsByTagName("ul");
FOR(i=0;i<tli.length;i++){
   tli[i].className=i==n?"hover":"";
   mli[i].style.display=i==n?"block":"none";
}
}
/*第三種形式 利用一個(gè)背景層定位*/
var m3={0:"",1:"評(píng)論內(nèi)容",2:"技術(shù)內(nèi)容",3:"點(diǎn)評(píng)內(nèi)容"}
FUNCTION nowtab(m,n){
IF(n!=0&&m3[0]=="")m3[0]=document.getElementById("main2").innerHTML;
document.getElementById("tip"+m).style.left=n*100+'px';
document.getElementById("main2").innerHTML=m3[n];
}
//-->
</script>
</head>
<body>
<br />
<br />
<!--第一種形式-->
<div ID="tabs0">
<ul class="menu0" ID="menu0">
   <li onclick="setTab(0,0)" class="hover">新聞</li>
   <li onclick="setTab(0,1)">評(píng)論</li>
   <li onclick="setTab(0,2)">技術(shù)</li>
   <li onclick="setTab(0,3)">點(diǎn)評(píng)</li>
</ul>
<div class="main" ID="main0">
   <ul class="block"><li>新聞列表</li></ul>
   <ul><li>評(píng)論列表</li></ul>
   <ul><li>技術(shù)列表</li></ul>
   <ul><li>點(diǎn)評(píng)列表</li></ul>
</div>
</div>
<br />
<br />
<!--第二種形式-->
<div ID="tabs1">
<div class="menu1box">
   <ul ID="menu1">
    <li class="hover" onmouseover="setTab(1,0)"><a href="#">新聞</a></li>
    <li onmouseover="setTab(1,1)"><a href="#">評(píng)論</a></li>
    <li onmouseover="setTab(1,2)"><a href="#">技術(shù)</a></li>
    <li onmouseover="setTab(1,3)"><a href="#">點(diǎn)評(píng)</a></li>
   </ul>
</div>
<div class="main1box">
   <div class="main" ID="main1">
    <ul class="block"><li>新聞列表</li></ul>
    <ul><li>評(píng)論列表</li></ul>
    <ul><li>技術(shù)列表</li></ul>
    <ul><li>點(diǎn)評(píng)列表</li></ul>
   </div>
</div>
</div>
<br />
<br />
<!--第三種形式-->
<div ID="tabs2">
<div class="menu2box">
   <div ID="tip2"></div>
   <ul ID="menu2">
    <li class="hover" onmouseover="nowtab(2,0)"><a href="#">新聞</a></li>
    <li onmouseover="nowtab(2,1)"><a href="#">評(píng)論</a></li>
    <li onmouseover="nowtab(2,2)"><a href="#">技術(shù)</a></li>
    <li onmouseover="nowtab(2,3)"><a href="#">點(diǎn)評(píng)</a></li>
   </ul>
</div>
   <div class="main" ID="main2">
新聞內(nèi)容
</div>
</div>
</body>
</html>

收藏自:http://kudrong.cn/article.asp?id=141

為自己的網(wǎng)頁(yè)制做Tab Pane

E-Spacy看到了漂亮的Tab Pane,于是也給自己的BLOG裝了個(gè)。

安裝方法是參照E-Spacy做的,效果見側(cè)欄 。

tabpane下載源代碼,解壓縮后,上傳到自己的網(wǎng)頁(yè)空間,推薦放到WP安裝目錄的wp-content\themes內(nèi)。

然后把此兩行代碼復(fù)制到模板文件的header.php內(nèi),head之間:

1
2
<script type="text/javascript" src="js/tabpane.js"></script>
<link type="text/css" rel="StyleSheet" href="css/tab.webfx.css" />

其中js/tabpane.jscss/tab.webfx.css部分改成自己的網(wǎng)絡(luò)鏈接地址,推薦使用

1
<?php bloginfo('template_directory'); ?>

這樣在更換空間時(shí)也不會(huì)出錯(cuò)。

繼續(xù)閱讀