本文分享一個能夠實現自動切換的選項卡功能,並給出它的具體實現過程。
關於選項卡大家一定不會陌生,應用非常的頻繁,通常選項卡都是需要點擊或者劃過才能夠實現切換。
代碼實例如下:
<html>
<head>
<meta charset=" utf-8">
<title>tab切換</title>
<style type="text/css">
body,h2,p{
margin:0px;
padding:0px;
}ul,li{
margin:0px;
padding:0px;
float:left;
list-style-type:none;
}
body{font-size:12px;}
.box{
width:722px;
height:99px;
margin:10px auto;
border:1px solid #dedede;
}
.list{
width:711px;
height:22px;
float:left;
padding:4px 0 0 9px;
border-top:1px solid #fff;
border-left:1px solid #fff;
border-right:1px solid #fff;
}
.list li{
width:74px;
height:22px;
float:left;
cursor:pointer;
color:#656565;
line-height:22px;
text-align:center;
}
.list li.hove{
width:72px;
height:20px;
color:#fc6703;
line-height:20px;
border-top:1px solid #dedede;
border-left:1px solid #dedede;
border-right:1px solid #dedede;
border-bottom:1px solid #fff;
background:#fff;
}
.content{
width:722px;
height:72px;
float:left;
display:none;
}
</style>
<script>
function $(id){
return typeof id === "string" ? document.getElementById(id) : id;
}
function $$(oParent, elem){
return (oParent || document).getElementsByTagName(elem);
}
function $$$(oParent, sClass){
var aElem = $$(oParent, '*');
var aClass = [];
var index = 0;
for(index=0;index<aElem.length;index++){
if(aElem[index].className == sClass){
aClass.push(aElem[index]);
}
}
return aClass;
}
function addEvent(oElm, strEvent, fuc) {
addEventListener?oElm.addEventListener(strEvent,fuc,false):oElm.attachEvent('on'+strEvent,fuc);
};
function Tab(){
this.initialize.apply(this, arguments);
}
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
Tab.prototype = {
initialize : function(obj, dis, content, onEnd, eq){
this.obj = $(obj);
this.oList = $$$(this.obj, 'list')[0];
this.aCont = $$$(this.obj, content);
this.oUl = $$(this.oList, 'ul')[0];
this.aLi = this.oUl.children;
this.timer = null;
eq ? (this.aLi.length < eq ? eq = 0 : eq) : eq = 0;
this.oEve(onEnd);
this.onEnd.method == 'mouseover' ? this.method = "mouseover" : this.method = "click";
this.onEnd.autoplay == 'stop' ? this.autoplay = "stop" : this.autoplay = "play";
this.aCont[eq].style.display = 'block';
this.aLi[eq].className = 'hove';
this.display(dis);
this.autoPlayTab(eq, dis);
},
oEve: function(onEnd){
this.onEnd = {
method: 'mouseover',
autoplay: 'stop',
};
Object.extend(this.onEnd, onEnd || {});
},
display : function(dis){
var _this = this;
var index = iNow = 0;
for(index=0;index<_this.aLi.length;index++){
(function(){
var j = index;
addEvent(_this.aLi[j], _this.method,
function() {
_this.fnClick(j,dis);
_this.autoPlayTab(j, dis);
})
})()
}
},
autoPlayTab : function(iNow, dis){
if(this.autoplay == 'play'){
var _this = this;
this.iNow = iNow;
this.obj.onmouseover = function(){
clearInterval(_this.timer);
};
this.obj.onmouseout = function(){
_this.timer = setInterval(playTab,5000);
};
clearInterval(_this.timer);
_this.timer = setInterval(playTab,5000);
function playTab(){
if(_this.iNow == _this.aLi.length)_this.iNow = 0;
_this.fnClick(_this.iNow, dis)
_this.iNow++
}
}
},
fnClick : function(oBtn, dis){
var index = 0;
for(index=0;index<this.aLi.length;index++){
this.aLi[index].className = '';
this.aCont[index].style.display = 'none';
}
this.aLi[oBtn].className = dis;
this.aCont[oBtn].style.display = 'block';
}
};
window.onload = function(){
new Tab("box", 'hove', 'content', {
method : 'mouseover',
autoplay : 'play'
},0);
};
</script>
</head>
<body>
<div id="box" class="box">
<div class="list">
<ul>
<li>div教程</li>
<li>jquery教程</li>
<li>css教程</li>
</ul>
</div>
<div class="content">螞蟻部落歡迎您</div>
<div class="content">本站url地址是softwhy.com</div>
<div class="content">只有努力才會有美好的未來</div>
</div>
</body>
</html>
上面的代碼實現了我們的要求,下面介紹一下它的實現過程。
(1)模擬實現jQuery中的id選擇器,參數是元素的id屬性值
function $(id){
return typeof id === "string" ? document.getElementById(id) : id;
}
(2).function $$(oParent, elem){
return (oParent || document).getElementsByTagName(elem);
},此函數實現了後去指定元素下所有特定元素的對象集合。
(3).此函數的功能是將oParent元素下所有class屬性值為sClass的元素存入數組
function $$$(oParent, sClass){
var aElem = $$(oParent, '*');
var aClass = [];
var index = 0;
for(index=0;index<aElem.length;index++){
if(aElem[index].className == sClass){
aClass.push(aElem[index]);
}
}
return aClass;
}
(4)事件處理函數的綁定封裝,實現了浏覽器兼容功能。
.function addEvent(oElm, strEvent, fuc) {
addEventListener?oElm.addEventListener(strEvent,fuc,false) : oElm.attachEvent('on'+strEvent,fuc);
}
(5).此方法實現了基本的初始化操作
function Tab(){ this.initialize.apply(this, arguments);
}
(6).實現了合並對象的功能,比如可以將對象a中的屬性合並到對象b中
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
(7).Tab.prototype = {},設置Tab構造函數的原型對象。
(8).initialize : function(obj, dis, content, onEnd, eq){},此方法可以進行一些初始化操作。第一個參數是元素id屬性值,第二個參數是class樣式類,第三個參數是內容div的class樣式類名稱,第四個參數是一個對象直接量,裡面存儲了一些相關參數,第五個參數規定默認哪個選項卡被選中,是一個數字。
(9).this.obj = $(obj),獲取指定的元素對象。
(10).this.oList = $$$(this.obj, 'list')[0],獲取class屬性值為list的標題外層div元素。
(11).this.aCont = $$$(this.obj, content),獲取選項卡內容元素集合。
(12).this.oUl = $$(this.oList, 'ul')[0],獲取標題ul元素。
(13).this.aLi = this.oUl.children,獲取ul元素下的所有子元素。
(14).this.timer = null,用作定時器函數的標識。
(15).eq ? (this.aLi.length < eq ? eq = 0 : eq) : eq = 0,如果eq是0則使用0,否則的話將使用eq傳遞的值,eq值要小於數組長度,否則eq值設置為0。
(16).this.oEve(onEnd),覆蓋默認設置。
(17).this.onEnd.method == 'mouseover' ? this.method = "mouseover" : this.method = "click",判斷是mouseover事件還是click事件。
(18).this.onEnd.autoplay == 'stop' ? this.autoplay = "stop" : this.autoplay = "play",默認是自動播放,否則就不是自動播放。
(19).this.aCont[eq].style.display = 'block',默認內容項顯示。
(20).this.aLi[eq].className = 'hove',設置對應的標題選項卡樣式。
(21).this.display(dis),注冊事件處理函數,參數是一個樣式類名稱。
(22).this.autoPlayTab(eq, dis),執行此函數確保在允許自動切換的時候選項卡可以自動切換。
(23).
用來進行對象合並
oEve: function(onEnd){
this.onEnd = {
method: 'mouseover',
autoplay: 'stop',
};
Object.extend(this.onEnd, onEnd || {});
}
這是默認的設置
this.onEnd = {
method: 'mouseover',
autoplay: 'stop',
}
如果傳遞了onend對象,就將其合並到默認對象,否則合並一個空對象
Object.extend(this.onEnd, onEnd || {})
(24).display : function(dis){},注冊事件處理函數,參數是一個樣式類名稱。
(25).var _this = this,將this賦值給變量_this,為什麼這麼做後面會介紹。(26).var index = iNow = 0,進行一些初始化操作。
(27).for(index=0;index<_this.aLi.length;index++){},通過for循環遍歷的方式注冊事件處理函數。
(28)
.(function(){ var j = index;
addEvent(_this.aLi[j], _this.method,
function() {
_this.fnClick(j,dis);
_this.autoPlayTab(j, dis);
})
})()
使用匿名自執行函數,其實就是形成了一個閉包。
之所以用閉包,是為了隔離匿名函數內部的index值和外部的index值。
之所以將this賦值給_this是因為,事件處理函數中的this是指向元素li的,而不是指向Tab()構造函數的對象實例。
(29).autoPlayTab : function(iNow, dis){},此函數實現了自動切換功能,第一個參數規定當前選項卡切換所處的索引位置,第二個參數一個樣式類名稱,就是設置處於當前狀態的樣式。
(30).if(this.autoplay == 'play'){},判斷是否允許自動切換。
(31).var _this = this,將this賦值給變量_this,原理和上面是一樣的。
(32).this.iNow = iNow,進行賦值操作。
(33).this.obj.onmouseover = function(){
clearInterval(_this.timer);
},當鼠標懸浮的時候的時候停止定時器函數的執行,其實也就是停止自動切換。
(34).當鼠標離開的時候,開始自動切換動作
this.obj.onmouseout = function(){
_this.timer = setInterval(playTab,5000);
}
(35).clearInterval(_this.timer),停止以前的定時器函數執行。
(36)._this.timer = setInterval(playTab,5000),開始自動切換。
(37).
function playTab(){
if(_this.iNow == _this.aLi.length)_this.iNow = 0;
_this.fnClick(_this.iNow, dis)
_this.iNow++
}
不斷調用此函數就實現了自動切換功能。
如果當前的索引等於li元素的個數,那麼就將其設置為0,也就是從頭進行新一輪切換。
然後調用對應的方法,並且讓索引值自增。
(38)實現了選項卡的切換顯示隱藏和樣式設置效果
.fnClick : function(oBtn, dis){
var index = 0;
for(index=0;index<this.aLi.length;index++){
this.aLi[index].className = '';
this.aCont[index].style.display = 'none';
}
this.aLi[oBtn].className = dis;
this.aCont[oBtn].style.display = 'block';
}
以上就是本文的全部內容,希望對大家的學習有所幫助。