一些公共的底层的JS方法

var GLOBAL = {};
GLOBAL.namespace = function (str) {
var arr = str.split('.'), o = GLOBAL;
for (var i = (arr[0] == 'GLOBAL') ? 1 : 0; i < arr.length; i++) {
o[arr[i]] = o[arr[i]] || {};
o = o[arr[i]];
}
}
GLOBAL.namespace('Dom');
GLOBAL.Dom.getElementsByClassName = function (str, root, tag) {
if (root) {
root = typeof root == 'string' ? document.getElementById(root) : root;
} else {
root = document.body;
}
tag = tag || "*";
var els = root.getElementsByTagName(tag), arr = [];
for (var i = 0, n = els.length; i < n; i++) {
for (var j = 0, k = els[i].className.split(' '), l = k.length; j < l; j++) {
if (k[j] == str) {
arr.push(els[i]);
break;
}
}
}
return arr;
}
GLOBAL.Dom.addClass = function (node, str) {
if (!new RegExp("(^|\\s+)" + str).test(node.className)) {
node.className = node.className + " " + str;
}
}
GLOBAL.Dom.removeClass = function (node, str) {
node.className = node.className.replace(new RegExp("(^|\\s+)" + str), "");
}
GLOBAL.namespace('Event');
GLOBAL.Event.on = function (node, eventType, handler, scope) {
node = typeof node == 'string' ? document.getElementById(node) : node;
scope = scope || node;
if (document.all) {
node.attachEvent('on' + eventType, function () {
handler.apply(scope, arguments);
});
} else {
node.addEventListener(eventType, function () {
handler.apply(scope, arguments);
}, false);
}
}

基础的HTML

<div class="J_tab">
<div class="tab">
<ul class="tab-menuWrapper">
<li class="J_tab-menu">menu1</li>
<li class="J_tab-menu">menu2</li>
<li class="J_tab-menu">menu3</li>
</ul>
<div class="tab-contentWrapper">
<div class="J_tab-content">
<div>content1</div>
<ul>abc</ul>
</div>
<div class="J_tab-content" style="display: none;">
<p>content2</p>
</div>
<div class="J_tab-content" style="display: none;">content3</div>
</div>
</div>
<hr />
<div class="tab">
<ul class="tab-menuWrapper">
<li class="J_tab-menu">menu1</li>
<li class="J_tab-menu">menu2</li>
<li class="J_tab-menu">menu3</li>
</ul>
<div class="tab-contentWrapper">
<div class="J_tab-content">
<div>content1</div>
<ul>abc</ul>
</div>
<div class="J_tab-content" style="display: none;">
<p>content2</p>
</div>
<div class="J_tab-content" style="display: none;">content3</div>
</div>
</div>
<hr />
<div class="tab">
<ul class="tab-menuWrapper">
<li class="J_tab-menu">menu1</li>
<li class="J_tab-menu">menu2</li>
<li class="J_tab-menu">menu3</li>
</ul>
<div class="tab-contentWrapper">
<div class="J_tab-content">
<div>content1</div>
<ul>abc</ul>
</div>
<div class="J_tab-content" style="display: none;">
<p>content2</p>
</div>
<div class="J_tab-content" style="display: none;">content3</div>
</div>
</div>
</div>

一些基本的CSS

ul {
padding:;
margin:;
} .tab {
width: 400px;
} .tab .tab-currentMenu {
background-color: #333;
color: #fff;
} .tab .tab-currentMenu1 {
background-color: blue;
color: #fff;
} .underline {
text-decoration: underline;
} .tab-menuWrapper {
padding-left: 20px;
} .tab-menuWrapper li {
float: left;
display: inline;
padding: 5px;
border: 1px solid #333;
border-bottom: none;
margin-right: 5px;
} .tab-contentWrapper {
border: 1px solid #333;
clear: left;
padding: 5px;
}

实现tab效果的JS

function Tab(config) {
debugger;
this._root = config.root;
this._currentClass = config.currentClass;
var trigger = config.trigger || 'click';
this._handler = config.handler;
var autoPlay = config.autoPlay;
var playTime = config.playTime || 3000;
this._tabMenus = GLOBAL.Dom.getElementsByClassName("J_tab-menu", this._root);
this._tabContents = GLOBAL.Dom.getElementsByClassName("J_tab-content", this._root);
this.currentIndex = 0;
var This = this;
if (autoPlay) {
setInterval(function () {
This._autoHandler();
}, playTime);
}
for (var i = 0; i < this._tabMenus.length; i++) {
this._tabMenus[i]._index = i;
GLOBAL.Event.on(this._tabMenus[i], trigger, function () {
This.showItem(this._index);
this.currentIndex = this._index;
});
}
}
Tab.prototype = {
showItem: function (n) {
for (var i = 0; i < this._tabContents.length; i++) {
this._tabContents[i].style.display = 'none';
}
this._tabContents[n].style.display = 'block';
if (this._currentClass) {
var currentMenu = GLOBAL.Dom.getElementsByClassName(this._currentClass, this._root)[0];
if (currentMenu) {
GLOBAL.Dom.removeClass(currentMenu, this._currentClass);
}
GLOBAL.Dom.addClass(this._tabMenus[n], this._currentClass);
}
if (this._handler) {
this._handler(n);
}
},
_autoHandler: function () {
this.currentIndex++;
if (this.currentIndex >= this._tabMenus.length) {
this.currentIndex = 0;
}
this.showItem(this.currentIndex);
}
};

调用tab的JS

        var tabs = GLOBAL.Dom.getElementsByClassName("tab");
console.dir(tabs.length);
new Tab({ root: tabs[0], trigger: "mouseover" });
new Tab({ root: tabs[1], currentClass: "tabcurrentMenu", autoPlay: true, playTime: 5000 });
new Tab({
root: tabs[2], currentClass: "tabcurrentMenu", trigger: "mouseover", handler: function (index) {
console.log('您激活的是第' + (index + 1) + '个标签');
}
});

用面对对象方式定tab标签的更多相关文章

  1. 多iframe使用tab标签方式添加、删除、切换的处理实例

    紧接着上一篇随笔iframe的内容增高或缩减时设置其iframe的高度的处理方案 如果采用iframe来切换显示内容的方式来展现办公Web.那么需要解决几个问题 1.tab标签需要和显示的iframe ...

  2. Android UI--ViewPager扩展Tab标签指示

    Android UI--ViewPager扩展Tab标签指示 2013年8月30日出来冒冒泡 ViewPager这个控件已经不算是陌生的了,各种玩Android的小伙伴们都有发表相应的文章来讲它.我看 ...

  3. React Native 系列(九) -- Tab标签组件

    前言 本系列是基于React Native版本号0.44.3写的.很多的App都使用了Tab标签组件,例如QQ,微信等等,就是切换不同的选项,显示不同的内容.那么这篇文章将介绍RN中的Tab标签组件. ...

  4. 在Bootstrap开发中解决Tab标签页切换图表显示问题

    在做响应式页面的时候,往往需要考虑更多尺寸设备的界面兼容性,一般不能写死像素,以便能够使得界面元素能够根据设备的不同进行动态调整,但往往有时候还是碰到一些问题,如Tab标签第一页面正常显示,但是切换其 ...

  5. Python - 面对对象(基础)

    目录 Python - 面对对象(基础) 一. 概述 二. 创建类和对象 三. 面向对象三大特征 封装 继承 多态 Python - 面对对象(基础) 一. 概述 面向过程:根据业务逻辑从上到下写垒代 ...

  6. TabActivity中的Tab标签详细设置

    参考链接: http://www.iteye.com/topic/602737 这个写的很不错,我是跟着一步步写下来的,不过到最后也遇到了麻烦,就是不能将Tab标签的文字和图片分开,始终是重合的,而且 ...

  7. Android(java)学习笔记129:Tab标签的使用

    1.案例1---TabProject (1)首先是main.xml文件: <?xml version="1.0" encoding="utf-8"?> ...

  8. 基于duilib实现的可滑动tab标签控件

    最近一直在忙棋牌游戏大厅的开发,使用了duilib界面库,在大厅界面游戏菜单的展现上需要用到滑动的效果,类似悠扬棋牌,jj棋牌的菜单左右(上下)滑动的效果.通过自己的设计思路完善了一个可滑动的tab标 ...

  9. 很好用的Tab标签切换功能,延迟Tab切换。

    一个网页,Tab标签的切换是常见的功能,但我发现很少有前端工程师在做该功能的时候,会为用户多想想,如果你觉得鼠标hover到标签上,然后切换到相应的内容,就那么简单的话,你将是一个不合格的前端工程师啊 ...

随机推荐

  1. 用 Linux自带的logrotate 来管理日志

    大家可能都有管理日志的需要,比如定时压缩日志,或者当日志超过一定大小时就自动分裂成两个文件等.最近就接到这样一个小任务.我们的程序用的是C语言,用log4cpp的library来实现日志记录.但是问题 ...

  2. Scala入门之控制结构

    package com.dtspark.scala.basics /** * Scala中的基本控制结构有顺序.条件和循环三种方式,这个其它的JVM语言是一致的,但是Scala也有一些高级的流程控制结 ...

  3. Addressing Complex and Subjective Product-Related Queries with Customer Reviews-www2016-20160505

    1.Information publication:www2016 author:Julian McAuley 2.What 学习商品评论中的信息,对商品的提问,自动给出回答:按照相关程度排序 3.D ...

  4. C# EventHandler and Delegate(委托的使用)

    委托的声明 public delegate void MyDelegate(string str); 注 1.委托的定义和方法的定义类似,只是在前面加了一个delegate,但委托不是方法,它是一种特 ...

  5. 【Alpha版本】冲刺阶段——Day 6

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...

  6. 转-Spring单例模式与线程安全

    问题背景 这段时间在做项目的时候,考虑到Spring中的bean默认是单例模式的,那么当多个线程调用同一个bean的时候就会存在线程安全问题.如果是Spring中bean的创建模式为非单例的,也就不存 ...

  7. 最短的可通过编译的C语言程序

    要求:写一个最短的,能通过编译的C语言程序,但不要求能正确运行. 一般人首先想到的是除0.即 int main() { /; } 除0会引发SIGFPE信号(浮点异常). 我们也可以删掉return, ...

  8. ESPCMS基本导航操作

    Espcms和dedecms一样,是用来建企业站的cms程序,功能强大,稳定,可以帮助您快速.便捷地新建一个企业网站.无忧主机向您推荐无忧主机php虚拟主机. 我们可以通过espcms设置来去掉比如购 ...

  9. 简单解决ListView和ScrollView冲突,复杂情况仅供参考

    ScrollView嵌套ListView冲突问题的最优解决方案 (转) 记录学习之用 项目做多了之后,会发现其实 ScrollView嵌套ListVew或者GridView等很常用,但是你也会发现各种 ...

  10. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...