未加星标

Custom TAB menu using two lines of javascript

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Learn One of the simplest way of doing the tab navigation menu for your next project using simple javascript code and CSS

Common Issues

Adding the tab menu for your website may be a prolonged process, because you need to search and download the plugin's for the tab menu and integrate them into your website.

The plugin's for tab menu or some other ways of doing the tab menu may contains lengthy JavaScript code. In this tab menu tutorial the JavaScript code is simplified to achieve the same.

Creating The Structure - Using Html

The HTML code to achieve the basic structure of the tab menu is shown below (example).

<div class="tab-container"> <div class="tab-menu"> <ul> <li> <a href="#" class="tab-a active-a" data-id="tab1">TAB-1 </a> </li> <li> <a href="#" class="tab-a" data-id="tab2">TAB-2 </a> </li> <li> <a href="#" class="tab-a" data-id="tab3">TAB-3 </a> </li> </ul> </div> <!--end of tab-menu--> <div class="tab tab-active" data-id="tab1"> <h2>Ut cursus nisi luctus sem sollicitudin porta. </h2> <p>Nullam urna mi, euismod nec lacus eget, viverra dictum augue. Donec ut tellus risus. Vivamus dignissim, augue vitae fermentum finibus, ipsum mi efficitur sapien, id feugiat nulla felis id massa. Morbi cursus euismod turpis pretium convallis. Praesent laoreet nunc in sem convallis volutpat. </p> </div> <!--end of tab one--> <div class="tab " data-id="tab2"> <h2>Euismod nec lacus eget, viverra dictum augue. </h2> <p>Maecenas vitae ornare est. Praesent suscipit tellus ante, sed elementum enim commodo a. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Praesent id ex sollicitudin. </p> </div> <!--end of tab two--> <div class="tab " data-id="tab3"> <h2>Eed elementum enim commodo a </h2> <p>Vel dolor eu efficitur. Ut convallis sit amet diam nec vulputate. Aenean elementum ac magna vitae eleifend. Vestibulum felis est, dictum sed lacus vitae, egestas fermentum nisl. Praesent commodo massa sit amet ante dignissim pulvinar. Nulla semper orci arcu, id venenatis leo posuere ultricies. </p> </div> <!--end of tab three--> </div> <!--end of container-->

Add Styling - Using Css

The following are the Css to achieve the styles for the tab menu. Here the .tab-a is the tab button and the .tab is the container of the all tabs in your design. both the .tab-a and the .tab have one more secret class to make the tab and tab menu active and inactive.

body{ margin: 0; padding: 0; height: 100%; background-color: #c7eae0; font-family: 'sans-serif', sans-serif; } .tab-container{ margin: 5% 10%; background-color: #c1e3d9; padding: 3%; border-radius: 4px; } .tab-menu{} .tab-menu ul{ margin: 0; padding: 0; } .tab-menu ul li{ list-style-type: none; display: inline-block; } .tab-menu ul li a{ text-decoration: none; color: rgba(0,0,0,0.4); background-color: #b4cbc4; padding: 7px 25px; border-radius: 4px; } .tab-menu ul li a.active-a{ background-color: #588d7d; color: #ffffff; } .tab{ display: none; } .tab h2{ color: rgba(0,0,0,.7); } .tab p{ color: rgba(0,0,0,0.6); text-align: justify; } .tab-active{ display: block; }

Handling The Event - Using JavaScript

The following are the JavaScript code to achieve the tab navigation menu for your website.

$(document).ready(function(){ /*creating click event for each tab menu*/ $(document.body).on("click",".tab-a",function(){ $(".tab").removeClass('tab-active'); /*adding the active class for the current selected tab*/ $(".tab[data-id='"+$(this).attr('data-id')+"']").addClass("tab-active"); $(".tab-a").removeClass('active-a'); /*adding the active class for the current selected tab menu*/ $(this).parent().find(".tab-a").addClass('active-a'); }); }); Live Demo

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaJavaScriptCSSHTML
分页:12
转载请注明
本文标题:Custom TAB menu using two lines of javascript
本站链接:http://www.codesec.net/view/483369.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(40)