分步解析JavaScript實現(xiàn)tab選項卡自動切換功能
本文分享一個能夠?qū)崿F(xiàn)自動切換的選項卡功能,并給出它的具體實現(xiàn)過程。
關(guān)于選項卡大家一定不會陌生,應(yīng)用非常的頻繁,通常選項卡都是需要點擊或者劃過才能夠?qū)崿F(xiàn)切換。
代碼實例如下:
<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>
上面的代碼實現(xiàn)了我們的要求,下面介紹一下它的實現(xiàn)過程。
(1)模擬實現(xiàn)jQuery中的id選擇器,參數(shù)是元素的id屬性值
function $(id){
return typeof id === "string" ? document.getElementById(id) : id;
}
(2).function $$(oParent, elem){
return (oParent || document).getElementsByTagName(elem);
},此函數(shù)實現(xiàn)了后去指定元素下所有特定元素的對象集合。
(3).此函數(shù)的功能是將oParent元素下所有class屬性值為sClass的元素存入數(shù)組
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)事件處理函數(shù)的綁定封裝,實現(xiàn)了瀏覽器兼容功能。
.function addEvent(oElm, strEvent, fuc) { addEventListener?oElm.addEventListener(strEvent,fuc,false) : oElm.attachEvent('on'+strEvent,fuc); }
(5).此方法實現(xiàn)了基本的初始化操作
function Tab(){ this.initialize.apply(this, arguments); }
(6).實現(xiàn)了合并對象的功能,比如可以將對象a中的屬性合并到對象b中
Object.extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } return destination; }
(7).Tab.prototype = {},設(shè)置Tab構(gòu)造函數(shù)的原型對象。
(8).initialize : function(obj, dis, content, onEnd, eq){},此方法可以進行一些初始化操作。第一個參數(shù)是元素id屬性值,第二個參數(shù)是class樣式類,第三個參數(shù)是內(nèi)容div的class樣式類名稱,第四個參數(shù)是一個對象直接量,里面存儲了一些相關(guān)參數(shù),第五個參數(shù)規(guī)定默認哪個選項卡被選中,是一個數(shù)字。
(9).this.obj = $(obj),獲取指定的元素對象。
(10).this.oList = $$$(this.obj, 'list')[0],獲取class屬性值為list的標題外層div元素。
(11).this.aCont = $$$(this.obj, content),獲取選項卡內(nèi)容元素集合。
(12).this.oUl = $$(this.oList, 'ul')[0],獲取標題ul元素。
(13).this.aLi = this.oUl.children,獲取ul元素下的所有子元素。
(14).this.timer = null,用作定時器函數(shù)的標識。
(15).eq ? (this.aLi.length < eq ? eq = 0 : eq) : eq = 0,如果eq是0則使用0,否則的話將使用eq傳遞的值,eq值要小于數(shù)組長度,否則eq值設(shè)置為0。
(16).this.oEve(onEnd),覆蓋默認設(shè)置。
(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',默認內(nèi)容項顯示。
(20).this.aLi[eq].className = 'hove',設(shè)置對應(yīng)的標題選項卡樣式。
(21).this.display(dis),注冊事件處理函數(shù),參數(shù)是一個樣式類名稱。
(22).this.autoPlayTab(eq, dis),執(zhí)行此函數(shù)確保在允許自動切換的時候選項卡可以自動切換。
(23).
用來進行對象合并
oEve: function(onEnd){ this.onEnd = { method: 'mouseover', autoplay: 'stop', }; Object.extend(this.onEnd, onEnd || {}); }
這是默認的設(shè)置
this.onEnd = { method: 'mouseover', autoplay: 'stop', }
如果傳遞了onend對象,就將其合并到默認對象,否則合并一個空對象
Object.extend(this.onEnd, onEnd || {})
(24).display : function(dis){},注冊事件處理函數(shù),參數(shù)是一個樣式類名稱。
(25).var _this = this,將this賦值給變量_this,為什么這么做后面會介紹。(26).var index = iNow = 0,進行一些初始化操作。
(27).for(index=0;index<_this.aLi.length;index++){},通過for循環(huán)遍歷的方式注冊事件處理函數(shù)。
(28)
.(function(){ var j = index; addEvent(_this.aLi[j], _this.method, function() { _this.fnClick(j,dis); _this.autoPlayTab(j, dis); }) })()
使用匿名自執(zhí)行函數(shù),其實就是形成了一個閉包。
之所以用閉包,是為了隔離匿名函數(shù)內(nèi)部的index值和外部的index值。
之所以將this賦值給_this是因為,事件處理函數(shù)中的this是指向元素li的,而不是指向Tab()構(gòu)造函數(shù)的對象實例。
(29).autoPlayTab : function(iNow, dis){},此函數(shù)實現(xiàn)了自動切換功能,第一個參數(shù)規(guī)定當前選項卡切換所處的索引位置,第二個參數(shù)一個樣式類名稱,就是設(shè)置處于當前狀態(tài)的樣式。
(30).if(this.autoplay == 'play'){},判斷是否允許自動切換。
(31).var _this = this,將this賦值給變量_this,原理和上面是一樣的。
(32).this.iNow = iNow,進行賦值操作。
(33).this.obj.onmouseover = function(){
clearInterval(_this.timer);
},當鼠標懸浮的時候的時候停止定時器函數(shù)的執(zhí)行,其實也就是停止自動切換。
(34).當鼠標離開的時候,開始自動切換動作
this.obj.onmouseout = function(){ _this.timer = setInterval(playTab,5000); }
(35).clearInterval(_this.timer),停止以前的定時器函數(shù)執(zhí)行。
(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++ }
不斷調(diào)用此函數(shù)就實現(xiàn)了自動切換功能。
如果當前的索引等于li元素的個數(shù),那么就將其設(shè)置為0,也就是從頭進行新一輪切換。
然后調(diào)用對應(yīng)的方法,并且讓索引值自增。
(38)實現(xiàn)了選項卡的切換顯示隱藏和樣式設(shè)置效果
.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'; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
javascript+html5實現(xiàn)仿flash滾動播放圖片的方法
這篇文章主要介紹了javascript+html5實現(xiàn)仿flash滾動播放圖片的方法,可實現(xiàn)類似Flash滾動播放的效果,非常具有實用價值,需要的朋友可以參考下2015-04-04用 JavaScript 給站外鏈接的 cursor 進行改造
用 JavaScript 給站外鏈接的 cursor 進行改造...2006-12-12JavaScript中有關(guān)一個數(shù)組中最大值和最小值及它們的下表的輸出的解決辦法
這篇文章主要介紹了JavaScript中有關(guān)一個數(shù)組中最大值和最小值及它們的下表的輸出的一種解決辦法,本文還給大家介紹了js快速獲取數(shù)組中最大值和最小值的方法,非常不錯,需要的朋友可以參考下2016-07-07JS判斷輸入字符串長度實例代碼(漢字算兩個字符,字母數(shù)字算一個)
下面小編就為大家?guī)硪黄狫S判斷輸入字符串長度實例代碼(漢字算兩個字符,字母數(shù)字算一個)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08