Home > Web Front-end > JS Tutorial > body text

Javascript implements tab switching effect

高洛峰
Release: 2017-03-24 13:43:07
Original
1578 people have browsed it

There are many various page effects on the current page. Commonly used ones include pop-up layer effect, seamless scrolling effect, and tab switching effect. Today I will share a tab switching effect written by myself using native javascript. Due to my limited skills, please point out any problems.
The rendering is as follows:

Javascript implements tab switching effect

html code:

 
 
 
 
js-tabs 
 
 

首页 技术 生活 作品

首页首页首页首页首页首页首页首页首页首页

技术技术技术技术技术技术技术技术技术技术

生活生活生活生活生活生活生活生活生活生活

作品作品作品作品作品作品作品作品作品作品



11111 22222 33333

11111111111111111111111111111111111

222222222222222222222222222222222222

333333333333333333333333333333333333333

Copy after login

javascript code:

function tabs(id,trigger){ 
var tabsBtn = document.getElementById(id).getElementsByTagName('h2')[0].getElementsByTagName('a'); 
var tabsContent = document.getElementById(id).getElementsByTagName('p'); 
for(var i = 0,len = tabsBtn.length; i < len; i++){ 
tabsBtn[i].index = i; 
if(trigger == 'click'){ 
tabsBtn[i].onclick = function(){ 
clearClass(); 
this.className = 'on'; 
showContent(this.index); 
} 
}else if(trigger == 'mouseover'){ 
tabsBtn[i].onmouseover = function(){ 
clearClass(); 
this.className = 'on'; 
showContent(this.index); 
} 
} 
} 
function showContent(n){ 
for(var i = 0,len = tabsBtn.length; i < len; i++){ 
tabsContent[i].className = 'hide'; 
} 
tabsContent[n].className = 'tabs-content'; 
} 
function clearClass(){ 
for(var i = 0,len = tabsBtn.length; i < len; i++){ 
tabsBtn[i].className = ''; 
} 
} 
}
Copy after login

Note:
1. Title For example, homepage, technology, life and works are in h2 tags.
2. Use the class named on to display the current title. If you change it to another class such as selected, you need to modify the corresponding content in tabs.js.
3. The content corresponding to the title is in the p tag. There can be no more p tags within p tags.
PS: These are some results I wrote casually based on the javascript knowledge I learned when I was bored.

For more articles related to javascript implementing tabs switching effect, please pay attention to the PHP Chinese website!

Related articles:

WeChat Mini Program: How to implement tabs tab effect example

WeChat Mini Program tabs effect implementation

Simple and stylish pure CSS3 Tabs option card effects

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!