兩級導航菜單是後台管理系統、運維平台常見的 UI 元件。本文將完整展示如何使用 jQuery 打造一個具備即時時鐘、自適應佈局、分頁iframe載入功能的兩級導航菜單。適合介面設計需求不高、但重視交互邏輯的專案快速落地。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "https://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>兩級導航菜單的示例</title>
<script src="jquery-1.10.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="two-nav.css" />
<script type="text/javascript">
var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
// 即時時鐘
function showtime() {
var date = new Date();
var yy = date.getYear();
if (yy < 1900) yy += 1900;
var MM = date.getMonth() + 1; if (MM < 10) MM = '0' + MM;
var dd = date.getDate(); if (dd < 10) dd = '0' + dd;
var hh = date.getHours(); if (hh < 10) hh = '0' + hh;
var mm = date.getMinutes(); if (mm < 10) mm = '0' + mm;
var ss = date.getSeconds(); if (ss < 10) ss = '0' + ss;
var ww = weeks[date.getDay()];
$('#currTime span').html('[ ' + yy + '-' + MM + '-' + dd + ' ' + hh + ':' + mm + ':' + ss + ' ' + ww + ' ]');
window.setTimeout(showtime, 1000);
}
// 自適應尺寸計算
var setContentSize = function(height, width) {
$('header').css('width', width);
$('#topnav').css('width', width);
$('#nav').css('width', width);
$('#content').css({height: height + 'px', width: width});
$('#maincontent').css({height: height + 'px', width: width});
};
$(document).ready(function() {
var docHeight = $(document).outerHeight();
var docWidth = $(document).width();
var headerHeight = $('#header').height();
var contentHeight = docHeight - headerHeight;
// 頂層菜單點擊切換
$('#topnav a').click(function() {
$('.select').removeClass('select');
$(this).addClass('select');
$('#nav').css('background-color', $(this).css('background-color'));
$('#nav').css('width', $('#topnav').width());
$('.nav_list').hide();
switch(this.id) {
case 'topmenu_home': $('#homebo').show(); $('#homebo a').first().click(); break;
case 'topmenu_itlearn': $('#itlearnbo').show(); $('#itlearnbo a').first().click(); break;
case 'topmenu_baike': $('#baikebo').show(); $('#baikebo a').first().click(); break;
case 'topmenu_scisrc': $('#scisrcbo').show(); $('#scisrcbo a').first().click(); break;
case 'topmenu_more': $('#morebo').show(); $('#morebo a').first().click(); break;
}
});
// 子菜單點擊載入 iframe
$('#nav a').click(function() {
setContentSize(contentHeight, docWidth - 15);
$('#nav .select').removeClass('select');
$(this).addClass('select');
var urls = {
'myblogModule': 'https://blog.csdn.net/lovesqcc',
'ifeveModule': 'https://ifeve.com/',
'csdnModule': 'https://csdn.net',
'infoqModule': 'https://www.infoq.com/cn/',
'boleModule': 'https://blog.jobbole.com/',
'itcommentModule': 'https://www.vaikan.com/',
'wikiModule': 'https://zh.wikipedia.org/zh-tw/Wikipedia',
'zhihuModule': 'https://www.zhihu.com/',
'acmModule': 'https://www.acm.org/',
'xiamiModule': 'https://www.xiami.com',
'opencourseModule': 'https://v.163.com/special/ted10collection/'
};
if (urls[this.id]) {
if (this.id === 'ifeveModule') setContentSize(contentHeight + 80, docWidth - 15);
$('#maincontent').attr('src', urls[this.id]);
}
});
// 初始化
$('.nav_list').hide();
$('#topmenu_home').click();
showtime();
});
</script>
<body>
<div id="header">
<div id="firstHeader">
<div id="logo">兩級導航菜單</div>
<div id="target">兩級導航菜單的示例</div>
<div id="toolbar">
<a href="#" id="userinfo">[ 你好: <span style="color:#f47920">admin</span> ]</a>
<a href="#" id="currTime"><span></span></a>
<a href="..." target="_blank"><span style="color:#f47920">[ 提建議 ]</span></a>
</div>
</div>
<div class="clear"></div>
<div id="topnav">
<div class="topnav_list">
<a href="#" class="select" id="topmenu_home"><span>首頁</span></a>
<a href="#" id="topmenu_itlearn"><span>IT學習</span></a>
<a href="#" id="topmenu_baike"><span>百科</span></a>
<a href="#" id="topmenu_scisrc"><span>學術資源</span></a>
<a href="#" id="topmenu_more"><span>更多</span></a>
</div>
</div>
<div class="clear"></div>
<div id="nav">
<div class="nav_list" id="homebo">
<a href="#" class="select" id="myblogModule"><span>我的博客</span></a>
</div>
<div class="nav_list" id="itlearnbo">
<a href="#" class="select" id="ifeveModule"><span>並發編程網</span></a>
<a href="#" id="csdnModule"><span>CSDN</span></a>
<a href="#" id="infoqModule"><span>Infoq</span></a>
<a href="#" id="boleModule"><span>伯樂在線</span></a>
<a href="#" id="itcommentModule"><span>外刊評論</span></a>
</div>
<div class="nav_list" id="baikebo">
<a href="#" class="select" id="wikiModule"><span>WIKI百科</span></a>
<a href="#" id="zhihuModule"><span>知乎</span></a>
</div>
<!-- 其他分類略 -->
</div>
<div id="content">
<iframe id="maincontent" src="" frameborder="0"></iframe>
</div>
</div>
</body>
點擊頂層菜單時,將該按鈕的背景色同步套用至側邊欄 #nav,並同步寬度,視覺上形成整體聯動效果。
透過 setContentSize 計算文檔可視高度扣除 header 高度,再動態設定 #content 及 #maincontent 的高寬,確保內容區填滿剩餘空間。
$(document).ready 確保 DOM 就緒後再綁定事件$('#topmenu_home').click() 初始化預設頁面.nav_list,再顯示對應分類,最後自動觸發第一個子項的 click 事件本範例展示了傳統 jQuery 開發模式下,如何以較少代碼實現功能完整的兩級導航系統。雖然現代前端框架(Vue/React)已成為主流,但在維護老專案或快速搭建內部工具時,這類輕量級方案依然具備實用價值。