兩級導航菜單在網頁中非常實用,實現的方法也有很多,本文為大家介紹下使用jquery是如何實現的
主要用於運維系統, 對界面要求不高的場合。 深深感到自己頁面設計能力弱爆了,只能借鑒一下了, 交互邏輯還可以勝任一點。 直接貼代碼: 1. HTML 頁面及 JS 交互, 注意引入 Jquery 文件 代碼如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://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> <!-- moonmm css --> <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 = 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'); $('#content').css('width', width); $('#maincontent').css('height', height + 'px' ); $('#maincontent').css('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'); console.log($(this).css('background-color')); $('#nav').css('background-color', $(this).css('background-color')); $('#nav').css('width', $('#topnav').width()); switch(this.id) { case 'topmenu_home': $('.nav_list').hide(); $('#homebo').show(); $('#homebo a').first().click(); break; case 'topmenu_itlearn': $('.nav_list').hide(); $('#itlearnbo').show(); $('#itlearnbo a').first().click(); break; case 'topmenu_baike': $('.nav_list').hide(); $('#baikebo').show(); $('#baikebo a').first().click(); break; case 'topmenu_scisrc': $('.nav_list').hide(); $('#scisrcbo').show(); $('#scisrcbo a').first().click(); break; case 'topmenu_more': $('.nav_list').hide(); $('#morebo').show(); $('#morebo a').first().click(); break; default : break; } } ); $('#nav a').click( function() { setContentSize(contentHeight, docWidth-15); $('#nav .select').removeClass('select'); $(this).addClass('select'); switch(this.id) { case 'myblogModule': $('#maincontent').attr('src', 'http://blog.csdn.net/lovesqcc'); break; case 'ifeveModule': setContentSize(contentHeight+80, docWidth-15); $('#maincontent').attr('src', 'http://ifeve.com/'); break; case 'csdnModule': $('#maincontent').attr('src', 'http://csdn.net'); break; case 'infoqModule': $('#maincontent').attr('src', 'http://www.infoq.com/cn/'); break; case 'boleModule': $('#maincontent').attr('src', 'http://blog.jobbole.com/'); break; case 'itcommentModule': $('#maincontent').attr('src', 'http://www.vaikan.com/'); break; case 'wikiModule': $('#maincontent').attr('src', 'http://zh.wikipedia.org/zh-tw/Wikipedia'); break; case 'zhihuModule': $('#maincontent').attr('src', 'http://www.zhihu.com/'); break; case 'acmModule': $('#maincontent').attr('src', 'http://www.acm.org/'); break; case 'xiamiModule': $('#maincontent').attr('src', 'http://www.xiami.com'); break; case 'opencourseModule': $('#maincontent').attr('src', 'http://v.163.com/special/ted10collection/'); break; default: break; } } ); $('.nav_list').hide(); $('#topmenu_home').click(); showtime(); } ); </script> </head> <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="http://aone.alibaba-inc.com/aone2/req/addFromProductline/9139" 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>&l