Design a Tabbed Interface
Designing a tabbed interface is straightforward procedure after you know how the interface works. In this article, you'll see how to create a set of tabs across the top of the page and swap out the content as the user clicks on each tab. The procedure is generic; you can use the same procedure to align tabs vertically, or place them anywhere on the page. You'll also see how to maintain the UI statethat is, highlight the active tab so users always know which tab is active. The code shown in this article works in Internet Explorer 4+ and in Netscape 6. Here's the tabbed interface procedure:
Create a set of <div> tags to hold the tabs across the top of the page. Create separate <div> tags to hold panels corresponding to each tab. The panels hold the contentthe text, graphics and controls you want to appear when the user clicks on a tab.
Write the code to activate the tabs. When a user clicks on a tab, you want to display the appropriate content for that tab. You may also want to highlight tabs as the user moves the cursor over them.
Write code to deactivate the current tab and hide the associated content when a user selects a different tab.
Open your favorite text editor and add the following to create the tabs and panels:
Each tab and panel's id attribute value has an appended number, for example, tab1, and panel1he appended number matches the tabs to the panels. Using this simple numbering mechanism, you can pass the number to any JavaScript routine and then retrieve a reference to the appropriate tab or panel. The class attribute for each <div> specifies one of two style sheet rules, named tab and panel, which implement the following style sheet rules.
These simple style sheet rules include positioning, font, color and border properties. The border property enhances the tabbed metaphor by providing rectangles around the tabs and panels. Setting the overflow property to auto forces the browser to draw scrollbars if the tab width exceeds the page width..
Do you like tabbed interfaces? Did you think you had to create complicated script or use controls to implement tabbed interfaces? Can you think of ways to improve the article code? Join the discussions at Web.dhtml.general and Web.dhtml.scripting to get answers, make comments, or help others with their problems.