分步解析JavaScript实现tab选项卡自动切换功能_javascript技巧

本文分享一个能够实现自动切换的选项卡功能,并给出它的具体实现过程。

关于选项卡大家一定不会陌生,应用非常的频繁,通常选项卡都是需要点击或者划过才能够实现切换。
代码实例如下:

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

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索javascript
, 切换
tab
javascript tab选项卡、tab选项卡、微信小程序 tab选项卡、mui tab选项卡切换、tabcontrol隐藏选项卡,以便于您获取更多的相关知识。

时间: 2024-07-29 07:23:15

分步解析JavaScript实现tab选项卡自动切换功能_javascript技巧的相关文章

JavaScript表单焦点自动切换代码_javascript技巧

废话不多说,关键代码如下所示: ---恢复内容开始--- <html> <head> <script> window.onload=function(){ var form=document.getElementsByTagName('form')[0]; var txt=form.elements['txt1']; var txt1=form.elements['txt2']; txt.onkeyup=function(){ if(this.value.length=

javascript实现tab响应式切换特效_javascript技巧

本文实例讲解了tab响应式切换效果,利用js对样式进行动态切换即可. 多的不说,请看代码 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0,

基于JavaScript实现移动端TAB触屏切换效果_javascript技巧

展示效果图如下所示: 效果演示 源码下载 我们使用移动端时可以通过触屏手势左右滑动来切换TAB栏目,如网易新闻等APP栏目切换.我们说的TAB一般由导航条和TAB对应的内容组成,切换导航条上的标签同时标签对应的内容也会跟着切换.本文将结合实例给大家介绍一个移动端TAB触屏切换效果. HTML 我们准备一个TAB导航#pagenavi,里面包含TAB导航要切换的四个导航按钮,然后是切换的主体内容#slider,这里应该放置四个li与导航按钮对应,内容自定义. <div class="box-

JavaScript tab选项卡插件实例代码_javascript技巧

今天,先从最简单的开始,将已有的一个Tab选项卡切换功能改写成javascript插件形式. 原生函数写法 将一个javascript方法改写为js插件最简单的方式就是将这个方法挂载到window全局对象下面 我们先来看看最原始的使用函数写法的代码: tab.html <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <meta http

Bootstrap选项卡动态切换效果_javascript技巧

最近在写一个系统的首页,就是平常的一个顶部导航栏,上面有登录和注册两个按钮,点击按钮弹出相应的登录或注册框,为了方便交互,把登录和注册在一个选项卡里放着,每次用户不用回到顶部去点击,只需要在当前框点击就可切换,先看一下样子吧,如图所示: 用Bootstrap平时在写静态页面时,只需要把.active类给自己想要第一个展现的框就可以,而动态的时候并不能简单的在js代码中给自己想要第一个展现的框直接设置.active,这样当切换时第一个设置为active的一直在界面中存在,所以我们需要写js代码,给

JS实现淡蓝色简洁竖向Tab点击切换效果_javascript技巧

本文实例讲述了JS实现淡蓝色简洁竖向Tab点击切换效果.分享给大家供大家参考.具体如下: 这里介绍一款淡蓝色竖向简洁Tab,选项卡,是很多人都喜欢的一种风格. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-blue-v-tab-cha-style-menu-codes/ 具体代码如下: <html> <head> <meta http-equiv="Content-Type" content=&qu

js基于面向对象实现网页TAB选项卡菜单效果代码_javascript技巧

本文实例讲述了js基于面向对象实现网页TAB选项卡菜单效果代码.分享给大家供大家参考.具体如下: 这是一款自动的网页TAB,基于面向对象的选项卡菜单,由于时间关系只做了简单的实现,界面没有美化,不多做介绍了. 先来看看运行效果截图: 在线演示地址如下: http://demo.jb51.net/js/2015/js-mxdx-tab-cha-style-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio

JavaScript实现的背景自动变色代码_javascript技巧

本文实例讲述了JavaScript实现的背景自动变色代码.分享给大家供大家参考,具体如下: 这里演示JavaScript实现网页背景自动变色,自己变换颜色,设定时间和颜色值即可,在你设定的颜色值.一定时间内自动切换网页背景颜色. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-auto-cha-bg-color-demo/ 具体代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Tra

JS实现滑动菜单效果代码(包括Tab,选项卡,横向等效果)_javascript技巧

本文实例讲述了JS实现滑动菜单效果代码.分享给大家供大家参考.具体如下: 这里实现一个特效将网页中的选项卡滑动门都集中到一个网页中来,有些同志曾经为同一个页面布置两个滑动门而烦恼,参考一下本例子,相信你会找到答案,而且有各种排列方式的选项卡,总有一款会满足你. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-move-tab-nav-menu-demo-style-codes/ 具体代码如下: <!DOCTYPE html PUBLIC &qu