Article reprinted from: http://tongling.github.io/JSCards/
Implementing tab switching effects with HTML+CSS+JS
Use the comprehensive knowledge you've learned before to implement a common tab effect on a news portal site:
Text Material: Real estate: 2.75 million buy Changping neighborhood triathlon Total Price 200,000 buy a 2 million within the purchase of the five ring three 1.4 million home East third ring Beijing first 0 first pay real estate 530,000 buy East 5 ring 50 Ping Jing property straight down 5000 Citic Palace Park building 40 Flat rental house big makeover Beauty Girl's mix and match small nest Classic fresh Jean Home 90 flat old room glow new Chinese cool color 66 ping Contrast lively home tile just like choose wife bathroom flue design housing: Tongzhou luxury 3 2.6 million Two ring scarce 2 250w West 3 ring permeability 2 29,001,300,002 Home Limited snapping Yellow Cheng Gen Primary School District only 2,600,121 flat 700,000 throw! Exclusive villa 2.8 million Suzhou Bridge 2 special price 2.48 million
Analyze the basic idea first. First write the HTML, then load the style, and finally use JS to modify the corresponding DOM, to change the tab and content purposes.
First, HTML page layout
HTML displays all text-related information, so the text-related information on this page is the three titles of the tabs above, as well as the contents of the tabs. So decided to use <ul> <li>
the layout of the title, content using <div>
layout. Write the following code:
The style that is shown now is this:
CSS Styles
To make the tab style shown, there are a few places to note: 1, the entire tab style settings 2, tab header style settings 3, tab content style settings 4, only one tab content can be displayed, the other tab content is hidden.
After writing, the effect will come out.
JavaScript implementation TAB Toggle
1, first need to get the tab title and tab content 2, tab content Three, you need to loop through to operate, know which tab and which option content match. 3, by changing the DOM CSS class name, the current click on the tab display, other hidden.
Full code:
<! DOCTYPE html>lang="EN"><metacharset="UTF-8"><title> tab</title><styleType="Text/css">/* CSS style creation */*{Padding:0px;Margin:0px;}A{Text-decoration:None;Color:Black;}A: hover{Text-decoration:None;Color:#336699;}#tab{Width:270px;Padding:5px;Height:150px;Margin:20px;}#tabUl{List-style:None;Display:;Height:30px;Line-height:30px;Border-bottom:2px#C88Solid;}#tabUlLi{Background:#FFF;Cursor:Pointer;Float:Left;List-style:NoneHeight:29px;Line-height:29px;Padding:0px10px;Margin:0px10px;Border:1pxSolid#BBB;Border-bottom:2pxSolid#C88;}#tabUlLi. On{Border-top:2pxSolidGray;Border-bottom:2pxSolid#FFF;}#tabDiv{Height:100px;Width:250px;Line-height:24px;Border-top:None;Padding:1px;Border:1pxSolid#336699;Padding:10px;}. Hide{Display:None;}</style><scriptType="Text/javascript">JS Implementation tab ToggleWindow.OnLoad=function(){VarMytab=Document.getElementById("Tab");Entire DivVarMyul=Mytab.getElementsByTagName("UL")[0];A nodeVarMyli=Myul.getElementsByTagName("Li");ArrayVarMydiv=Mytab.getElementsByTagName("Div");ArrayFor(VarI=0;I<Myli.Length;I++){Myli[I].Index=I;Myli[I].OnClick=function(){For(VarJ=0;J<Myli.Length;J++){Myli[J].ClassName="Off";Mydiv[J].ClassName="Hide";}This.ClassName="On";Mydiv[This.Index].ClassName="Show";}}}</script><body><!--HTML page layout--<divID = "Tab"><ul><liclass="Off"> property</li><liclass="On"> Home</li><liclass="Off"> Secondhand</li></ul><divId="FirstPage"class="Hide"><ahref = "#">275 Wan shopping changping Neighborhood Triathlon Total Price 200,000 buy a home</a><br/><ahref = "#">200 in the five ring three 1.4 million home to the east third ring</a><br/><ahref = "#"> Beijing 0 First pay property 530,000 buy East 5 ring 50 Ping</a><br/><ahref = "#"> property straight down 5000 Citic House Park King House</a><br/></div><divId="Secondpage"class="Show"><ahref = "#">40 Flat rental house big makeover beauty girl's mixed up small nest</a><br/><ahref = "#"> Classic Fresh Jean Home 90 flat old room glow Renaissance</a><br/><ahref = "#"> New Chinese cool color warmth 66 flat contrast lively home</a><br/><ahref = "#"> Tiles It's like choosing a wife. Design of toilet flue</a><br/></div><divId="Thirdpage"class = "Hide"><a href = "#" > Tongzhou Luxury 3 2,600,002 ring scarce 2 250w dump </a><br/> <a href = "#" > West 3-ring permeability 2 29,001,300,002-limit snap-in </a><br/ > <a href = "#" > Yellow Cheng Gen Primary School District only 2,600,121 Ping 700,000 throw! </a><br/> <a href = "#" > exclusive villa 2.8 million Suzhou Bridge 2 discount price 2.48 million </a><br/> </div></div></body></ Html>
Implement tab transition effects with Html+css+js