Home > Article > Web Front-end > Comparison of simple tab switching effects between native js and jQuery_javascript skills
Tab pages are usually suitable for situations where space is limited and there is a lot of content, while taking into account the beauty of the page and not giving users visual fatigue due to excessive information. It has a wide range of uses. Below we use two methods to simply implement it.
First, build the page elements. We usually use a list to carry the clickable part of the tab, including ul and ol. Here we make the tabs distributed horizontally, so we need to make them float to the left. The content of the page can be hosted in divs. In addition, we need to uniformly control the styles and behaviors of elements with common characteristics, so we have the following dom structure:
<div id="main"> <ul id="tabbar" class="cl"> <li class="t1">t1</li> <li class="def">t2</li> <li class="def">t3</li> <li class="def">t4</li> <li class="def">t5</li> </ul> <div id="content"> <div class="cont t1">Hi !</div> <div class="cont t2">I Like You!</div> <div class="cont t3">Hello World!</div> <div class="cont t4">How Are You?</div> <div class="cont t5">I'm fine ,and you?</div> </div> </div>
After ul floats left, in order to clear the impact of floating on subsequent elements, the clear attribute is set through the after pseudo-class, and at the same time, the lower version of ie is added to zoom to trigger ie haslayout. So we have the following style:
html,body,div,ul,li{margin:0; padding:0; } .cl{zoom:1;} .cl:after{display:block; height:0; clear:both; visibility:hidden; overflow:hidden; content:'.';} ul{list-style:none;} body{padding-top:100px; background:#eee; font-family:Microsoft YaHei, Arial, Helvetica, sans-serif;} #main{margin:0 auto; width:800px;} #main #tabbar{} #main #tabbar li,#main #content .cont{text-align:center; color:#fff;} #main #tabbar li{padding:0 20px; height:35px; line-height:35px; font-size:14px; cursor:pointer; float:left;} #main #content{height:350px; overflow:hidden; position:relative;} #main #content .cont{width:100%; height:350px; line-height:350px; font-size:48px; z-index:0; position:absolute;} #main #tabbar li.def{color:#333; background:#fff;} #main #tabbar li.t1,#main #content .cont.t1{color:#fff; background:#4e6b9c;} #main #tabbar li.t2,#main #content .cont.t2{color:#fff; background:#c52946;} #main #tabbar li.t3,#main #content .cont.t3{color:#fff; background:#33a6ff;} #main #tabbar li.t4,#main #content .cont.t4{color:#fff; background:#ffab4e;} #main #tabbar li.t5,#main #content .cont.t5{color:#fff; background:#64bccc;}
The html part is roughly the same.
When implemented using native js, we mainly bind click events to each li separately. Click to display the current content page and hide other content pages. The process of showing and hiding continues to increase and decrease the transparency of the content through a timer until it is completely Hide or show.
window.onload = function(){ var tabs = document.getElementById("tabbar").getElementsByTagName("li"); var cont = document.getElementById("content").getElementsByTagName("div"); var len = cont.length; var flag = true; var fade = function(elem, callback, type){ type || (type = "in"); var px, timer; if(type == "in") { px = 0; timer = setInterval(function(){ px += 3; if(px <= 100) { elem.style.opacity ? (elem.style.opacity = (px / 100)) : (elem.style["filter"] = "alpha(opacity=" + px + ")"); } else { clearInterval(timer); elem.style.opacity ? (elem.style.opacity = 1) : (elem.style["filter"] = "alpha(opacity=100)"); callback && callback(elem); } },10); } else { px = 100; timer = setInterval(function(){ px -= 3; if(px >= 0) { document.addEventListener ? (elem.style.opacity = (px / 100)) : (elem.style["filter"] = "alpha(opacity=" + px + ")"); } else { clearInterval(timer); elem.style.opacity ? (elem.style.opacity = 0) : (elem.style["filter"] = "alpha(opacity=0)"); callback && callback(elem); } },10); } } for(var i = 0; i < len; i++) { cont[i].style.zIndex = len - i; tabs[i].index = cont[i].index = i; tabs[i].onclick = function(){ if(flag) { flag = false; cont[this.index].style.display = "block"; fade(cont[this.index]); for(var i = 0; i < len; i++) { tabs[i].className = "def"; if(tabs[i].index != this.index) { fade ( cont[i], function(elem) { elem.style.display = "none"; elem.className = "cont t" + (elem.index + 1); flag = true; }, "out" ); } } this.className = "t" + (this.index + 1); } } } };
As can be seen from the above, it is actually quite troublesome to use native js to operate dom, otherwise the "write less, do more" jQuery would not have been born. The following is a simple implementation using jQuery:
$(function(){ var tabs = $("#tabbar li"); var cont = $("#content .cont"); var len = cont.length; cont.each(function(inx, elem){$(elem).css("z-index", len - inx);}).andSelf().hide().andSelf().eq(1).show(); tabs.click(function(){ var inx = tabs.index(this); tabs.removeAttr("class").addClass("def").andSelf().eq(inx + 1).addClass("t" + (inx + 1)); cont.fadeOut(300).not(this).andSelf().eq(inx).fadeIn(300); }); } );
This example is relatively simple, but very practical. Of course, in actual work, we generally don't write it like this. We usually use it as a basis to encapsulate a reusable control, but the basic idea remains the same.
The above is the entire content of this article, I hope you all like it.