| Note:
Layout code slightly modified by DD for easier implementation
Updated: 08/30/01 Fixed IE 6 onMouseover menu bug. Only
code of Step 3 affected!
Description: One of the best
of it's kind, this is two-level menu bar script. Use it to intelligently organize
your site's links while adding a touch of DHTML coolness at the same time. Wonderfully
useful and cool a creation this is.
The script has been updated to support a
variety of subtle, but interesting new features, most of them dealing with menu display.
You can now specify a width for each individual menu category and sub category, invert the
later so they are displayed above, instead of below, the bar (useful if your bar is at the
bottom of the page), and align it left, center, or right. Finally, you can now have
multiple menu bars on one page! The core code has also be streamlined for greater
efficiency, plus a few bugs have been squashed. |
<script language="JavaScript"
src="navcond.js"></script>
<script language="JavaScript">
/*
Top Navigational Bar II (By Mike Hall)
Last updated: 00/05/08
Permission granted and modified by Dynamicdrive.com to include script in archive
For this and 100's more DHTML scripts, visit http://dynamicdrive.com
*/
var myNavBar1 = new NavBar(0);
var dhtmlMenu;
//define menu items (first parameter of NavBarMenu specifies main category width, second
specifies sub category width in pixels)
//add more menus simply by adding more "blocks" of same code below
dhtmlMenu = new NavBarMenu(100, 0);
dhtmlMenu.addItem(new NavBarMenuItem("Home",
"http://dynamicdrive.com"));
myNavBar1.addMenu(dhtmlMenu);
dhtmlMenu = new NavBarMenu(100, 120);
dhtmlMenu.addItem(new NavBarMenuItem("News", ""));
dhtmlMenu.addItem(new NavBarMenuItem("CNN", "http://www.cnn.com"));
dhtmlMenu.addItem(new NavBarMenuItem("MSNBC", "http://msnbc.com"));
dhtmlMenu.addItem(new NavBarMenuItem("ABCNews",
"http://www.abcnews.com"));
myNavBar1.addMenu(dhtmlMenu);
dhtmlMenu = new NavBarMenu(110, 120);
dhtmlMenu.addItem(new NavBarMenuItem("Technology", ""));
dhtmlMenu.addItem(new NavBarMenuItem("News.com",
"http://www.news.com"));
dhtmlMenu.addItem(new NavBarMenuItem("Techweb",
"http://www.techweb.com"));
dhtmlMenu.addItem(new NavBarMenuItem("Wired",
"http://www.wired.com"));
dhtmlMenu.addItem(new NavBarMenuItem("Dynamic Drive",
"http://www.dynamicdrive.com"));
myNavBar1.addMenu(dhtmlMenu);
dhtmlMenu = new NavBarMenu(100, 150);
dhtmlMenu.addItem(new NavBarMenuItem("Search", ""));
dhtmlMenu.addItem(new NavBarMenuItem("Yahoo",
"http://www.yahoo.com"));
dhtmlMenu.addItem(new NavBarMenuItem("Alta Vista",
"http://www.altavista.com"));
dhtmlMenu.addItem(new NavBarMenuItem("Infoseek",
"http://www.infoseek.com"));
dhtmlMenu.addItem(new NavBarMenuItem("Excite",
"http://www.excite.com"));
dhtmlMenu.addItem(new NavBarMenuItem("HotBot",
"http://www.hotbot.com"));
myNavBar1.addMenu(dhtmlMenu);
dhtmlMenu = new NavBarMenu(100, 150);
dhtmlMenu.addItem(new NavBarMenuItem("Webmaster", ""));
dhtmlMenu.addItem(new NavBarMenuItem("Dynamic Drive",
"http://dynamicdrive.com"));
dhtmlMenu.addItem(new NavBarMenuItem("Website Abstraction",
"http://wsabstract.com"));
dhtmlMenu.addItem(new NavBarMenuItem("Freewarejava.com",
"http://freewarejava.com"));
dhtmlMenu.addItem(new NavBarMenuItem("BrainJar",
"http://brainjar.com"));
myNavBar1.addMenu(dhtmlMenu);
//set menu colors
myNavBar1.setColors("#000000", "#000000", "#C0C0C0",
"#ffffff", "#666666", "#000000", "#cccccc",
"#ffffff", "#000080")
//uncomment below line to center the menu (valid values are "left",
"center", and "right"
//myNavBar1.setAlign("center")
var fullWidth;
function init() {
// Get width of window, need to account for scrollbar width in Netscape.
fullWidth = getWindowWidth()
- (isMinNS4 && getWindowHeight() < getPageHeight() ? 16 : 0);
myNavBar1.resize(fullWidth);
myNavBar1.create();
myNavBar1.setzIndex(2);
//UNCOMMENT BELOW LINE TO MOVE MENU DOWN 50 pixels
//myNavBar1.moveTo(0, 50);
}
</script>
Unzip the above file, and locate "navcond.js".
Upload this .js file to your webpage directory, or the script won't run!
To configure the menu/sub menu items and their
colors, simply edit the code of Step 1 above (see documentation inside). It it extremely
straightforward.
Note: The above menu setup uses the external JavaScript file
"navcond.js", which is a condensed version of the original
"dhtmllib.js" and "navbar.js" files. It is condensed in that the
contained code is stripped of ALL comments and linebreaks, resulting in a much smaller
file download (25k vs 40k with the later two files). If your main concern is with
legibility and ease-of-modification of the menu script (ie: for the sake of others in your
company who may have to work with the menu), you should use the two other .js files
included inside the zip file instead: "dhtmllib.js" and "navbar.js".
Simply upload the two files in place of "navcond.js", and change the
reference inside Code 1 to point to them instead. Note, however, in return for extensive
documentation and legibility, you sacrifice download speed.
Menu documentation
While the below is not required reading, it is
definitely recommended, and is helpful in getting to know how the menu works, and to
harness some of its more advanced features.
1) Main menu methods
The following are the menu's core methods,
which with the above code setup, you call using (myNavBar1.the_method()):
| setSizes(border,
padding, separator) |
Defines the width of
the border around headers and menus, the amount of spacing surrounding the item text and
the width of the border between each item in a drop down. |
| setColors(bdColor,
hdrFgColor, hdrBgColor, hdrHiFgColor, hdrHiBgColor, itmFgColor, itmBgColor, itmHiFgColor,
itmHiBgColor) |
Sets the various
colors used for the bar. The first value sets the border color. The next two set the text
and background colors for a header and the two after that set the colors used when a
header is highlighted. The last four values do the same for the menu items. |
| setFonts(hdrFamily,
hdrStyle, hdrWeight, hdrSize, itmFamily, itmStyle, itmWeight, itmSize) |
Defines the font used
for the headers and the menu items. Standard CSS values may be used. |
| hide() |
Hides the navigation
bar from view. |
| show() |
Make the navigation
bar visible. |
| moveTo(x,
y) |
Moves the bar to the
specified coordinates. By default navigation bars will be positioned at (0, 0). |
| moveBy(dx,
dy) |
Moves the bar by the
given values. |
| setzIndex(z)
|
Sets the zIndex value
of the bar and. drop down menus. This allows you to set the stacking order when a bar
overlaps another bar or positioned element. |
| getWidth() |
Returns the current
width of the bar. This is useful if you initially define a bar with a width of zero and
want to find what the resulting width is after it has been created. |
| getMinWidth()
|
Returns the minimum
width allowed for the bar. This is based on the total width required for the headers. |
| setAlign(alignment) |
Given a value of
"left", "center" or "right" this will change the position of
the headers relative to the bar. If a numeric value is given instead, the headers will be
moved to the position that many pixels from the left edge of the bar. The headers will be
repositioned dynamically if the bar has already been created. |
| resize(width) |
Resizes the bar to the
given width. If already created the headers will be repositioned according the the bar's
current alignment setting. Note that if you specify a width less than the value given by getMinWidth()
the bar will be resized using the minimum required width instead. |
| invert() |
Toggles the placement
of the menus. By default they drop down below the bar but they can also be made to appear
above the |
2) Menu links
targetting
When an item or header is clicked on, the associated link
string is checked. If it's a null string, nothing is done. If the string starts with
"javascript:" the code that follows is executed. For anything else, the string
is taken to be a URL and the window's location is set to that value, causing the
link to be followed.
To setup a menu link so that the url loads in another
frame, use the following javascript: url:
javascript:parent.framename.location='http://dynamicdrive.com'
where "framename" is the name of the frame you
wish to target.
3) Displaying the menu across two
frames?
Frames are treated as individual windows by the browser.
The bar and drop down menus cannot cross over the boundaries between
frames, any more than an image on a page can be made to stick outside of the browser
window. Likewise, you cannot have a drop down appear in a frame other than the one
containing its parent bar. |