分步解析JavaScript實現tab選項卡自動切換功能,javascripttab

來源:互聯網
上載者:User

分步解析JavaScript實現tab選項卡自動切換功能,javascripttab

本文分享一個能夠實現自動切換的選項卡功能,並給出它的具體實現過程。

關於選項卡大家一定不會陌生,應用非常的頻繁,通常選項卡都是需要點擊或者划過才能夠實現切換。
代碼執行個體如下:

<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對象,就將其合并到預設對象,否則合并一個Null 物件

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'; }

以上就是本文的全部內容,希望對大家的學習有所協助。

您可能感興趣的文章:
  • js 多層疊的TAB選項卡
  • 一個精簡的JS DIV層tab切換代碼
  • 基於jquery的tab切換 js原理
  • 一個js的tab轉場效果代碼[代碼分離]
  • js實現tab選項卡函數代碼
  • 純php打造的tab選項卡效果代碼(不用js)
  • Javascript 自適應高度的Tab選項卡
  • 跨瀏覽器通用、可重用的選項卡tab切換js代碼
  • javascript實現tabs選項卡轉場效果(自寫原生js)
  • javascript實現tabs選項卡轉場效果(擴充版)
  • Extjs 3.3切換tab隱藏相應工具列出現空白解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.