Tabbed content
From MODx Wiki
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.
Create tabs
We create navigation tabs as list items and we create different divs for data pages:
Now we are ready with the HTML structure and we must style the pages a bit. This is the current look:
CSS styling
Style the tabs
#tabs { padding: 10px; } #tabs #nav { margin-bottom: 0px; margin-left: 8px; } #tabs ul { list-style: none; margin: 0px; padding: 0px; } #tabs #nav ul li { display: inline; padding: 3px 7px; cursor: default; background-color: #eeeeee; border: 1px solid #cccccc; border-bottom: none; margin-right: 2px; }
Current look:
Style active tabs and hovering effect
When a page is selected, the active tab will be "in front".
#tabs #nav ul li.active { border: 1px solid #000066; border-bottom: 1px solid white; background-color: #ffffff; }
When we hover over an unselected tab, create an effect.
#tabs #nav ul li:hover { background-color: #ffffff; }
Style the pages
#tabs #data { margin-top: 3px; } #tabs #data div { display: no+ne; border: 1px solid #000066; padding: 10px; } #tabs #data div.active { display: block; }
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.
Current state
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.
<script type="text/javascript" language="javascript"> function navAllInactive() { oNav = document.getElementById('nav'); oLis = oNav.getElementsByTagName('LI'); for (i = 0; i < oLis.length; i++) { oLis[i].className = ''; } } function hideAllData() { oData = document.getElementById('data'); oDivs = oData.getElementsByTagName('DIV'); for (i = 0; i < oDivs.length; i++) { oDivs[i].style.display = 'none'; } } function doShow(o, pageId) { hideAllData(); navAllInactive(); o.className = 'active'; oData = document.getElementById(pageId); oData.style.display = 'block'; return false; } </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)
That's it
<!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"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Tabs</title> #tabs { padding: 10px; } #tabs #nav { margin-bottom: 0px; margin-left: 8px; } #tabs ul { list-style: none; margin: 0px; padding: 0px; } #tabs #nav ul li { display: inline; padding: 3px 7px; cursor: default; background-color: #eeeeee; border: 1px solid #cccccc; border-bottom: none; margin-right: 2px; } #tabs #nav ul li.active { border: 1px solid #000066; border-bottom: 1px solid white; background-color: #ffffff; } #tabs #nav ul li:hover { background-color: #ffffff; } #tabs #data { margin-top: 3px; } #tabs #data div { display: no+ne; border: 1px solid #000066; padding: 10px; } #tabs #data div.active { display: block; } </style> <script type="text/javascript" language="javascript"> function navAllInactive() { oNav = document.getElementById('nav'); oLis = oNav.getElementsByTagName('LI'); for (i = 0; i < oLis.length; i++) { oLis[i].className = ''; } } function hideAllData() { oData = document.getElementById('data'); oDivs = oData.getElementsByTagName('DIV'); for (i = 0; i < oDivs.length; i++) { oDivs[i].style.display = 'none'; } } function doShow(o, pageId) { hideAllData(); navAllInactive(); o.className = 'active'; oData = document.getElementById(pageId); oData.style.display = 'block'; return false; } </script> </head> <div id="tabs"> <div id="nav"> <li onclick="javascript:doShow(this, 'one');" class="active">Tab one</li> <li onclick="javascript:doShow(this, 'two');">Tab two</li> <li onclick="javascript:doShow(this, 'three');">Tab three</li> </ul> </div> <div id="data"> <div id="one" class="active"> Page one </div> <div id="two"> Page two </div> <div id="three"> Page three </div> </div> </div> </body> </html>
Tips
- Edit pages and only later add the display: no+ne attribute to #tabs #data div.
- Store your page content to chunks for better readability.
The code would look like this:
!!! 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!
Categories: HOWTO | Misc



