Home > Web Front-end > JS Tutorial > Step by step parsing JavaScript to implement automatic tab switching function_javascript skills

Step by step parsing JavaScript to implement automatic tab switching function_javascript skills

WBOY
Release: 2016-05-16 15:18:26
Original
1266 people have browsed it

This article shares a tab function that enables automatic switching and gives its specific implementation process.

Everyone must be familiar with tabs. They are used very frequently. Usually, tabs need to be clicked or swiped to switch.
The code example is as follows:

<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" &#63; 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&#63;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 &#63; (this.aLi.length < eq &#63; eq = 0 : eq) : eq = 0;
 this.oEve(onEnd);
 this.onEnd.method == 'mouseover' &#63; this.method = "mouseover" : this.method = "click";
 this.onEnd.autoplay == 'stop' &#63; 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>
Copy after login

The above code achieves our requirements. Here is an introduction to its implementation process.
(1) Simulate the id selector in jQuery, the parameter is the id attribute value of the element

function $(id){
return typeof id === "string" ? document.getElementById(id) : id;
}

(2).function $$(oParent, elem){
return (oParent || document).getElementsByTagName(elem);
}, this function implements the object collection of all specific elements under the specified element.
(3). The function of this function is to store all elements with the class attribute value sClass under the oParent element into the array

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;
}
Copy after login

(4) Binding encapsulation of event processing functions to achieve browser compatibility.

.function addEvent(oElm, strEvent, fuc) {
 addEventListener&#63;oElm.addEventListener(strEvent,fuc,false) : oElm.attachEvent('on'+strEvent,fuc);
}
Copy after login

(5). This method implements basic initialization operations

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

(6). Implemented the function of merging objects, for example, you can merge the attributes in object a into object b

Object.extend = function(destination, source) {
 for (var property in source) {
  destination[property] = source[property];
 }
 return destination;
}
Copy after login

(7).Tab.prototype = {}, set the prototype object of the Tab constructor.
(8).initialize: function(obj, dis, content, onEnd, eq){}, this method can perform some initialization operations. The first parameter is the element id attribute value, the second parameter is the class style class, the third parameter is the class style class name of the content div, the fourth parameter is an object literal, which stores some related parameters. The five parameters specify which tab is selected by default, which is a number.
(9).this.obj = $(obj), get the specified element object.
(10).this.oList = $$$(this.obj, 'list')[0], get the title outer div element whose class attribute value is list.
(11).this.aCont = $$$(this.obj, content), obtains the collection of tab content elements.
(12).this.oUl = $$(this.oList, 'ul')[0], get the title ul element.
(13).this.aLi = this.oUl.children, get all child elements under the ul element.
(14).this.timer = null, used as the identifier of the timer function.
(15).eq ? (this.aLi.length < eq ? eq = 0 : eq) : eq = 0, if eq is 0, use 0, otherwise the value passed by eq will be used, and the eq value must be less than the array length , otherwise the eq value is set to 0.
(16).this.oEve(onEnd), overrides the default settings.
(17).this.onEnd.method == 'mouseover' ? this.method = "mouseover" : this.method = "click", determine whether it is a mouseover event or a click event.
(18).this.onEnd.autoplay == 'stop' ? this.autoplay = "stop" : this.autoplay = "play", the default is automatic play, otherwise it will not be automatic play.
(19).this.aCont[eq].style.display = 'block', the default content item is displayed.
(20).this.aLi[eq].className = 'hove', set the corresponding title tab style.
(21).this.display(dis), register the event handler function, the parameter is a style class name.
(22).this.autoPlayTab(eq, dis), execute this function to ensure that the tab can be automatically switched when automatic switching is allowed.
(23).

is used to merge objects

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

Copy after login

This is the default setting

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

If the onend object is passed, merge it into the default object, otherwise merge an empty object

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


Copy after login

(24).display: function(dis){}, register event processing function, the parameter is a style class name.
(25).var _this = this, assign this to the variable _this, why this is done will be introduced later. (26).var index = iNow = 0, perform some initialization operations.
(27).for(index=0;index<_this.aLi.length;index++){}, register the event processing function through for loop traversal.
(28)

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

使用匿名自执行函数,其实就是形成了一个闭包。
之所以用闭包,是为了隔离匿名函数内部的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);
}
Copy after login

(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++

}
Copy after login

不断调用此函数就实现了自动切换功能。
如果当前的索引等于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';
 }
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template