The example in this article describes the simple demonstration code of jQuery to achieve the tab switching effect. Share it with everyone for your reference. The details are as follows:
The operation effect diagram is as follows
1. Main program
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>选项卡</title> <link type="text/css" rel="stylesheet" href="css/layout.css" /> </head> <body> <!--整体构局说明,用ul完成按钮的横向布局,用div完成三个内容框架的垂直布局(类似于类表),然后将三个内容框架全隐藏,通过下面的:first-child属性只将第一个框架内容显示出来--> <div class="tab"> <div class="tab_menu"> <ul> <li class="on">实事</li> <li>政治</li> <li>体育</li> </ul> </div> <div class="tab_box"> <div>实事内容</div> <div>政治内容</div> <div>体育内容</div> </div> </div> <script type="text/javascript" src="js/jquery-1.11.3.js"></script> <script type="text/javascript" src="js/layout.js"></script> </body> </html>
2. CSS style
Preliminary layout code:
*{ margin:0; padding:0} ul{ list-style: none; } .tab{ width: 300px; margin: 0 auto; } .tab .tab_menu{ border: 1px solid #000000; height: 30px; width: 300px; } .tab .tab_menu ul li{ float: left; width: 99px; text-align: center; line-height: 30px; border-right: 1px #333333 solid; } .tab .tab_menu ul li:last-child{ border-right:none; width: 100px; } .tab .tab_menu ul li.on{ background: #999; } .tab .tab_box > div{ width: 300px; height: 200px; border: #333333 solid; border-width: 0 1px 1px 1px; }
The above code implements the layout as follows:
But we only need the content in a frame to be displayed, so just add some small code to assist with the above code~~~~~~
Further layout style code:
*{ margin:0; padding:0} ul{ list-style: none; } .tab{ width: 300px; margin: 0 auto; } .tab .tab_menu{ border: 1px solid #000000; height: 30px; width: 300px; } .tab .tab_menu ul li{ float: left; width: 99px; text-align: center; line-height: 30px; border-right: 1px #333333 solid; } .tab .tab_menu ul li:last-child{ border-right:none; width: 100px; } .tab .tab_menu ul li.on{ background: #999; } .tab .tab_box > div{ width: 300px; height: 200px; border: #333333 solid; border-width: 0 1px 1px 1px; display: none; //将三个内容框架全隐藏,通过下面的:first-child属性只将第一个框架内容显示出来 } .tab .tab_box > div:first-child{ display: block; }
The above program adds an additional display: none to the .tab .tab_box > div style. In addition, only the first frame content is displayed through the :first-child attribute~~~~~~This is what we see The layout is consistent with the animation renderings I just posted above, and the layout is considered complete~~~~~~
3. Jquery code:
$(function(){ $(".tab_menu ul li").click(function(){ $(this).addClass("on").siblings().removeClass("on"); //切换选中的按钮高亮状态 var index=$(this).index(); //获取被按下按钮的索引值,需要注意index是从0开始的 $(".tab_box > div").eq(index).show().siblings().hide(); //在按钮选中时在下面显示相应的内容,同时隐藏不需要的框架内容 }); });
I hope this article will be helpful to everyone learning jquery programming.