dHTML Drop Down Menu Tutorial - Part 6b
Building the Menu Bar - Adding menu labels
Quick Tip: Early revisions of my menu code used multiple document.write statements to write the html one line at a time. This works fine in most browsers except Opera 5.x (and to a lesser extent Opera 6.x) where document.write seems to be very slow. In this system we'll build fewer, longer strings. This greatly increases the speed in the Opera browsers.
This method builds a string of HTML for a menu label and stores it it in the labelText array. Due to differences in Netscape 4, the code is quite long. Netscape 4 uses a layer nested in an ilayer (mouseover and mouseout events are attached to the layer - NS4 doesn't offer onclick events for layers). The other browsers use just a div element (the events are attached to the parent td element).
The Code
this.addLabel = function(bullet, labelText, menuNo,
labelWidth, offColour, onColour, labelURL, align) {
this.numLabels += 1;
tier[menuNo] = 0;
if (this.o_Bor != null) borderMod[menuNo] = 1;
else borderMod[menuNo] = 0;
if (menuNo != null) {
onColours[menuNo] = onColour;
offColours[menuNo] = offColour;
onClass[menuNo] = this.onClass;
offClass[menuNo] = this.offClass;
labelBulletName[menuNo] = bullet;
}
temp = new String('');
this.rowText[this.numLabels] = new String('');
if (this.orientation == 'vertical')
this.rowText[this.numLabels] += '<tr id="labelRow'+ menuNo + '">';
temp += '<td id="labelCell' + menuNo + '" width="'+ labelWidth + '" bgcolor="' +
offColour + '" valign="middle" height="' + this.height + '" ';
if (!ns4) {
temp += ' onmouseout="menuOut(); "onmouseover="menuOver(); ';
if (this.orientation == 'vertical')
temp += 'return !showMenuSide('+ menuNo+', event, tier['+menuNo+']);" ';
else
temp += 'return !showMenu(' + menuNo + ', event);" ';
if (this.targetType=='self')
temp += ' onclick="document.location.href=\'' + labelURL + '\';" ';
if (this.targetType=='new')
temp += ' onclick="openMe(\'' + labelURL + '\'); return false;" ';
if (this.targetType=='frame')
temp += ' onclick="parent.' + this.targetFrame +
'.document.location.href=\'' + labelURL + '\';" ';
if (this.targetType=='iframe')
temp += ' onclick="' + this.targetFrame + '.location.href=\'' +
labelURL + '\';" ';
}
temp +='>';
if (ns4) {
temp +='<ilayer><layer onmouseout="menuOut();" onmouseover="menuOver(); ';
if (this.orientation == 'vertical')
temp +='return !showMenuSide('+menuNo+', event, tier['+menuNo+']);" ';
else
temp +='return !showMenu(' + menuNo + ', event);" ';
} else {
temp +='<div ';
}
temp += ' class="myMenuLabel' + align + '" width="' + labelWidth +
'" id="menuLabel' + menuNo +'"><a href="' + labelURL +'"
target="' + this.targetFrame + '" class="' + this.offClass + '"
id="menuLink' + menuNo +'">';
if (bullet != null)
temp += '<img src="' + eval(bullet + ".URL") + '" border="0"
align="' + this.bulletAlign + '"
id="menuBullet' + menuNo + '" name="menuBullet' + menuNo + '">';
temp += labelText + '</a>';
if (ns4) temp += '</layer></ilayer>';
else temp += '</div>';
temp += '</td>';
this.labelText[this.numLabels] = new String(temp);
}
Usage
The following Code adds three menu labels to the menu bar:
myTest.addLabel('menuItemBullet', 'test1', 1, 150, '#cccccc',
'#ffffff', 'test1.asp', 'left');
myTest.addLabel('labelBullet', 'test2', 2, 150, '#cccccc',
'#ffffff', 'test2.asp', 'center');
myTest.addLabel('subMenuBullet', 'test3', 3, 150, '#cccccc',
'#ffffff', 'test3.asp', 'right');
|