最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

分步解析JavaScript實(shí)現(xiàn)tab選項(xiàng)卡自動(dòng)切換功能

 更新時(shí)間:2016年01月25日 08:43:03   作者:gogoggo  
這篇文章主要分步解析JavaScript實(shí)現(xiàn)tab選項(xiàng)卡自動(dòng)切換功能代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文分享一個(gè)能夠?qū)崿F(xiàn)自動(dòng)切換的選項(xiàng)卡功能,并給出它的具體實(shí)現(xiàn)過(guò)程。

關(guān)于選項(xiàng)卡大家一定不會(huì)陌生,應(yīng)用非常的頻繁,通常選項(xiàng)卡都是需要點(diǎn)擊或者劃過(guò)才能夠?qū)崿F(xiàn)切換。
代碼實(shí)例如下:

<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">只有努力才會(huì)有美好的未來(lái)</div>
</div>
</body>
</html>

上面的代碼實(shí)現(xiàn)了我們的要求,下面介紹一下它的實(shí)現(xiàn)過(guò)程。
(1)模擬實(shí)現(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ù)實(shí)現(xiàn)了后去指定元素下所有特定元素的對(duì)象集合。
(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ù)的綁定封裝,實(shí)現(xiàn)了瀏覽器兼容功能。

.function addEvent(oElm, strEvent, fuc) {
 addEventListener?oElm.addEventListener(strEvent,fuc,false) : oElm.attachEvent('on'+strEvent,fuc);
}

(5).此方法實(shí)現(xiàn)了基本的初始化操作

function Tab(){ this.initialize.apply(this, arguments);
}

(6).實(shí)現(xiàn)了合并對(duì)象的功能,比如可以將對(duì)象a中的屬性合并到對(duì)象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ù)的原型對(duì)象。
(8).initialize : function(obj, dis, content, onEnd, eq){},此方法可以進(jìn)行一些初始化操作。第一個(gè)參數(shù)是元素id屬性值,第二個(gè)參數(shù)是class樣式類(lèi),第三個(gè)參數(shù)是內(nèi)容div的class樣式類(lèi)名稱(chēng),第四個(gè)參數(shù)是一個(gè)對(duì)象直接量,里面存儲(chǔ)了一些相關(guān)參數(shù),第五個(gè)參數(shù)規(guī)定默認(rèn)哪個(gè)選項(xiàng)卡被選中,是一個(gè)數(shù)字。
(9).this.obj = $(obj),獲取指定的元素對(duì)象。
(10).this.oList = $$$(this.obj, 'list')[0],獲取class屬性值為list的標(biāo)題外層div元素。
(11).this.aCont = $$$(this.obj, content),獲取選項(xiàng)卡內(nèi)容元素集合。
(12).this.oUl = $$(this.oList, 'ul')[0],獲取標(biāo)題ul元素。
(13).this.aLi = this.oUl.children,獲取ul元素下的所有子元素。
(14).this.timer = null,用作定時(shí)器函數(shù)的標(biāo)識(shí)。
(15).eq ? (this.aLi.length < eq ? eq = 0 : eq) : eq = 0,如果eq是0則使用0,否則的話將使用eq傳遞的值,eq值要小于數(shù)組長(zhǎng)度,否則eq值設(shè)置為0。
(16).this.oEve(onEnd),覆蓋默認(rèn)設(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",默認(rèn)是自動(dòng)播放,否則就不是自動(dòng)播放。
(19).this.aCont[eq].style.display = 'block',默認(rèn)內(nèi)容項(xiàng)顯示。
(20).this.aLi[eq].className = 'hove',設(shè)置對(duì)應(yīng)的標(biāo)題選項(xiàng)卡樣式。
(21).this.display(dis),注冊(cè)事件處理函數(shù),參數(shù)是一個(gè)樣式類(lèi)名稱(chēng)。
(22).this.autoPlayTab(eq, dis),執(zhí)行此函數(shù)確保在允許自動(dòng)切換的時(shí)候選項(xiàng)卡可以自動(dòng)切換。
(23).

用來(lái)進(jìn)行對(duì)象合并

oEve: function(onEnd){
 this.onEnd = {
  method: 'mouseover',
  autoplay: 'stop',
 };
 Object.extend(this.onEnd, onEnd || {});
}

這是默認(rèn)的設(shè)置

this.onEnd = {
 method: 'mouseover',
 autoplay: 'stop',
}

如果傳遞了onend對(duì)象,就將其合并到默認(rèn)對(duì)象,否則合并一個(gè)空對(duì)象

Object.extend(this.onEnd, onEnd || {})


(24).display : function(dis){},注冊(cè)事件處理函數(shù),參數(shù)是一個(gè)樣式類(lèi)名稱(chēng)。
(25).var _this = this,將this賦值給變量_this,為什么這么做后面會(huì)介紹。(26).var index = iNow = 0,進(jìn)行一些初始化操作。
(27).for(index=0;index<_this.aLi.length;index++){},通過(guò)for循環(huán)遍歷的方式注冊(cè)事件處理函數(shù)。
(28)

.(function(){ var j = index;
 addEvent(_this.aLi[j], _this.method,
 function() {
  _this.fnClick(j,dis);
  _this.autoPlayTab(j, dis);
 })
})()

使用匿名自執(zhí)行函數(shù),其實(shí)就是形成了一個(gè)閉包。
之所以用閉包,是為了隔離匿名函數(shù)內(nèi)部的index值和外部的index值。
之所以將this賦值給_this是因?yàn)椋录幚砗瘮?shù)中的this是指向元素li的,而不是指向Tab()構(gòu)造函數(shù)的對(duì)象實(shí)例。
(29).autoPlayTab : function(iNow, dis){},此函數(shù)實(shí)現(xiàn)了自動(dòng)切換功能,第一個(gè)參數(shù)規(guī)定當(dāng)前選項(xiàng)卡切換所處的索引位置,第二個(gè)參數(shù)一個(gè)樣式類(lèi)名稱(chēng),就是設(shè)置處于當(dāng)前狀態(tài)的樣式。
(30).if(this.autoplay == 'play'){},判斷是否允許自動(dòng)切換。
(31).var _this = this,將this賦值給變量_this,原理和上面是一樣的。
(32).this.iNow = iNow,進(jìn)行賦值操作。
(33).this.obj.onmouseover = function(){
  clearInterval(_this.timer);
},當(dāng)鼠標(biāo)懸浮的時(shí)候的時(shí)候停止定時(shí)器函數(shù)的執(zhí)行,其實(shí)也就是停止自動(dòng)切換。

(34).當(dāng)鼠標(biāo)離開(kāi)的時(shí)候,開(kāi)始自動(dòng)切換動(dòng)作

this.obj.onmouseout = function(){
 _this.timer = setInterval(playTab,5000);
}

(35).clearInterval(_this.timer),停止以前的定時(shí)器函數(shù)執(zhí)行。
(36)._this.timer = setInterval(playTab,5000),開(kāi)始自動(dòng)切換。
(37).

function playTab(){
 if(_this.iNow == _this.aLi.length)_this.iNow = 0;
 _this.fnClick(_this.iNow, dis)
  _this.iNow++

}

不斷調(diào)用此函數(shù)就實(shí)現(xiàn)了自動(dòng)切換功能。
如果當(dāng)前的索引等于li元素的個(gè)數(shù),那么就將其設(shè)置為0,也就是從頭進(jìn)行新一輪切換。
然后調(diào)用對(duì)應(yīng)的方法,并且讓索引值自增。

(38)實(shí)現(xiàn)了選項(xiàng)卡的切換顯示隱藏和樣式設(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';
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論

南丹县| 鹿泉市| 蚌埠市| 微山县| 香港 | 怀集县| 桂平市| 灵川县| 雷山县| 都江堰市| 云龙县| 荥经县| 固始县| 青州市| 安徽省| 肃宁县| 丹棱县| 古田县| 惠来县| 濮阳市| 高邑县| 涿鹿县| 高州市| 武强县| 诏安县| 安多县| 平定县| 库伦旗| 黄山市| 廉江市| 富宁县| 景泰县| 图们市| 芒康县| 富平县| 永平县| 固原市| 云安县| 河池市| 百色市| 安溪县|