This article shares the specific code of JavaScript to achieve the tab switching effect for your reference. The specific content is as follows Build the main interface HTML Code <h1>Realize the tab switching effect</h1> <ul id="tab"> <li><a href="">Movies and TV</a></li> <li><a href="">Entertainment</a></li> <li><a href="">TV Series</a></li> </ul> <div id="content"> <div id="content1">Latest movie recommendations:<br>"Fast and Furious"<br>"Big Hero 6"</div> <div id="content2">Hot entertainment recommendations:<br>"Running Man"<br>"The Voice of China"</div> <div id="content3">Popular TV series recommendations:<br>"Three Lives, Three Worlds"<br>"Our Times"</div> </div> Writing CSS files Create a new CSS file and write CSS code to render the HTML interface written previously. Remember to import the CSS file in the HTML file. <link rel="stylesheet" href="mCSS.css" > CSS file code *{ margin: 0; padding: 0; } #tab { overflow: hidden; } #tab li { float: left; list-style: none; width: 80px; height: 40px; text-align: center; } #tab li:first-child, #content1 { background: #ffcc00; } #tab li:first-child + li, #content2 { background: #ff00cc; } #tab li:last-child, #content3 { background: #00ccff; } #tab li a { line-height: 40px; color: white; text-decoration: none; } #content { position: relative; } #content1, #content2, #content3 { width: 300px; height: 100px; position: absolute; top: 0; left: 0; padding: 30px; display: none; } #content1{ display: block; } Write JavaScript script files to achieve switching effects JavaScript code // When a certain label is clicked, the corresponding div is displayed, and the others are hidden. // Find the element that triggered the event var as = document.querySelectorAll("#tab a"); // Bind event processing function for (var i = 0; i < as.length; i++) { as[i].onclick = function () { // Hide all divs var divs = document.querySelectorAll("#content>div"); for (var i = 0; i < divs.length; i++) { divs[i].style.display = "none"; } // Let the corresponding div display // Get the href of the current a var i = this.href.lastIndexOf("#"); var id = this.href.slice(i); console.log(id) document.querySelector(id).style.display = "block"; } } Complete case code mHTML.html <link rel="stylesheet" href="mCSS.css" > <h1>Realize the tab switching effect</h1> <ul id="tab"> <li><a href="#content1" >Movies and TV</a></li> <li><a href="#content2" >Entertainment</a></li> <li><a href="#content3" >TV series</a></li> </ul> <div id="content"> <div id="content1">Latest movie recommendations:<br>"Fast and Furious"<br>"Big Hero 6"</div> <div id="content2">Hot entertainment recommendations:<br>"Running Man"<br>"The Voice of China"</div> <div id="content3">Popular TV series recommendations:<br>"Three Lives, Three Worlds"<br>"Our Times"</div> </div> <script src="mJS.js"></script> mCSS.css *{ margin: 0; padding: 0; } #tab { overflow: hidden; } #tab li { float: left; list-style: none; width: 80px; height: 40px; text-align: center; } #tab li:first-child, #content1 { background: #ffcc00; } #tab li:first-child + li, #content2 { background: #ff00cc; } #tab li:last-child, #content3 { background: #00ccff; } #tab li a { display: block; width: 100%; height: 100%; line-height: 40px; color: white; text-decoration: none; } #content { position: relative; } #content1, #content2, #content3 { width: 300px; height: 100px; position: absolute; top: 0; left: 0; padding: 30px; display: none; } #content1{ display: block; } mJS.js // When a certain label is clicked, the corresponding div is displayed, and the others are hidden. // Find the element that triggered the event var as = document.querySelectorAll("#tab a"); // Bind event processing function for (var i = 0; i < as.length; i++) { as[i].onclick = function () { // Hide all divs var divs = document.querySelectorAll("#content>div"); for (var i = 0; i < divs.length; i++) { divs[i].style.display = "none"; } // Let the corresponding div display // Get the href of the current a var i = this.href.lastIndexOf("#"); var id = this.href.slice(i); console.log(id) document.querySelector(id).style.display = "block"; } } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to center images horizontally and vertically in DIV or DIV
>>: Paragraph layout and line breaks in HTML web pages
1. System installation package yum -y install mak...
Carousel animation can improve the appearance and...
A distinct Meaning: distinct is used to query the...
When the amount of data in MySQL is large, limit ...
Table of contents What is a Promise? Usage of rej...
Table of contents 1. Write in front 2. Overlay to...
Change the default style of select, usually throug...
It was found in the test that when the page defini...
When using the idea development tool to debug cod...
background nginx-kafka-module is a plug-in for ng...
I updated MySQL 8.0 today. The first problem: Nav...
Often, we may need to export local database data ...
2048 mini game, for your reference, the specific ...
Table of contents 1. Introduction to NFS 2. NFS C...
I recently wrote a mobile page at work, which was...