This article mainly shares several sample codes for tab switching. It has a very good reference value. Let’s take a look with the editor below
1. Move the mouse in and out to switch
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>tab切换</title> <style type="text/css"> * {padding: 0;margin: 0;} li {list-style: none;} .wrapper { margin: 0 auto; width: 100%; max-width: 1140px; } .tabbox { margin: 40px auto; width: 400px; height: 200px; border: 1px solid #f70; overflow: hidden; } .tabbox .tab-tit{ position: relative; height: 40px; } ul { position: absolute; left: -1px; width: 401px; height: 40px; line-height: 40px; background-color: #eaeaea; } ul li { float: left; border-left: 1px solid #f70; border-bottom: 1px solid #f70; text-align: center; width: 99px; height: 40px; overflow: hidden; } .clear {clear: both;} .select { padding-right: 1px; border-bottom: none; background-color: #fff; } a:link, a:visited { font-size: 16px; font-weight: bold; color: #888; text-decoration: none; } .select a { color: #333; } a:hover, a:active { color: #f20; font-weight: bold; } .tab-txt { width: 400px; padding: 40px; overflow: hidden; } .demo {display: none;} .tab-txt p { line-height: 40px; } </style> </head> <body> <p class="wrapper"> <p id="tabBox" class="tabbox"> <p id="tabTit" class="tab-tit"> <ul> <li class="select"><a href="javascript:;">女枪</a></li> <li><a href="javascript:;">提莫</a></li> <li><a href="javascript:;">盖伦</a></li> <li><a href="javascript:;">剑圣</a></li> </ul> </p> <!-- <p class="clear"></p> --> <p id="tabTxt" class="tab-txt"> <p class="demo" style="display: block;"> <p>我有两把枪,一把叫射,另一把叫,啊~</p> <p>你有一双迷人的眼睛,我非常喜欢!</p> </p> <p class="demo"> <p>我去前面探探路</p> <p>提莫队长正在待命!</p> </p> <p class="demo"> <p>放马过来吧,你会死的很光荣的!</p> <p>快点儿结束吧,我头有点儿转晕了……</p> </p> <p class="demo"> <p>我的剑就是你的剑。</p> <p>眼睛多,看东西才会更加清楚</p> </p> </p> </p> </p> <script type="text/javascript"> function $(id) { return typeof id === "string" ? document.getElementById(id) : id; } window.onload = function() { var tits = $("tabTit").getElementsByTagName("li"); var txts = $("tabTxt").getElementsByClassName("demo"); if(tits.length != txts.length) {return;} for(var i=0,l=tits.length; i<l; i++) { tits[i].id = i; tits[i].onmouseover = function() { for(var j=0; j<l; j++) { tits[j].className = ""; txts[j].style.display = "none"; } this.className = "select"; txts[this.id].style.display = "block"; } } } </script> </body> </html>
2. Delayed switching when the mouse moves in and out
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>tab切换之延时切换</title> <style type="text/css"> * {padding: 0;margin: 0;} li {list-style: none;} .wrapper { margin: 0 auto; width: 100%; max-width: 1140px; } .tabbox { margin: 40px auto; width: 400px; height: 200px; border: 1px solid #f70; overflow: hidden; } .tabbox .tab-tit{ position: relative; height: 40px; } ul { position: absolute; left: -1px; width: 401px; height: 40px; line-height: 40px; background-color: #eaeaea; } ul li { float: left; border-left: 1px solid #f70; border-bottom: 1px solid #f70; text-align: center; width: 99px; height: 40px; overflow: hidden; } .clear {clear: both;} .select { padding-right: 1px; border-bottom: none; background-color: #fff; } a:link, a:visited { font-size: 16px; font-weight: bold; color: #888; text-decoration: none; } .select a { color: #333; } a:hover, a:active { color: #f20; font-weight: bold; } .tab-txt { width: 400px; padding: 40px; overflow: hidden; } .demo {display: none;} .tab-txt p { line-height: 40px; } </style> </head> <body> <p class="wrapper"> <p id="tabBox" class="tabbox"> <p id="tabTit" class="tab-tit"> <ul> <li class="select"><a href="javascript:;">女枪</a></li> <li><a href="javascript:;">提莫</a></li> <li><a href="javascript:;">盖伦</a></li> <li><a href="javascript:;">剑圣</a></li> </ul> </p> <!-- <p class="clear"></p> --> <p id="tabTxt" class="tab-txt"> <p class="demo" style="display: block;"> <p>我有两把枪,一把叫射,另一把叫,啊~</p> <p>你有一双迷人的眼睛,我非常喜欢!</p> </p> <p class="demo"> <p>我去前面探探路</p> <p>提莫队长正在待命!</p> </p> <p class="demo"> <p>放马过来吧,你会死的很光荣的!</p> <p>快点儿结束吧,我头有点儿转晕了……</p> </p> <p class="demo"> <p>我的剑就是你的剑。</p> <p>眼睛多,看东西才会更加清楚</p> </p> </p> </p> </p> <script type="text/javascript"> function $(id) { return typeof id === "string" ? document.getElementById(id) : id; } window.onload = function() { var timer = null; var tits = $("tabTit").getElementsByTagName("li"); var txts = $("tabTxt").getElementsByClassName("demo"); if(tits.length != txts.length) {return;} for(var i=0,l=tits.length; i<l; i++) { tits[i].id = i; tits[i].onmouseover = function() { var that = this; if(timer) { clearTimeout(timer); timer = null; } timer = setTimeout(function() { for(var j=0; j<l; j++) { tits[j].className = ""; txts[j].style.display = "none"; } that.className = "select"; txts[that.id].style.display = "block"; },500); } } } </script> </body> </html>
3. Tab automatically switches when the mouse moves in Move out and switch immediately
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>tab切换之自动切换</title> <style type="text/css"> * {padding: 0;margin: 0;} li {list-style: none;} .wrapper { margin: 0 auto; width: 100%; max-width: 1140px; } .tabbox { margin: 40px auto; width: 400px; height: 200px; border: 1px solid #f70; overflow: hidden; } .tabbox .tab-tit{ position: relative; height: 40px; } ul { position: absolute; left: -1px; width: 401px; height: 40px; line-height: 40px; background-color: #eaeaea; } ul li { float: left; border-left: 1px solid #f70; border-bottom: 1px solid #f70; text-align: center; width: 99px; height: 40px; overflow: hidden; } .clear {clear: both;} .select { padding-right: 1px; border-bottom: none; background-color: #fff; } a:link, a:visited { font-size: 16px; font-weight: bold; color: #888; text-decoration: none; } .select a { color: #333; } a:hover, a:active { color: #f20; font-weight: bold; } .tab-txt { width: 400px; padding: 40px; overflow: hidden; } .demo {display: none;} .tab-txt p { line-height: 40px; } </style> </head> <body> <p class="wrapper"> <p id="tabBox" class="tabbox"> <p id="tabTit" class="tab-tit"> <ul> <li class="select"><a href="javascript:;">女枪</a></li> <li><a href="javascript:;">提莫</a></li> <li><a href="javascript:;">盖伦</a></li> <li><a href="javascript:;">剑圣</a></li> </ul> </p> <!-- <p class="clear"></p> --> <p id="tabTxt" class="tab-txt"> <p class="demo" style="display: block;"> <p>我有两把枪,一把叫射,另一把叫,啊~</p> <p>你有一双迷人的眼睛,我非常喜欢!</p> </p> <p class="demo"> <p>我去前面探探路</p> <p>提莫队长正在待命!</p> </p> <p class="demo"> <p>放马过来吧,你会死的很光荣的!</p> <p>快点儿结束吧,我头有点儿转晕了……</p> </p> <p class="demo"> <p>我的剑就是你的剑。</p> <p>眼睛多,看东西才会更加清楚</p> </p> </p> </p> </p> <script type="text/javascript"> function $(id) { return typeof id === "string" ? document.getElementById(id) : id; } window.onload = function() { var index = 0; var timer = null; var tits = $("tabTit").getElementsByTagName("li"); var txts = $("tabTxt").getElementsByClassName("demo"); if(tits.length != txts.length) {return;} for(var i=0,l=tits.length; i<l; i++) { tits[i].id = i; tits[i].onmouseover = function() { clearInterval(timer); styleFun(this.id); } tits[i].onmouseout = function() { timer = setInterval(autoPlay, 2000); } } //在开启定时器的同时清楚定时器并置空 if(timer) { clearInterval(timer); timer = null; } timer = setInterval(autoPlay, 2000); function autoPlay() { index++; if(index >= tits.length) { index = 0; } styleFun(index); } function styleFun(ele) { for(var j=0,m=tits.length; j<m; j++) { tits[j].className = ""; txts[j].style.display = "none"; } tits[ele].className = "select"; txts[ele].style.display = "block"; //将鼠标移入移出时的index传给autoPlay; index = ele; } } </script> </body> </html>
##4. Advertising column switching example
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> * { margin: 0; padding: 0; list-style: none; } .wrap { height: 170px; width: 490px; margin: 20px auto; overflow: hidden; position: relative; margin: 100px auto; } .wrap ul { position: absolute; } .wrap ul li { height: 170px; } .wrap ol { position: absolute; right: 5px; bottom: 10px; } .wrap ol li { height: 20px; width: 20px; background: #ccc; border: solid 1px #666; margin-left: 5px; color: #000; float: left; line-height: center; text-align: center; cursor: pointer; } .wrap ol .on { background: #E97305; color: #fff; } </style> <script type="text/javascript"> window.onload = function() { var wrap = document.getElementById('wrap'), pic = document.getElementById('pic'), piclist = pic.getElementsByTagName('li'), list = document.getElementById('list').getElementsByTagName('li'), picheight = 170, index = 0, timer = null; if(piclist.length != list.length) { return; } // 定义并调用自动播放函数 if(timer) { clearInterval(timer); timer = null; } timer = setInterval(picFunc, 2000); function picFunc() { index++; if(index >= piclist.length) { index = 0; } changePic(index); } // 定义图片切换函数 function changePic(ele) { for(var j = 0, m = piclist.length; j < m; j++) { list[j].className = ""; } pic.style.top = -ele * picheight + "px"; list[ele].className = "on"; index = ele; } // 鼠标划过整个容器时停止自动播放 wrap.onmouseover = function() { clearInterval(timer); } // 鼠标离开整个容器时继续播放至下一张 wrap.onmouseout = function() { timer = setInterval(picFunc, 2000); } // 遍历所有数字导航实现划过切换至对应的图片 for(var i = 0, l = list.length; i < l; i++) { list[i].id = i; list[i].onmouseover = function() { changePic(this.id); } } } </script> </head> <body> <p class="wrap" id='wrap'> <ul id="pic"> <li><img src="/static/imghwm/default1.png" data-src="http://img.mukewang.com/54111cd9000174cd04900170.jpg" class="lazy" alt=""></li> <li><img src="/static/imghwm/default1.png" data-src="http://img.mukewang.com/54111dac000118af04900170.jpg" class="lazy" alt=""></li> <li><img src="/static/imghwm/default1.png" data-src="http://img.mukewang.com/54111d9c0001998204900170.jpg" class="lazy" alt=""></li> <li><img src="/static/imghwm/default1.png" data-src="http://img.mukewang.com/54111d8a0001f41704900170.jpg" class="lazy" alt=""></li> <li><img src="/static/imghwm/default1.png" data-src="http://img.mukewang.com/54111d7d00018ba604900170.jpg" class="lazy" alt=""></li> </ul> <ol id="list"> <li class="on">1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ol> </p> </body> </html>For more detailed explanations of various tab switching related articles, please pay attention to the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use
