Advertisement
ten80snowboarder

Simple Tabs w/ CSS & jQuery

Jan 21st, 2012
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. HTML
  2. =================================================
  3. <ul class="tabs">
  4. <li><a href="#tab-1">Gallery</a></li>
  5. <li><a href="#tab-2">Submit</a></li>
  6. </ul>
  7.  
  8. <div class="tab_container">
  9. <div id="tab-1" class="tab_content">
  10. <!--Content-->
  11. </div>
  12. <div id="tab-2" class="tab_content">
  13. <!--Content-->
  14. </div>
  15. </div>
  16.  
  17. CSS
  18. =================================================
  19. ul.tabs {
  20. margin: 0;
  21. padding: 0;
  22. float: left;
  23. list-style: none;
  24. height: 32px; /*--Set height of tabs--*/
  25. border-bottom: 1px solid #999;
  26. border-left: 1px solid #999;
  27. width: 100%;
  28. }
  29. ul.tabs li {
  30. float: left;
  31. margin: 0;
  32. padding: 0;
  33. height: 31px; /*--Subtract 1px from the height of the unordered list--*/
  34. line-height: 31px; /*--Vertically aligns the text within the tab--*/
  35. border: 1px solid #999;
  36. border-left: none;
  37. margin-bottom: -1px; /*--Pull the list item down 1px--*/
  38. overflow: hidden;
  39. position: relative;
  40. background: #e0e0e0;
  41. }
  42. ul.tabs li a {
  43. text-decoration: none;
  44. color: #000;
  45. display: block;
  46. font-size: 1.2em;
  47. padding: 0 20px;
  48. border: 1px solid #fff; /*--Gives the bevel look with a 1px white border inside the list item--*/
  49. outline: none;
  50. }
  51. ul.tabs li a:hover {
  52. background: #ccc;
  53. }
  54. html ul.tabs li.active, html ul.tabs li.active a:hover { /*--Makes sure that the active tab does not listen to the hover properties--*/
  55. background: #fff;
  56. border-bottom: 1px solid #fff; /*--Makes the active tab look like it's connected with its content--*/
  57. }
  58. .tab_container {
  59. border: 1px solid #999;
  60. border-top: none;
  61. overflow: hidden;
  62. clear: both;
  63. float: left; width: 100%;
  64. background: #fff;
  65. }
  66. .tab_content {
  67. padding: 20px;
  68. font-size: 1.2em;
  69. }
  70.  
  71. jQuery
  72. =================================================
  73. $(document).ready(function() {
  74.  
  75. //When page loads...
  76. $(".tab_content").hide(); //Hide all content
  77. $("ul.tabs li:first").addClass("active").show(); //Activate first tab
  78. $(".tab_content:first").show(); //Show first tab content
  79.  
  80. //On Click Event
  81. $("ul.tabs li").click(function() {
  82.  
  83. $("ul.tabs li").removeClass("active"); //Remove any "active" class
  84. $(this).addClass("active"); //Add "active" class to selected tab
  85. $(".tab_content").hide(); //Hide all tab content
  86.  
  87. var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
  88. $(activeTab).fadeIn(); //Fade in the active ID content
  89. return false;
  90. });
  91.  
  92. /* Allow a hashtag to be passed in the URL so any tab can be loaded directly */
  93. if ( location.hash.length > 4 && location.hash.substr(0,5) == '#tab-' ) {
  94. $('.tabs li a').each(function(i){
  95. if ( $(this).attr('href') == location.hash ) {
  96. $(this).trigger('click');
  97. }
  98. });
  99. }
  100.  
  101. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement