一些公共的底层的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. nios II--实验6——串口软件部分

    软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for Eclipse,需要进 ...

  2. [HDOJ5439]Aggregated Counting(乱搞)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5439 题意:按规则构造一个数列a a(1)=1 a(2)=2 a(2)=2 -------> 写两个 ...

  3. 如何设置div高度为100%

    div高度通常都是固定值,直接将div高度设为100%是无效的,那么如何设置才能有效呢? 直接给div设置height:100%即可,无效的原因一定是父元素的高度为0,最常见的就是给body的直接元素 ...

  4. Go加密解密之DES

    一.DES简介 DES(Data Encryption Standard)是对称加密算法,也就是加密和解密用相同的密钥.其入口参数有三个:key.data.mode.key为加密解密使用的密钥,dat ...

  5. oracle 删除用户及相关表数据,释放磁盘空间

    来源于:http://www.itpub.net/thread-513609-1-1.html http://bbs.csdn.net/topics/330251089 http://blog.csd ...

  6. oracle操作记录

    由于之前建的job过多,造成数据库cpu占用率达到99%,造成需要的job崩溃. 以下为解决方案: 1. 查询当前的job列表 : select * from user_jobs; 2. 暂停所有的j ...

  7. CAP理论

    自打引入CAP理论的十几年里,设计师和研究者已经以它为理论基础探索了各式各样新颖的分布式系统,甚至到了滥用的程度.NoSQL运动也将CAP理论当作对抗传统关系型数据库的依据. CAP理论主张任何基于网 ...

  8. iOS开发小技巧--修改按钮内部图片和文字之间的间距(xib)

    调整按钮的Edge属性,选择调整图片的Edge还是label的Edge,如图:

  9. Shell命令_if

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #if if [ 条件判断式 ] ...

  10. Dubbo_异常_服务注册运行正常但是Dubbo-Admin看不到服务(亲测可用)

    一.背景: 1.Dubbo服务正常注册到ZooKeeper 2.客户端调用Dubbo服务正常 二.原因: Dubbo-Admin未配置分组信息 三.解决步骤: 1.dubbo.properties添加 ...