There are many various page effects on today's pages. Commonly used ones include pop-up layer effects, seamless scrolling effects, and tab switching effects. 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:
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 = ''; } } }
Note: 1. Titles such as 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 effects I wrote casually based on the javascript knowledge I learned when I was bored.
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