Several methods and examples of tab switching implementation

Source: Internet
Author: User

First, write down the tab frame, plus the simplest style, the code is as follows:

<! doctype html> <html> <head> <meta http-equiv= "Content-type"  content= "text/html;  charset=utf-8 " /> <style>   *{    padding: 0;   
  margin: 0;
 &NBSP}   li{    list-style: none;     float:left;  &NBSP}    #tabCon {    clear: both;   } </style> </head > <body>   <div id= "Tancontainer" >     <div id= "tab" >       <ul>         <li> Title A </li>         <li> title two </li>          <li> title three </li>         <li> Title IV </li>       </ul>     </div>     <div id= "Tabcon" >        <div> content One </div>       <div> content two </div>        <div> Content III </div>       <div> content four </div >     </div>   </div> </body> </html>


Now the display effect is as follows:



Four tab titles and four content areas are displayed in the page, now to achieve the tab switch effect, that is, click on the title one, the contents of a display, the other content does not show; Click Heading Two, show the contents, and show the other content ...

So, the whole idea is very simple, give four header binding events, trigger the corresponding content display, other content hidden.

Method One: Click on the content of the title to display, not click the title corresponding to the content hidden. The code is as follows:

<! doctype html> <html> <head> <meta http-equiv= "Content-type"  content= "text/html;
 charset=utf-8 " /> <style>   *{      padding: 0;
      margin: 0;  &NBSP}   li{      list-style: none;   } </style > <script>   function tab (PID) {      var tabs=["Tab1"
, "TaB2", "Tab3", "TAB4"];       for (var i=0;i<4;i++) {         if (tabs[i]==pid) {            
document.getElementById (Tabs[i]). style.display= "Block";         }else{         
   document.getelementbyid (Tabs[i]). style.display= "None";        &NBSP}       }   } </script> </head> <body >   <div id= "Tancontainer" >       <div id= "TabNav" >       <ul>            <li onclick= "tab (' Tab1 ')" > title one </li>            <li onclick= "tab (' TaB2 ')" > title two </li>            <li onclick= "tab (' Tab3 ')" > Title III </li>            <li onclick= "tab (' Tab4 ')" > title four </li>       </ ul>       </div>       <div id= "tab ">         <div id=" tab1 > Content one </div>          <div id= "TAB2" > Content two </div>          <div id= "TAB3" > Content three </div>         <div id = "TAB4" > Content four </div>       </div>   </div> </body > </html>


Method Two: First set all content to hide, and then click on the title to use the content display. The code is as follows:

<! doctype html> <html> <head> <meta http-equiv= "Content-type"  content= "text/html;  charset=utf-8 " /> <style>   *{    padding: 0;   
  margin: 0;
 &NBSP}   li{    list-style: none;     float:left;
 &NBSP}    #tabCon {    clear: both;   }    #tabCon_1 {
    display: none;  &NBSP}    #tabCon_2 {    display: none;   }    #tabCon _3{    display: none;   } </style> <script>   function  changetab (tabcon_num) {    for (i=0;i<=3;i++)  {         document.getelementbyid ("Tabcon_" +i). style.display= "None";  //hide all layers        }   &NBsp;    document.getelementbyid ("Tabcon_" +tabcon_num). style.display= "block";//show current layer  
  }  </script> </head> <body>   <div id= "Tancontainer" >     <div id= "tab" >       <ul>          <li onclick= "Changetab (' 0 ')" > title one </li>          <li onclick= "Changetab (' 1 ')" > title two </li>          <li onclick= "Changetab (' 2 ')" > Title III </li>          <li onclick= "Changetab (' 3 ')" > title four </li>        </ul>     </div>     <div id= "Tabcon" >        <div id= "Tabcon_0" > Content one </div>        <div id= "Tabcon_1" > Content two </div>       <div id= "tabcon_2" > Content three </div >       <div id= "tabcon_3" > Content four </div>      </div>   </div> </body> </html>


Method Three: Show and hide through is have class control, first of all the content hidden dispaly set to None, and the class display set as block, traversing all the title node and content node, click on the title, The header node and the right content node have class, but not the other. The code is as follows:

<! doctype html> <html> <head> <meta http-equiv= "Content-type"  content= "text/html;  charset=utf-8 " /> <style>   *{    padding: 0;   
  margin: 0;
 &NBSP}   li{    list-style: none;     float:left;  &NBSP}    #tabCon  {    clear: both;   }   # tabcon div {    display:none;   }    #tabCon  div.fdiv  {    display:block;   } </style> </head> <body>    <div id= "Tancontainer" >     <div id= "tab" >        <ul>         <li class= "Fli" > title one </li
>         <li> Title Two </li>        <li> Title III </li>          <li> title four </li>       </ul>      </div>     <div id= "Tabcon" >       <div  class= "Fdiv" > Content one </div>       <div> content two </div>        <div> Content III </div>       <div> content four < /div>     </div>   </div> </body> <script>   var
 tabs=document.getelementbyid ("tab"). getElementsByTagName ("Li");
  var divs=document.getelementbyid ("Tabcon"). getElementsByTagName ("div");   for (var i=0;i<tabs.length;i++) {    tabs[i].onclick=function () {Change (
this);}  &NBSP}   function change (obj) { &nbsP;for (var i=0;i<tabs.length;i++) {    if (tabs[i]==obj) {    tabs
[I].classname= "Fli";
    divs[i].classname= "Fdiv";
  }else{    tabs[i].classname= "";     divs[i].classname= "";    &NBSP}     }   }  </script> </html>

The disadvantage of the


method is that there is no div tag under the div for the content block.

Method Four: Do not use JS, with "input:checked" To do the tab switch, first of all the content hidden, the contents of the selected display. The code is as follows:

<! doctype html> <html> <head> <meta http-equiv= "Content-type"  content= "text/html;  charset=utf-8 " /> <title>input:checked tab Switch </title> <style> input{   opacity: 0;/* hidden input selection box/} label{  cursor: pointer;/* Mouse moved up into a hand-like * *   float: 
Left label:hover{  background:  #eee;} input:checked+label{  color: red;}///* Select Front has. tabs& Nbsp;input:nth-of-type (x): Checked. Panels .panel:nth-child (x) */Tabs input:nth-of-type (1): checked~.
Panels .panel:nth-child (1),. Tabs input:nth-of-type (2): Checked~.panels .panel:nth-child (2) {
  opacity: 1; }. panel{  opacity: 0;   position: absolute;/* make the content area position the same as */} </style> </head > <body>   <div class= "tabs" >       <input  Checked id= "One"  name= "tabs"  type= "Radio" > &NBsp;     <label for= "One" > Title I </label>        <input id= "Two"  name= "tabs"  type= "Radio" >       <
Label for= "Two" > title two </label>       <div class= "Panels" >         <div class= "Panel" >            <p> Content One </p>         </div >         <div class= "Panel" >            <p> Content two </p>         < /div>       </div>   </div> </body> </html>


The disadvantage of the modified method is that different regions can be switched only by clicking.



Several different tab-switching examples


first, imitation of the "Renmin University" official website tab switch , the background is a picture, the effect of the chart is as follows:


Effects when you move the mouse over the news

Effect of mouse move to post

The effect of moving the mouse over the AC

Academic, communicative and stylistic content is empty, I did not write. The complete code is as follows:

<! doctype html> <html> <head> <meta http-equiv= "Content-type"  content= "text/html;  charset=utf-8 " /> <style>   *{    padding: 0;   
  margin: 0;  &NBSP}   body{    font-family: arial,verdana,sans-serif, "XXFarEastFont-Arial";   &NBSP}   li{    list-style: none;     float:left;   &NBSP}   a{    text-decoration: none;     color: #
FFEEC6;  &NBSP}    #tanContainer {    height: 210px;     
width: 470px;
    background: url (homeinfo-trans-bg.png);
    overflow: hidden;
    color:  #ffeec6;  &NBSP}    #tanContainer  li a{    height: 25px;     display: inline-block;
    margin-left: 18px;
    font-size: 12px;
    padding-top: 12px;
    margin-bottom: 15px;  &NBSP}   ul li a.fli {  }    #tabOne {    
width: 122px;
    opacity: 0;  &NBSP}    #tabTwo {    padding-left: 102px;   }   # tabcon {    clear: both;   }    #tabCon  p a{ 
   color:  #FFF2D5;  &NBSP}   div div p{    font-size: 12px;    
 margin: 10px 0 0 20px;
    width: 440px;  &NBSP}    #bigPara {    font-size: 16px;     color:   #FFF2D5;
    border-bottom: 1px dotted  #FFF2D5;
    padding-bottom: 5px;  &NBSP}    #tabCon  div {    display:none;   }    #tabCon  div.fdiv {    display:block;   } </style> </head> < body>   <div id= "Tancontainer" >     <div id= "tab" >        <ul>         <li><a  class= "Fli"  href= "#"  id= "TabOne" > News </a></li>          <li><a href= "#"  id= "Tabtwo" > Announcements </a></li>          <li><a href= "#" > Academic </a></li>          <li><a href= "#" > Communication </a></li>         <li><a href= "#" > Style </a></li>        </ul>     </div>     <div id= "Tabcon" >       <div class= "Fdiv" >          <p id= "Bigpara" ><a href= "#" > Cyprus President Nicos Anasta Siadis visits Renmin University   won ...</a>< /p>         <p><a href= "#"  title= "The Chinese People's University to carry out special education   Carry forward jiao spirit   practice "three strict three real" (2015-10-25) > The Chinese People's University to carry out special education   promote Jiao Spirit   practice "three strict three real" (2015-10-25) </a></ P>         <p><a href= "#" > The Chinese People's university earnestly implements the main responsibility and supervision responsibility of the party conduct and clean government (2015-10-23) </a></p>         <p><a href= "#" > The fourth session of the Chinese People's University Sports Culture festival opens  2015 New Year's meeting (2015-10-18) </a></p>          <p><a href= "#" > Chinese People's University "the way" the chief consultant of the Economic Research Institute held   Turkmenistan ambassador to China </a></p>        </div>       <div>          <P><A>2015-2016 school year first semester 8th Week School Leadership Open Day arrangement ... (2015-10-22) </a></p>         <p><a> About holding the second youth Management Cadre post skill Competition of Renmin University of China. (2015-09-30) </a></p>         <p><a> Our school 16th Chinese University video public class online, please be actively concerned about ... (2015-10-26) </a></p>         <p><a> Notice of organizing the youth teachers of our school to visit the social practice activities of Lu Xun Museum ... (2015-10-23) </a></p>         <p><a> About the holding of the fourth session of the Chinese People's University staff badminton "1+1" group ... (2015-10-23) </a></p>         <p><a> Chinese People's University MOOCs course recording studio equipment acquisition project won the bid ... (2015-10-23) </a></p>       </div> &Nbsp;     <div> Content III </div>       <div> Content four </div>       <div> content five </div>     </div >   </div> </body> <script>   var tabs=document.getelementbyid ("tab")
). getElementsByTagName ("Li");
  var divs=document.getelementbyid ("Tabcon"). getElementsByTagName ("div");   for (var i=0;i<tabs.length;i++) {    tabs[i].onmouseover=function () {
Change (this);}  &NBSP}   function change (obj) {  for (var i=0;i<tabs.length;i++) {     if (tabs[i]==obj) {    tabs[i].classname= "Fli";     
Divs[i].classname= "Fdiv";       if (i==0) {         document.getElementById ("Tancontainer"). style.backgroundposition= "0 0"       }else{        document.getelementbyid ("
Tancontainer "). style.backgroundposition=" 0 -210px "         }else{
    tabs[i].classname= "";
    divs[i].classname= "";    &NBSP}     }   }  </script> </html>



This example is very simple also very common tab switch, JS a bit more is to change the position of the background picture, the other is simple style.


two, on a said without JS, with input:checked to achieve the tab switch effect, now use this principle plus CSS3 do a beautiful example, in the switch, the content area is gradually present. The effect chart is as follows:


The effect of HTML/CSS when mouse clicks

When the mouse clicks Ajax Effect

The complete code is as follows:

<! doctype html> <html> <head> <meta http-equiv= "Content-type"  content= "text/html;  charset=utf-8 " /> <title>input:checked tab Switch </title> <style> tabs{  
color:  #FFF;
  font-family:  "Microsoft Ya-hei";
input{  opacity: 0;/* Hide Input selection box */} input:checked+label{  padding-bottom: 6px;
  font-weight: bold; label{  cursor: pointer;/* Mouse Move up into hand/  float: left;   width: 
120px;
  line-height: 40px;
  margin-right: 5px;
  text-align: center; }. Tabs label:nth-of-type (1) {  background:  #5eb0de; Tabs label:nth-of-type (2) { 
 background:  #86cad7; }. Tabs label:nth-of-type (3) {  background:  #e9bab3; Tabs label:nth-of-type (4) { 
 background:  #a8c194; } label:hover{  font-weight: bOld /* Select front of. Tabs input:nth-of-type (x): Checked. Panels .panel:nth-child (x) * * * Tabs input:nth-of-type
(1): Checked~.panels .panel:nth-child (1) {  opacity: 1;   background:  #5eb0de;
  -webkit-transition: .3s; }. Tabs input:nth-of-type (2): Checked~.panels .panel:nth-child (2) {  opacity: 1;  
 background:  #86cad7;
  -webkit-transition: .3s; }. Tabs input:nth-of-type (3): Checked~.panels .panel:nth-child (3) {  opacity: 1;  
 background:  #e9bab3;
  -webkit-transition: .3s; }. Tabs input:nth-of-type (4): Checked~.panels .panel:nth-child (4) {  opacity: 1;  
 background:  #a8c194;
  -webkit-transition: .3s; }. panel{  opacity: 0;   position: absolute;/* make the content area position the same as * *   height: 
200px;
  width: 455px;   margin-top: 25px;
  padding: 0 20px; } </style> </head> <body>   <div class= "tabs" >        <input checked id= "One"  name= "tabs"  type= "Radio" >        <label for= "One" >HTML/CSS</label>       < Input id= "Two"  name= "tabs"  type= "Radio" >       <label  For= "Two" >JavaScript</label>       <input id= "three"  name= " Tabs " type=" Radio ">       <label for=" three ">ajax</label" >       <input id= "Four"  name= "tabs"  type= "Radio" >        <label for= "Four" >Sever Side</label>        <div class= "Panels" >         <div class= "Panel" >            <h2>html text label Language </h2>           <p>html   is the key to the world of  WEB  technology. html  is very easy to learn! You're going to love it! </p>         </div>          <div class= "Panel" >           <h2 >javascript scripting language </h2>           <p>javascript   is the most popular scripting language in the world. <br/>              JavaScript  is part of  web  language, which is suitable for PCs, laptops, tablets and mobile phones. <br/>              JavaScript  was designed to
 HTML  page adds interactivity.           </p>    &nbsP;    </div>         <div class= " Panel ">           <h2>ajax ajax </h2>            <p>ajax = asynchronous javascript  and xml (asynchronous  JavaScript  and  xml). <br/>           AJAX  is not a new programming language, but a new way to use existing standards. <br/>           AJAX  is the art of exchanging data and updating some Web pages with servers,
Without reloading the entire page.           </p>          </div>         <div class= "Panel" >          <h2>sever side Server Scripts </h2>            <p>SQL  is the standard computer language for accessing and processing databasesStatement <br/>           ASP  is a powerful tool for creating dynamic interactive Web pages. <br/>           ADO  means  ActiveX  Data Object (activex data objects). <br/>           PHP  is a powerful server-side scripting language for creating dynamic interactive sites.
<br/>           VBScript  is a scripting language produced by Microsoft Corporation.           </p>       
  </div>       </div>   </div> </body> </html>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.