Tabbed content

From MODx Wiki

Jump to: navigation, search

Short tutorial on how to achieve a tabbed effect such as seen on http://www.jadralniklub-burja.si/index.php?id=13

This tutorial uses a few basic things like CSS styling, HTML structuring and some javascript. In this example we'll set up three tabs for three different pages.

Contents

HTML structure

Create placeholders

We split the content in two basic parts: navigation (tabs) and data (pages). We use div elements and set the id attribute to both. We add an extra wrapper, so we can style only specific elements.

  1. <div id="tabs">
  2.  
  3. <div id="nav">
  4. </div>
  5.  
  6. <div id="data">
  7. </div>
  8.  
  9. </div>

Create tabs

We create navigation tabs as list items and we create different divs for data pages:

  1. <div id="tabs">
  2. <div id="nav">
  3. <li>Tab one</li>
  4. <li>Tab two</li>
  5. <li>Tab three</li>
  6. </ul>
  7. </div>
  8. <div id="data">
  9. <div id="one">
  10. Page one
  11. </div>
  12. <div id="two">
  13. Page two
  14. </div>
  15. <div id="three">
  16. Page three
  17. </div>
  18. </div>
  19. </div>

Now we are ready with the HTML structure and we must style the pages a bit. This is the current look:

Image:Tabs1.png


CSS styling

Style the tabs

  1. #tabs {
  2. padding: 10px;
  3. }
  4.  
  5. #tabs #nav {
  6. margin-bottom: 0px;
  7. margin-left: 8px;
  8. }
  9.  
  10. #tabs ul {
  11. list-style: none;
  12. margin: 0px;
  13. padding: 0px;
  14. }
  15.  
  16. #tabs #nav ul li {
  17. display: inline;
  18. padding: 3px 7px;
  19. cursor: default;
  20. background-color: #eeeeee;
  21. border: 1px solid #cccccc;
  22. border-bottom: none;
  23. margin-right: 2px;
  24. }

Current look:

Image:Tabs2.png

Style active tabs and hovering effect

When a page is selected, the active tab will be "in front".

  1. #tabs #nav ul li.active {
  2. border: 1px solid #000066;
  3. border-bottom: 1px solid white;
  4. background-color: #ffffff;
  5. }

When we hover over an unselected tab, create an effect.

  1. #tabs #nav ul li:hover {
  2. background-color: #ffffff;
  3. }

Style the pages

  1. #tabs #data {
  2. margin-top: 3px;
  3. }
  4.  
  5. #tabs #data div {
  6. display: no+ne;
  7. border: 1px solid #000066;
  8. padding: 10px;
  9. }
  10.  
  11. #tabs #data div.active {
  12. display: block;
  13. }

All pages will be hidden when the page loads, except for the first one. So we mark it as active with a special class. We also mark the first tab as active the same way.

  1. <li class="active">Tab one</li>
  2. ....
  3. <div id="one" class="active">
  4. Page one
  5. </div>
  6. .....

Current state

Image:Tabs3.png

Respond to clicks

We have the content ready, now let's add some dynamics to the page.

Create javascript functions

We need the functions for:

  • set all tabs as inactive
  • hide all pages
  • show only active page

We insert the following javascript functions info our HTML code. The standards prefer if we do this in the <head> section.

  1. <script type="text/javascript" language="javascript">
  2. function navAllInactive() {
  3. oNav = document.getElementById('nav');
  4. oLis = oNav.getElementsByTagName('LI');
  5.  
  6. for (i = 0; i < oLis.length; i++) {
  7. oLis[i].className = '';
  8. }
  9. }
  10.  
  11. function hideAllData() {
  12. oData = document.getElementById('data');
  13. oDivs = oData.getElementsByTagName('DIV');
  14.  
  15. for (i = 0; i < oDivs.length; i++) {
  16. oDivs[i].style.display = 'none';
  17. }
  18. }
  19.  
  20. function doShow(o, pageId) {
  21. hideAllData();
  22. navAllInactive();
  23. o.className = 'active';
  24. oData = document.getElementById(pageId);
  25. oData.style.display = 'block';
  26.  
  27. return false;
  28. }
  29. </script>

Make clicks work

Let's link the functions with the html elements.

Whenever you click on a tab, the following chain of events should happen:

  • hide all pages
  • mark all tabs as inactive
  • show only target page
  • mark clicked tab as active

Respond to click

Link every tab with a specific page. (code snippet)

  1. <div id="nav">
  2. <li onclick="javascript:doShow(this, 'one');" class="active">Tab one</li>
  3. <li onclick="javascript:doShow(this, 'two');">Tab two</li>
  4. <li onclick="javascript:doShow(this, 'three');">Tab three</li>
  5. </ul>
  6. </div>

That's it

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Tabs</title>
  5. #tabs {
  6. padding: 10px;
  7. }
  8.  
  9. #tabs #nav {
  10. margin-bottom: 0px;
  11. margin-left: 8px;
  12. }
  13.  
  14. #tabs ul {
  15. list-style: none;
  16. margin: 0px;
  17. padding: 0px;
  18. }
  19.  
  20. #tabs #nav ul li {
  21. display: inline;
  22. padding: 3px 7px;
  23. cursor: default;
  24. background-color: #eeeeee;
  25. border: 1px solid #cccccc;
  26. border-bottom: none;
  27. margin-right: 2px;
  28. }
  29.  
  30. #tabs #nav ul li.active {
  31. border: 1px solid #000066;
  32. border-bottom: 1px solid white;
  33. background-color: #ffffff;
  34. }
  35.  
  36. #tabs #nav ul li:hover {
  37. background-color: #ffffff;
  38. }
  39.  
  40. #tabs #data {
  41. margin-top: 3px;
  42. }
  43.  
  44. #tabs #data div {
  45. display: no+ne;
  46. border: 1px solid #000066;
  47. padding: 10px;
  48. }
  49.  
  50. #tabs #data div.active {
  51. display: block;
  52. }
  53. </style>
  54.  
  55. <script type="text/javascript" language="javascript">
  56. function navAllInactive() {
  57. oNav = document.getElementById('nav');
  58. oLis = oNav.getElementsByTagName('LI');
  59.  
  60. for (i = 0; i < oLis.length; i++) {
  61. oLis[i].className = '';
  62. }
  63. }
  64.  
  65. function hideAllData() {
  66. oData = document.getElementById('data');
  67. oDivs = oData.getElementsByTagName('DIV');
  68.  
  69. for (i = 0; i < oDivs.length; i++) {
  70. oDivs[i].style.display = 'none';
  71. }
  72. }
  73.  
  74. function doShow(o, pageId) {
  75. hideAllData();
  76. navAllInactive();
  77. o.className = 'active';
  78. oData = document.getElementById(pageId);
  79. oData.style.display = 'block';
  80.  
  81. return false;
  82. }
  83. </script>
  84. </head>
  85. <div id="tabs">
  86. <div id="nav">
  87. <li onclick="javascript:doShow(this, 'one');" class="active">Tab one</li>
  88. <li onclick="javascript:doShow(this, 'two');">Tab two</li>
  89. <li onclick="javascript:doShow(this, 'three');">Tab three</li>
  90. </ul>
  91. </div>
  92. <div id="data">
  93. <div id="one" class="active"> Page one </div>
  94. <div id="two"> Page two </div>
  95. <div id="three"> Page three </div>
  96. </div>
  97. </div>
  98. </body>
  99. </html>

Tips

  1. Edit pages and only later add the display: no+ne attribute to #tabs #data div.
  2. Store your page content to chunks for better readability.

The code would look like this:

  1. <div id="data">
  2. <div id="one" class="active">{{tabsPageOne}}</div>
  3. <div id="two">{{tabsPageTwo}}</div>
  4. <div id="three">{{tabsPageThree}}</div>
  5. </div>


!!! IMPORTANT !!!

In the code above there is a CSS style for #tabs #data div which containts word "none", divided by a plus sign (+). It is there because of the wiki spam filter! Remove it when trying the code out in your browser!

Personal tools