原生javascript實現(xiàn)Tab選項卡切換功能
分析個人用原生JS獲取類名元素的代碼:
getByClassName:function(className,parent){
var elem = [],
node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
p = new RegExp("(^|\\s)"+className+"(\\s|$)");
for(var n=0,i=node.length;n<i;n++){
if(p.test(node[n].className)){
elem.push(node[n]);
}
}
return elem;
}
parent參數(shù)是可選的,但需要先判斷它是否存在,且是節(jié)點dom元素 parent != undefined&&parent.nodeType==1 ,nodeType == 1可以判斷節(jié)點是否為dom元素,在火狐瀏覽器里面,空白也算是節(jié)點(.childnodes),用這個屬性就判斷是否為dom元素,排除空白符.
移除元素的類名:
var cur = new RegExp(this.sCur,'g'); //this.sCur就是類名,這里是用變量保存 如:this.sCur = "cur";
this.oTab_btn[n].className = this.oTab_btn[n].className.replace(cur,'');
調(diào)用例子:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
body,p,ul,li {padding: 0;margin: 0;}
ul {list-style: none;}
h3 {padding: 5px;background-color: #999;margin-bottom: 10px;}
pre {border: 1px dotted #000;}
.explan {padding: 10px;color: #333;line-height: 1.6;}
.box {width: 300px;height:100px;border: 1px solid #ccc;}
.box ul{height: 30px;line-height: 30px;}
.box ul li {float: left;display: inline;width: 150px;text-align: center;background-color: #eee;cursor: pointer;}
.box .tab-cur {background-color: #000;color: #fff;}
.box p {display: none;padding: 30px;}
/*tabB*/
#tabB {width: 450px;}
.box .tab-cur02 {background-color: #025023;}
</style>
</head>
<body>
<div class="explan">
<strong>使用閱讀 :</strong> <br>
{'tabBtn':'#tabA .tab-i','tabCon':'#tabA .tab-c','cur':'tab-cur'} 【必選】 <br>
(1)'tabBtn':'#tabA .tab-i','tabCon':'#tabA .tab-c' 選擇器:只支持 #id .className,(ID + 空格 + 類名) 【必選】<br>
?。?)'cur':'tab-cur'(默認) :為切換按鈕當(dāng)前狀態(tài)(類名)【必選】<br>
?。?)'type':'mouseover'|| 'clicl' 默認是點擊 【可選】
</div>
<h3>tabA</h3>
<pre>new LGY_tab({'tabBtn':'#tabA .tab-i',
'tabCon':'#tabA .tab-c',
'cur':'tab-cur'
});
</pre>
<div class="box" id="tabA">
<ul>
<li class="tab-i">btn-A</li>
<li class="tab-i">btn-B</li>
</ul>
<p class="tab-c">con-A</p>
<p class="tab-c">con-B</p>
</div>
<h3>tabB</h3>
<pre>new LGY_tab({'tabBtn':'#tabB .tab-i',
'tabCon':'#tabB .tab-k',
'cur':'tab-cur02',
'type':'mouseover'
});
</pre>
<div class="box" id="tabB">
<ul>
<li class="tab-i">btn-A</li>
<li class="tab-i">btn-B</li>
<li class="tab-i">btn-C</li>
</ul>
<p class="tab-k">con-A</p>
<p class="tab-k">con-B</p>
<p class="tab-k">con-C</p>
</div>
<script type="text/javascript" src="下方的代碼段.js"></script>
<script type="text/javascript">
//
new LGY_tab({'tabBtn':'#tabA .tab-i',
'tabCon':'#tabA .tab-c',
'cur':'tab-cur'
});
//
new LGY_tab({'tabBtn':'#tabB .tab-i',
'tabCon':'#tabB .tab-k',
'cur':'tab-cur02',
'type':'mouseover'
});
//test
//
new LGY_tab({'tabBtn':'#tabB .tab-j',
'tabCon':'#tabB .tab-k',
'cur':'tab-cur02',
'type':'mouseover'
});
</script>
</body>
</html>
JS詳細代碼:
function LGY_tab(option){
this.oTab_btn = this.getDom(option.tabBtn);//切換點擊的元素
this.oTab_clist = this.getDom(option.tabCon); //切換的內(nèi)容
if(!this.oTab_btn || !this.oTab_clist) return;
this.sCur = option.cur; //激活的狀態(tài)
this.type = option.type || 'click';
this.nLen = this.oTab_btn.length;
this.int();
}
LGY_tab.prototype = {
getId:function(id){
return document.getElementById(id);
},
getByClassName:function(className,parent){
var elem = [],
node = parent != undefined&&parent.nodeType==1?parent.getElementsByTagName('*'):document.getElementsByTagName('*'),
p = new RegExp("(^|\\s)"+className+"(\\s|$)");
for(var n=0,i=node.length;n<i;n++){
if(p.test(node[n].className)){
elem.push(node[n]);
}
}
return elem;
},
getDom:function(s){
var nodeName = s.split(' '),
p = this.getId(nodeName[0].slice(1)),
c = this.getByClassName(nodeName[1].slice(1),p);
if(!p || c.length==0) return null;
return c;
},
change:function(){
var cur = new RegExp(this.sCur,'g');
for(var n=0;n<this.nLen;n++){
this.oTab_clist[n].style.display = 'none';
this.oTab_btn[n].className = this.oTab_btn[n].className.replace(cur,'');
}
},
int:function(){
var that = this;
this.oTab_btn[0].className += ' '+this.sCur;
this.oTab_clist[0].style.display = 'block';
for(var n=0;n<this.nLen;n++){
this.oTab_btn[n].index = n;
this.oTab_btn[n]['on'+this.type] = function(){
that.change();
that.oTab_btn[this.index].className +=' ' + that.sCur;
that.oTab_clist[this.index].style.display = 'block';
}
}
}
}
最終效果圖展示:
效果是不是很棒呢,而且兼容性也不錯,代碼也很簡潔,完全可以替代龐大的jQuery選項卡切換插件了。
- js基于面向?qū)ο髮崿F(xiàn)網(wǎng)頁TAB選項卡菜單效果代碼
- js簡單實現(xiàn)豎向tab選項卡的方法
- JS+CSS實現(xiàn)的經(jīng)典tab選項卡效果代碼
- 原生js實現(xiàn)tab選項卡切換
- JS實現(xiàn)同一個網(wǎng)頁布局滑動門和TAB選項卡實例
- js實現(xiàn)類似菜單風(fēng)格的TAB選項卡效果代碼
- jquery插件tytabs.jquery.min.js實現(xiàn)漸變TAB選項卡效果
- JS實現(xiàn)不規(guī)則TAB選項卡效果代碼
- JS基于面向?qū)ο髮崿F(xiàn)的多個倒計時器功能示例
- JS基于面向?qū)ο髮崿F(xiàn)的選項卡效果示例
- 學(xué)習(xí)javascript面向?qū)ο?實例講解面向?qū)ο筮x項卡
- JS使用面向?qū)ο蠹夹g(shù)實現(xiàn)的tab選項卡效果示例
相關(guān)文章
微信小程序項目實踐之九宮格實現(xiàn)及item跳轉(zhuǎn)功能
這篇文章主要介紹了微信小程序項目實踐之九宮格實現(xiàn)及item跳轉(zhuǎn)功能,需要的朋友可以參考下2018-07-07遍歷json 對象的屬性并且動態(tài)添加屬性的實現(xiàn)
下面小編就為大家?guī)硪黄闅vjson 對象的屬性并且動態(tài)添加屬性的實現(xiàn)。小編覺的挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12