這個選單最大的優點,是可以浮在任何網頁元素之上,甚至跨過框架(frameset)——一般的選單往往會被 select、object、Flash 等元素擋住。如果你有這方面的需求,那麼這個方案會是很理想的選擇。
整體而言,它具有以下特性:
(1)選單的樣式與資料都透過 XML 設定。
(2)樣式可以隨心所欲修改,例如:
<base> <background-color>#DDDDDD</background-color> <background-image>url(menu_bg.jpg)</background-image> <font-size>10pt</font-size> <color>#000099</color> <width>100%</width> <height>32</height> <text-align>center</text-align> <cursor>default</cursor> </base>
只要節點名稱符合各自對應標籤(table、tr、td 之一)的 style attribute 即可。
(3)主選單項之間可以設定分隔圖片,子選單項則可設定分隔線樣式。
(4)所有圖片都由 CSS 控制,避免路徑問題。
(5)可設定虛擬目錄別名,為選單連結使用絕對路徑帶來便利,例如:
<context> <path>hdsa</path> </context>
設定該值後,使用者只需依據檔案的目錄結構寫好選單,就不必擔心虛擬目錄的問題。
由於選單使用了 window.createPopup(),因此被限制在 IE5.5 以上版本,而且子選單若是新開視窗,會被攔截。
兩個腳本可以寫在同一個檔案中。
// ------------------ 下面一些函數用來處理彈出視窗 ------------------ //
var pops = new Array(); // 用來儲存 Popup 視窗家族的陣列
function CreatePopup(degree)
{
if (degree < 0) return null; // 層數不能小於 0
if (pops[degree] != null) return pops[degree]; // 如果已經存在則不需建立
if (degree == 0)
pops[0] = window.createPopup(); // 建立最頂層 Popup 視窗
else {
if (pops[degree - 1] == null)
pops[degree - 1] = CreatePopup(degree - 1); // 遞迴回溯從第一層開始建立
pops[degree] = pops[degree - 1].document.parentWindow.createPopup();
}
pops[degree].document.body.setAttribute("degree", degree);
return pops[degree];
}
CreatePopup(1); // 建立一個 2 層的 Popup 家族
var oPopup = pops[0];
var timer = null;
// 顯示一級子選單
function showSubMenu1(obJShow, strGetID) {
clearTimer();
endHideSubMenu();
var objGet = eval(document.getElementById(strGetID));
pops[1].hide();
pops[0].document.body.innerHTML = objGet.innerHtml;
pops[0].show(0, 0, 1, 1, obJShow);
var intWidth = pops[0].document.body.scrollWidth;
var intHeight = pops[0].document.body.scrollHeight;
pops[0].hide();
pops[0].show(10, objShow.offsetHeight - 2, intWidth, intHeight, obJShow);
}
// 顯示二級選單
function showSubMenu2(strGetID, objTr) {
clearTimer();
endHideSubMenu();
var objGet = eval(document.getElementById(strGetID));
pops[1].document.body.innerHTML = objGet.innerHtml;
pops[1].show(0, 0, 1, 1, pops[0].document.body);
var intWidth = pops[1].document.body.scrollWidth;
var intHeight = pops[1].document.body.scrollHeight;
pops[1].hide();
pops[1].show(objTr.offsetWidth - 2, -2, intWidth, intHeight, objTr);
}
// 隱藏一級子選單
function hideSubMenu1(){
window.setTimeout("pops[0].hide()", 1000);
}
// 隱藏二級子選單
function hideSubMenu2() {
pops[1].hide();
}
// 隱藏所有下拉選單
function hideSubMenu() {
if (pops[0] != null) pops[0].hide();
if (pops[1] != null) pops[1].hide();
}
function clearTimer() {
if (timer != null)
window.clearTimeout(timer);
timer = null;
}
// 位置資訊
var m_strTitle = "您的位置->";
// -------- 主選單樣式相關定義 ---------//
var m_strMainMenuStyle = "";
var m_strMainMenuItemStyle = "";
var m_strMMItemProBase = {};
var m_strMMItemProMOver = {};
var m_strMMItemSplitImg;
// -------- 子選單樣式相關定義 ---------//
var m_strSubMenuStyle = "";
var m_strSubMenuItemStyle = "";
var m_strSMItemProBase = {};
var m_strSMItemProMOver = {};
var m_strSMItemBorderB = "";
// 定義虛擬目錄路徑
var m_strContextPath = "";
// 選單 XML 資料檔物件
var m_doc;
// 主選單集合
var m_objMMList;
var STR_POS_FRONT = "您的位置:";
// 載入選單
function loadMenu(strXMLFile) {
m_doc = new ActiveXObject("MSXML2.DOMDocument.3.0");
m_doc.async = false;
m_doc.load(strXMLFile);
getMainMenuStyle();
getSubMenuStyle();
// 得到主選單集合
m_objMMList = m_doc.selectSingleNode("//popmenu/menus").childNodes;
// 得到虛擬目錄
if (m_doc.selectSingleNode("//popmenu/context/path")) {
m_strContextPath = m_doc.selectSingleNode("//popmenu/context/path").text;
}
printMainMenu(); // 輸出主選單
printSubMenu(); // 輸出一級子選單
}
// 鼠標移入移出主選單樣式的改變
function chgMainMenuStyle(obj, bolChg) {
if (bolChg) { // 鼠標懸停改變顏色
for (var pro in m_strMMItemProMOver) obj.style[pro] = m_strMMItemProMOver[pro];
} else { // 鼠標移出
obj.style.border = "0";
for (var pro in m_strMMItemProBase) obj.style[pro] = m_strMMItemProBase[pro];
}
}
// 鼠標移入移出子選單樣式的改變
function chgSubMenuStyle(obj, bolChg) {
if (bolChg) {
for (var pro in m_strSMItemProMOver) obj.style[pro] = m_strSMItemProMOver[pro];
} else {
for (var pro in m_strSMItemProBase) obj.style[pro] = m_strSMItemProBase[pro];
}
}
// 構造節點樣式
function getNodeStyle(strPath) {
var strStyle = "";
if (m_doc.selectSingleNode(strPath)) {
var nodeList = m_doc.selectSingleNode(strPath).childNodes;
for (var i = 0; i < nodeList.length; i++) {
strStyle += nodeList[i].nodeName.toLowerCase() + ":" + nodeList[i].text + ";";
}
}
return strStyle;
}
// 構造節點 Style Property(由 attribute 轉為 property,如 background-color -> backgroundColor)
function getNodeProperty(strPath) {
var arrProperty = {};
if (m_doc.selectSingleNode(strPath)) {
var nodeList = m_doc.selectSingleNode(strPath).childNodes;
for (var i = 0; i < nodeList.length; i++) {
var nodeName = nodeList[i].nodeName.toLowerCase();
var nodeText = nodeList[i].text;
var aName = nodeName.split("-");
var strProperty = aName[0];
for (var j = 1; j < aName.length; j++) {
strProperty += aName[j].substring(0, 1).toUpperCase() + aName[j].substr(1);
}
arrProperty[strProperty] = nodeText;
}
}
return arrProperty;
}
// 打開連結,所有選單連結都透過這個函數開啟,並顯示位置資訊
function onClickEvent(strUrl, strTarget, strVIEwText) {
m_bolFirst = true;
m_strTitle = "";
var arrText = strVIEwText.split("->");
if (arrText.length > 0) m_strTitle = arrText[0];
for (var i = 1; i < arrText.length; i++) m_strTitle += "-" + arrText[i];
m_strTitle = m_strTitle.substring(STR_POS_FRONT.length);
hideSubMenu(); // 隱藏下拉選單
if (strUrl == "#") return;
// 如果 url 是一個 Javascript 字串則直接執行
if (strUrl.toLowerCase().indexOf("Javascript") != -1) {
executeScript(strUrl);
return;
}
strUrl = m_strContextPath + strUrl; // 加上虛擬目錄
window.open(strUrl, strTarget);
}
// 執行一個 Javascript 字串
function executeScript(strScript) {
eval(strScript);
}
以上程式碼展示了以 window.createPopup() 建立多層彈出視窗、並以 XML 描述選單結構與樣式的完整思路。由於篇幅關係,樣式讀取(getMainMenuStyle、getSubMenuStyle)與主/子選單輸出(printMainMenu、printSubMenu)等函數可依相同邏輯補齊。