一些公共的底层的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. [BZOJ 1260][CQOI2007]染色(DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1260 分析: f[i][j]表示i~j刷成s[i]~s[j]这个样子需要的最小次数 则 ...

  2. 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure

    yu Code 15 Comments  机器学习(ML),自然语言处理(NLP),信息检索(IR)等领域,评估(Evaluation)是一个必要的 工作,而其评价指标往往有如下几点:准确率(Accu ...

  3. webpack入坑之旅(一)不是开始的开始

    最近学习框架,选择了vue,然后接触到了vue中的单文件组件,官方推荐使用 Webpack + vue-loader构建这些单文件 Vue 组件,于是就开始了webpack的入坑之旅.因为原来没有用过 ...

  4. MySQL修改,表结构大幅修改

    ------------------create table t_video_file_temp( video_id bigint not null comment '视频Id', file_md5 ...

  5. 了解ASP.NET MVC几种ActionResult的本质:JavaScriptResult & JsonResult

    在之前的两篇文章(<EmptyResult & ContentResult>和<FileResult>)我们剖析了EmptyResult.ContentResult和F ...

  6. 线段树单点更新poj 2828

    n个人 他要插入的位置 和权值(这东西就最后输出来的) 倒的插就一定是他自己的位子 一个线段树维护一下就可以了 nlog(n) #include<stdio.h> #include< ...

  7. java的debug和release编译方式

    以前没有注意过,其实Java编译成.class 有两种方式 使用javac,默认使用的release方式,而我们经常使用的MyEclipse工具,用的是debug模式. 区别暂时了解如下: relea ...

  8. linux下截取整个网页

    前提需要安装 gimp图片处理软件 打开gimp 文件-创建-从网页  然后输入网页地址就可以截取整个网页了

  9. CSS3动画属性之Animation

    首先定义一个动画规则: @keyframes mymove { from {top:0px;} to {top:200px;} } @-moz-keyframes mymove /* Firefox ...

  10. spring spel

    •Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言.   •语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpE ...