WEB前端:01_Tab选项卡

 

Tab选项卡

 采用两种方法实现选项卡切换功能,目前只提供了最基本的切换效果,后期增加jquery版和渐变切换效果。

 

效果图:

纯JS简化版:

<html>
<head>
<title>Tab选项卡 - 纯JS简化版</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<style type="text/css">
#btn { width: 500px; height: 30px; margin:0;padding: 0; list-style: none;}
#btn li { cursor: pointer; float: left; width: 100px; height: 30px; margin-right: 2px; background: #666; color:#fff; text-align: center; line-height: 30px;}
#btn li.onbtn {background: #f00;}
#tabs div {display: none; width: 500px; height: 100px; border: 1px solid #ccc;}
</style>
<script type="text/javascript">
window.onload = function() {

	var btn = document.getElementById('btn');
	var btnli = btn.getElementsByTagName('li');
	var tabs = document.getElementById('tabs');
	var tabsdiv = tabs.getElementsByTagName('div');

	for (var a=0; a<btnli.length; a++) {
		btnli[a].index=a;
		btnli[a].onmouseover = function() {
			for (var b=0; b<tabsdiv.length; b++) {
				btnli[b].className = "";
				tabsdiv[b].style.display = "none";
			}
			this.className = "onbtn";
			tabsdiv[this.index].style.display = "block";
		}
	}
	
	btnli[0].className = "onbtn";
	tabsdiv[0].style.display = "block";
	
}
</script>
<body>

<div>
<ul id="btn">
	<li>选项一</li>
	<li>选项二</li>
	<li>选项三</li>
</ul>
<div id="tabs">
	<div>选项一内容</div>
	<div>选项二内容</div>
	<div>选项三内容</div>
</div>
</div>

</body>
</html>

 

 纯JS面向对象版:

<html>
<head>
<title>纯JS - 面向对象版</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<style type="text/css">
#btn, #btn2 { width: 500px; height: 30px; margin:0;padding: 0; list-style: none;}
#btn li, #btn2 li { cursor: pointer; float: left; width: 100px; height: 30px; margin-right: 2px; background: #666; color:#fff; text-align: center; line-height: 30px;}
#btn li.onbtn, #btn2 li.onbtn {background: #f00;}
#tabs div, #tabs2 div {display: none; width: 500px; height: 100px; border: 1px solid #ccc; margin-bottom:10px;}
</style>
<script type="text/javascript">

function tab(btn, tabs) {
	var _this = this;
	var btn = document.getElementById(btn);
	var tabs = document.getElementById(tabs);
	this.btnli = btn.getElementsByTagName('li');
	this.tabsdiv = tabs.getElementsByTagName('div');

	for (var a=0; a<this.btnli.length; a++) {
		this.btnli[a].index = a;
		this.btnli[a].onmouseover = function() {
			_this.onmouseoverfn(this);
		}
	}
	this.tabsdiv[0].style.display = "block";
	this.btnli[0].className = "onbtn";
}

tab.prototype.onmouseoverfn = function(obj) {
	for (var b=0; b<this.btnli.length; b++) {
		this.tabsdiv[b].style.display = "none";
		this.btnli[b].className = "";
	}
	this.tabsdiv[obj.index].style.display = "block";
	this.btnli[obj.index].className = "onbtn";
}

window.onload = function() {
	new tab('btn', 'tabs');
	new tab('btn2', 'tabs2');
}

</script>
<body>

<div>
<ul id="btn">
	<li>选项一</li>
	<li>选项二</li>
	<li>选项三</li>
</ul>
<div id="tabs">
	<div>选项一内容</div>
	<div>选项二内容</div>
	<div>选项三内容</div>
</div>
</div>

<div>
<ul id="btn2">
	<li>选项一</li>
	<li>选项二</li>
	<li>选项三</li>
</ul>
<div id="tabs2">
	<div>选项一内容</div>
	<div>选项二内容</div>
	<div>选项三内容</div>
</div>
</div>

</body>
</html>

 

posted on 2014-04-27 16:29  桑伯帝  阅读(397)  评论(0编辑  收藏  举报

导航