|
|
One of the most common DHTML requests I get is for a Windows Explorer-style hierarchical menu, where there's a list of topics or "folders" that a user can click on to reveal subtopics, or "files," within that folder. It's a common desktop metaphor that seems ever more necessary on the Web, especially as we see navigation bars incorporating larger and more complex content while still trying to fit on the screen. Hierarchical menus are a solution to the common problem of having too many links in too small a space.
But, I figured, there are several Web sites where you can find large, complex scripts to create large, complex hierarchical menus. Instead, I'll show you how to make your own menus, of any length or complexity, with the use of an extremely useful and widely unknown DOM CSS property,
When you set an element to
Unfortunately, not every browser supports the Houdini with an indexWith a little clever design, we can build ourselves a standard, left-hand navigation bar that we can make dynamic with only a few lines of JavaScript. The secret is understanding how layers and tables act like containers for content, and how they automatically resize themselves to fit the content they contain. If you've spent much time designing Web sites, you've noticed how important the content in a table is to the size of the table; the "transparent, one-pixel GIF" hack developed as Web designers realized that some browsers would ignore specified widths and heights of tables if the content of the table didn't fill the entire space.
We can now use this property of content containers to our advantage, nesting tables and layers that will automatically resize to create our hierarchical menu. In a nutshell, we'll create a layer that lists a couple of topic areas, "Projects" and "Interests," from my Web site. The layer also contains tables, which contain sub-listings of each area, that are initially set to
If we change the display value of a table with JavaScript once the page has loaded, the containing layer will resize to incorporate the table, and the links will appear as if from nowhere. Voila! That's the basis of our menus, and many other DHTML tricks! This simple manipulation of one DOM CSS object, in this case Let's take a look now at the menu we'll be creating, so you can get a better picture of how this fits together:
As you can see, first we have a layer with two links, "Projects" and "Interests." When "Projects" is clicked, the menu expands and displays a list of sublinks below "Projects." In HTML, the layout is quite simple, but by adding
We can extend this menu another level just as easily, and show chapters below the "Designing with JavaScript" link, by nesting another table:
And we get this:
|
|
|
|
|
||||||||