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:
Property:
2.75 million purchase of Changping Lintie Sanju total price of 200,000 buy one
Purchased 5 million and 3 homes within 2 million
Beijing's first real estate with zero down payment
Beijing real estate straight down 5000 Citic Mansion
Home:
40-flat rental house remodeled
Classic fresh and simple European love home 90 flat old room rejuvenated
New Chinese style cool color tenderness 66 flat hit color lively home
Tiles are like choosing your wife's bathroom flue design
Second-hand housing:
Tongzhou luxury 3 bedroom 2.6 million second ring scarcity 2 bedroom 250w dump
West 3rd Ring Transparent 2 Houses 2.9 million 1.3 Million 2 Houses
Huangchenggen Elementary School District is only 2.6 million, 121 draws and 700,000 throws!
Exclusive villa 2.8 million Suzhou Bridge 2 bedroom preferential 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 several places to note:
1. style settings for the entire tab
2. Style settings for TAB titles
3. Style settings for tab content
4. You can only display the contents of one tab, and the contents of other tabs are hidden.
After writing, the effect will come out.
JavaScript implementation TAB Toggle
1, first need to get the tab title and tab content
2, the tab content has three, need to iterate to operate, know which tab and which option content matches.
3, by changing the DOM CSS class name, the current click on the tab display, other hidden.
Full code:
<! DOCTYPE html>
<html>
<head lang = "en">
<meta charset = "UTF-8">
<title> tab </ title>
<style type = "text / css">
/ * CSS style making * /
* {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;}
#tab ul {list-style: none; display:; height: 30px; line-height: 30px; border-bottom: 2px # C88 solid;}
#tab ul li {background: #FFF; cursor: pointer; float: left; list-style: none height: 29px; line-height: 29px; padding: 0px 10px; margin: 0px 10px; border: 1px solid #BBB; border-bottom: 2px solid # C88;}
#tab ul li.on {border-top: 2px solid Saddlebrown; border-bottom: 2px solid #FFF;}
#tab div {height: 100px; width: 250px; line-height: 24px; border-top: none; padding: 1px; border: 1px solid # 336699; padding: 10px;}
.hide {display: none;}
</ style>
<script type = "text / javascript">
// JS implements tab switching
window.onload = function () {
var myTab = document.getElementById ("tab"); // The entire div
var myUl = myTab.getElementsByTagName ("ul") [0]; // a node
var myLi = myUl.getElementsByTagName ("li"); // Array
var myDiv = myTab.getElementsByTagName ("div"); // Array
for (var i = 0; i <myLi.length; i ++) {
myLi [i] .index = i;
myLi [i] .onclick = function () {
for (var j = 0; j <myLi.length; j ++) {
myLi [j] .className = "off";
myDiv [j] .className = "hide";
}
this.className = "on";
myDiv [this.index] .className = "show";
}
}
}
</ script>
</ head>
<body>
<!-HTML page layout->
<div id = "tab">
<ul>
<li class = "off"> Property </ li>
<li class = "on"> Home </ li>
<li class = "off"> Second-hand housing </ li>
</ ul>
<div id = "firstPage" class = "hide">
<a href = "#"> 2.57 million purchase of Changping Lintie Sanju, a total price of 200,000 buy a home </a> <br/>
<a href = "#"> Built 5 million three homes within 2 million, 1.4 million Anjiadong third ring </a> <br/>
<a href = "#"> Beijing's first real estate with zero down payment for purchase of 530,000 East 5 ring 50 flat </a> <br/>
<a href = "#"> Beijing Real Estate goes straight down 5000 CITIC House Park House King's existing house </a> <br/>
</ div>
<div id = "secondPage" class = "show">
<a href = "#"> 40 flat rental house remodeled beautiful girl's mix and nest </a> <br/>
<a href = "#"> Classic and fresh Jane Oijia 90-story old house rejuvenates </a> <br/>
<a href = "#"> New Chinese style cool color tenderness 66 flat hit color lively home </a> <br/>
<a href = "#"> Tiles are like choosing your wife ’s bathroom flue design </a> <br/>
</ div>
<div id = "thirdPage" class = "hide">
<a href = "#"> Tongzhou luxury 3 bedroom 2.6 million second ring scarcity 2 bedroom 250w dump </a> <br/>
<a href = "#"> West 3rd Ring Transparent 2 Houses 2.9 million 1.3 Million 2 Houses Limited Sale </a> <br/>
<a href = "#"> Huangchenggen Elementary School District is only 2.6 million, 121 and 700,000 throws! </a> <br/>
<a href = "#"> Exclusive villa price of 2.8 million Suzhou Bridge 2 bedroom special price 2.48 million </a> <br/>
</ div>
</ div>
</ body>
</ html>
[Frontend] html+css+javascript implementing tab transitions