Home >Web Front-end >JS Tutorial >js implements simple sliding door menu (tab) effect code_javascript skills
The example in this article describes the implementation of a simple sliding door menu in js. Share it with everyone for your reference. The details are as follows:
A simple and practical web tab menu that implements two tabs on the same page. The first is a sliding door, which is basically similar in terms of layout. The second is a tab, which is mainly used here. Js custom function: tabMenu, function function: implement tab menu, parameter description: tabMenu(tabBox,navClass);
Parameter 1: tabBox (tab container id)
Parameter two: navClass (current label style class)
Note: Depends on the specified html structure.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-simple-scroll-menu-tab-codes/
The specific code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>简洁的选项卡菜单</title> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <style type="text/css"> ul,li{margin:0;padding:0;} .tabBox{width:268px;margin:20px;} ul.tabTag{height:28px;border-bottom:1px solid #000;list-style:none} ul.tabTag li{float:left;line-height:27px;height:27px;padding:0 18px;color:#ccc;border:1px solid #ccc;border-bottom:none;margin-right:5px;background:#fff;cursor:pointer;} ul.tabTag li.active{border-color:#000;background:#eee;color:red;position:relative;top:1px;} .tabCon{border:1px solid #000;border-top:none;background:#fff;} .tCon{display:none;background:#eee;padding:25px;} </style> <script type="text/javascript"> function tabMenu(tabBox,navClass){ var tabNavLi=document.getElementById(tabBox).getElementsByTagName("ul")[0].getElementsByTagName("li"); var tabCon=document.getElementById(tabBox).getElementsByTagName("div")[0].getElementsByTagName("div"); var tabLens=tabCon.length; for(var i=0;i<tabLens;i++){ //应用js闭包传入参数i作为当前索引值赋值给m (function(m){ tabNavLi[m].onmouseover = function(){ for(var j=0; j<tabLens; j++){ tabNavLi[j].className = (j==m)?navClass:""; tabCon[j].style.display = (j==m)?"block":""; } } })(i); } } //函数调用 window.onload=function(){ tabMenu("tabBox1","active"); tabMenu("tabBox2","active"); } </script> </head> <body> <!--demo1--> <div id="tabBox1" class="tabBox"> <ul class="tabTag"> <li class="active">新闻1</li> <li>体育1</li> <li>财经1</li> </ul> <div class="tabCon"> <div class="tCon" style="display:block">新闻1内容</div> <div class="tCon">体育1内容</div> <div class="tCon">财经1内容</div> </div> </div> <!--demo2--> <div id="tabBox2" class="tabBox"> <ul class="tabTag"> <li class="active">新闻2</li> <li>体育2</li> <li>财经2</li> </ul> <div class="tabCon"> <div class="tCon" style="display:block">新闻2内容</div> <div class="tCon">体育2内容</div> <div class="tCon">财经2内容</div> </div> </div> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.