---
title: "选项卡效果"
author: "corangecn"
date: "2014-09-20T15:33:01Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2014-09-20T15:33:01Z"
id: 2118488
url: "https://corangecn.iteye.com/blog/2118488"
---

[![http://www.corange.cn//uploadfiles/20110419_24823.jpg](http://www.corange.cn//uploadfiles/20110419_24823.jpg)](http://www.corange.cn//uploadfiles/20110419_24823.jpg)

演示地址:[http://www.corange.cn/demo/3766/index.html](http://www.corange.cn/demo/3766/index.html)   
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;  
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312" /&gt;  
&lt;title&gt;Demo&lt;/title&gt;  
&lt;Style&gt;  
\*{padding:0; margin:0}  
body{font-size:12px; font-family:"微软雅黑","宋体"; color:#333;}  
h1{font-size:16px; background:#ccc; padding:5px 10px; margin-bottom:15px}  
.effect{ margin-bottom:15px; padding:0 10px}  
.hide{display:none}  
ul{list-style:none; overflow:hidden; height:1%; width:240px}  
li{float:left; cursor:pointer; padding:5px 0; width:78px; border:1px solid #ccc; text-align:center}  
li.current{ font-weight:bold; color:#000;}  
#tab\_content1 div,#tab\_content2 div,#tab\_content3 div,#tab\_content4 div{ width:228px; height:150px; padding:10px 0 0 10px; border:1px solid #ccc;}  
&lt;/style&gt;  
&lt;script&gt;  
/\*浏览器判断\*/  
var isIe = function(){return document.all?true:false;};  
var isIe6 = function(){return isIe() && !window.XMLHttpRequest;}  
var isIe7 = function(){return isIe() && /msie 7.0/gi.test(window.navigator.appVersion);};  
var isIe8 = function(){return isIe() && /msie 8.0/gi.test(window.navigator.appVersion);};  
var isFF = function(){return /firefox/gi.test(window.navigator.userAgent);};  
var isOp = function(){return /opera/gi.test(window.navigator.userAgent);};  
var isChrom = function(){return /Chrom/gi.test(window.navigator.userAgent);};  
/\**  
\* 通过HTML元素的id获取Dom对象组  
\* @param {String | Object} HTML标签的id或者Dom对象，参数可多个  
\* @return {Object | Array} HTMLElement对象 或 HTMLElement对象组  
\*/  
var getId = function(){  
var els = \[];  
for(var i = 0,leng = arguments.length; i &lt; leng; i++){  
var el = arguments\[i];  
if(typeof el == "string") el = document.getElementById(el);  
if(leng == 1) return el;  
els.push(el);   
}  
return els;  
};  
/\**  
\* 通过HTML元素的标签名获取Dom数组对象  
\* @param {String | Object} HTML标签的id或者Dom对象 --此项为可选  
\* @param {String} HTML标签名称 --此项为可选  
\* @return {Array} HTMLElement数组对象  
\*/  
var getTagName = function(el, tagName){  
return (getId(el) || document).getElementsByTagName(tagName || "\*");  
};  
/\**  
\* 继承对象（复制属性/方法）  
\* @param {Object} 被复制对象（子对象）  
\* @param {Object} 复制对象（父对象）  
\* @param {Boolean} 是否重写属性/方法  
\* @return {Object} 返回被复制对象（子对象）  
\*/  
var extend = function(target, souce, rewrite){  
for(var property in souce){  
if(rewrite) target\[property] = souce\[property]; //重写并添加新的属性/方法  
else if(!target\[property]) target\[property] = souce\[property]; //只添加新的属性/方法  
}  
return target;  
};  
/\**  
\* 设置监听器  
\* @param {Object} 监听对象  
\* @param {String} 监听类型  
\* @param {Function} 监听方法  
\*/  
var addEvent = function(obj,type,func){  
if(obj.addEventListener){  
obj.addEventListener(type,func,false);  
} else if(obj.attachEvent){  
obj.attachEvent("on" + type,func);  
} else {  
obj\["on" + type] = func;  
}  
}  
/\**  
\* 选择环境运行函数  
\* @param {Function} 执行函数  
\* @param {Object} 运行环境  
\* @return {Function} 返回一个已被绑定运行环境的函数  
\*/  
var bind = function(func,environment){  
var params = Array.prototype.slice.call(arguments,2);  
return function(){  
func.apply(environment,params);  
}  
}  
/\**  
\* @classDescription 选项卡构造函数  
\* @type {Object}  
\* @param {String | Object} TAB父层的id或者Dom对象   
\* @param {String} 绑定鼠标行为的tab  
\* @param {String | Object} TAB CONTENT父层的id或者Dom对象   
\* @param {String} 被切换的TAB CONTENT  
\* @param {Object} 参数集合（包括event,current,autoPlay,timeout）  
\*/   
function Tab(tabId, tabTag, contentId, contentTag, option){  
var tabEl = getId(tabId),  
tabChild = tabEl.children,  
contentEl = getId(contentId),  
contentChild = contentEl.children;

this.tabsFather = tabEl;  
this.tabs = \[];  
this.contents = \[];  
this.currentIndex = 0;  
this.timer = null;  
for(var i = 0, l = tabChild.length; i &lt; l; i++){  
if(tabChild\[i].tagName.toLowerCase() == tabTag)  
this.tabs.push(tabChild\[i]);  
}  
for(var i = 0, l = contentChild.length; i &lt; l; i++){  
if(contentChild\[i].tagName.toLowerCase() == contentTag)  
this.contents.push(contentChild\[i]);  
}

this.setOption(option);  
this.run();  
}  
Tab.prototype.setOption = function(option){  
this.option = {  
event: "click", //绑定事件  
current: "current", //切换的className  
autoPlay: false, //是否自动切换选项卡  
timeout: 1500 //切换时间  
}  
extend(this.option, option || {}, true);  
}  
Tab.prototype.run = function(){  
for(var i = 0, l = this.tabs.length; i &lt; l; i++){  
addEvent(this.tabs\[i], this.option.event, bind(function(tabs, contents, index, current, \_this){   
var curIndex = \_this.currentIndex;  
tabs\[curIndex].className = tabs\[curIndex].className.replace(new RegExp("\\\\s\*" + current,"g"), "");  
tabs\[index].className += " " + current;  
contents\[curIndex].className += " hide";  
contents\[index].className = contents\[curIndex].className.replace(/\\s\*hide/g, "");  
\_this.currentIndex = index;  
}, this.tabs\[i], this.tabs, this.contents, i, this.option.current, this));  
}  
if(this.option.autoPlay){  
this.auto();  
addEvent(this.tabsFather, "mouseover", bind(function(){  
clearInterval(this.timer);  
}, this))  
addEvent(this.tabsFather, "mouseout", bind(function(){  
this.auto();  
}, this))  
}  
}  
Tab.prototype.auto = function(){  
this.timer = setInterval(bind(function(){  
this.next();  
}, this), this.option.timeout)  
}  
Tab.prototype.next = function(){  
var index = (this.currentIndex + 1) &gt; (this.tabs.length - 1)?0:(this.currentIndex + 1);  
if(isIe()){  
this.tabs\[index].fireEvent("on" + this.option.event);  
} else {  
var e = document.createEvent("MouseEvent");  
e.initEvent(this.option.event,true,true);  
this.tabs\[index].dispatchEvent(e);  
}   
}  
Tab.prototype.prev = function(){  
var index = (this.currentIndex - 1) &gt;= 0?(this.currentIndex - 1):(this.tabs.length - 1);  
if(isIe()){  
this.tabs\[index].fireEvent("on" + this.option.event);  
} else {  
var e = document.createEvent("MouseEvent");  
e.initEvent(this.option.event,true,true);  
this.tabs\[index].dispatchEvent(e);  
}   
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;div class="effect"&gt;  
&lt;h1&gt;选项卡&lt;/h1&gt;  
&lt;pre&gt;调用方法： new Tab("tab\_test1", "li", "tab\_content1", "div");  
&lt;/pre&gt;  
&lt;ul id="tab\_test1"&gt;  
&lt;li class="current"&gt;Tab1&lt;/li&gt;  
&lt;li&gt;Tab2&lt;/li&gt;  
&lt;li&gt;Tab3&lt;/li&gt;  
&lt;/ul&gt;  
&lt;div id="tab\_content1"&gt;  
&lt;div&gt;content1&lt;/div&gt;  
&lt;div class="hide"&gt;content2&lt;/div&gt;  
&lt;div class="hide"&gt;content3&lt;/div&gt;  
&lt;/div&gt;  
&lt;button onclick="t1.prev()"&gt;pre&lt;/button&gt;&lt;button onclick="t1.next()"&gt;next&lt;/button&gt;  
&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;  
&lt;pre&gt;调用方法： new Tab("tab\_test2", "li", "tab\_content2", "div", {event:"mouseover"});  
&lt;/pre&gt;  
&lt;ul id="tab\_test2"&gt;  
&lt;li class="current"&gt;Tab1&lt;/li&gt;  
&lt;li&gt;Tab2&lt;/li&gt;  
&lt;li&gt;Tab3&lt;/li&gt;  
&lt;/ul&gt;  
&lt;div id="tab\_content2"&gt;  
&lt;div&gt;content1&lt;/div&gt;  
&lt;div class="hide"&gt;content2&lt;/div&gt;  
&lt;div class="hide"&gt;content3&lt;/div&gt;  
&lt;/div&gt;  
&lt;button onclick="t2.prev()"&gt;pre&lt;/button&gt;&lt;button onclick="t2.next()"&gt;next&lt;/button&gt;  
&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;  
&lt;pre&gt;调用方法： new Tab("tab\_test2", "li", "tab\_content2", "div", {autoPlay:true});  
&lt;/pre&gt;  
&lt;ul id="tab\_test3"&gt;  
&lt;li class="current"&gt;Tab1&lt;/li&gt;  
&lt;li&gt;Tab2&lt;/li&gt;  
&lt;li&gt;Tab3&lt;/li&gt;  
&lt;/ul&gt;  
&lt;div id="tab\_content3"&gt;  
&lt;div&gt;content1&lt;/div&gt;  
&lt;div class="hide"&gt;content2&lt;/div&gt;  
&lt;div class="hide"&gt;content3&lt;/div&gt;  
&lt;/div&gt;  
&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;  
&lt;pre&gt;调用方法： new Tab("tab\_test4", "li", "tab\_content4", "div", {autoPlay:true, timeout:3000});  
&lt;/pre&gt;  
&lt;ul id="tab\_test4"&gt;  
&lt;li class="current"&gt;Tab1&lt;/li&gt;  
&lt;li&gt;Tab2&lt;/li&gt;  
&lt;li&gt;Tab3&lt;/li&gt;  
&lt;/ul&gt;  
&lt;div id="tab\_content4"&gt;  
&lt;div&gt;content1&lt;/div&gt;  
&lt;div class="hide"&gt;content2&lt;/div&gt;  
&lt;div class="hide"&gt;content3&lt;/div&gt;  
&lt;/div&gt;  
&lt;br&gt;&lt;br&gt;  
&lt;/div&gt;  
&lt;/body&gt;  
&lt;Script&gt;  
var t1 = new Tab("tab\_test1", "li", "tab\_content1", "div");  
var t2 = new Tab("tab\_test2", "li", "tab\_content2", "div", {event:"mouseover"});  
var t3 = new Tab("tab\_test3", "li", "tab\_content3", "div", {autoPlay:true});  
var t4 = new Tab("tab\_test4", "li", "tab\_content4", "div", {autoPlay:true, timeout:3000});  
&lt;/script&gt;  
&lt;/html&gt; 

分享到:0

原文地址:[http://www.corange.cn/archives/2011/04/3766.html](http://www.corange.cn/archives/2011/04/3766.html)