zoukankan      html  css  js  c++  java
  • 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 {  500px; height: 30px; margin:0;padding: 0; list-style: none;}
    #btn li { cursor: pointer; float: left;  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;  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 {  500px; height: 30px; margin:0;padding: 0; list-style: none;}
    #btn li, #btn2 li { cursor: pointer; float: left;  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;  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>
    
    
  • 相关阅读:
    [转载]网络流ISAP算法的简单介绍
    [漫画]120430 混血男孩
    [代码]SGU 270 Thimbles
    [代码]UVALive 5882 Racing Car Trail
    [代码]SGU 298 King Berl VI
    [解题报告]Codeforces 105D Entertaining Geodetics
    07年的第一个小时
    简单工厂模式
    讨厌什么
    休息像神的味道
  • 原文地址:https://www.cnblogs.com/haicheng/p/3694684.html
Copyright © 2011-2022 走看看