網頁制作poluoluo文章簡介:前段時間項目裡用到的,項目已經發布,今天整理一下發出來。 不用框架,純JavaScript+CSS制作的二級伸縮菜單,簡單易懂,具有很強的定制性和重復開發性。 程序說明: 頁面調用方法: 以下為引用的內容: !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
前段時間項目裡用到的,項目已經發布,今天整理一下發出來。
不用框架,純JavaScript+CSS制作的二級伸縮菜單,簡單易懂,具有很強的定制性和重復開發性。
程序說明:
頁面調用方法:
以下為引用的內容:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JavaScript可伸縮菜單 - Oncoding編碼營原創</title>
<!--引入JS文件-->
<script type="text/javascript" src="jsmenu.js"></script>
.....
<!--加載程序-->
<body onload = "initMenu()">
<!--此處須定義ID-->
<div class="jsmenu" id="pm_menu">
<!--展開的一級分類-->
<div>
<span class="fath">業界資訊</span>
<p class="son">
<!--class="current"當前所在二級分類-->
<a class="current" href="#">業界新聞</a>
<a href="#">技術動態</a>
<a href="#">八卦評論</a>
</p>
</div>
<!--閉合的一級分類-->
<div class="collapsed">
....
</div>
<!--獨立的一級分類-->
<div><span class="single"><a href="#">酷站</a></span></div>
....
程序說明:
以下為引用的內容:
//在ID為“js_menu”的div內開始程序
function initMenu(){
var pm_menu = new JSMenu("js_menu");
pm_menu.init();
}
//定義主函數
function JSMenu(id) {
if (!document.getElementById || !document.getElementsByTagName)
return false;
this.menu = document.getElementById(id);
this.submenus = this.menu.getElementsByTagName("div");
}
//引入函數,取得所有span
JSMenu.prototype.init = function() {
var mainInstance = this;
for (var i = 0; i < this.submenus.length; i++)
this.submenus[i].getElementsByTagName("span")[0].onclick = function() {
mainInstance.toggleMenu(this.parentNode);
};
this.expandOne();
};
//展開含"current"的菜單
JSMenu.prototype.expandOne = function() {
for (var i = 0; i < this.submenus.length; i++) {
var links = this.submenus[i].getElementsByTagName("a");
for (var j = 0; j < links.length; j++){
if (links[j].className == "current")
this.expandMenu(this.submenus[i]);
}
}
};
//變換菜單狀態函數
JSMenu.prototype.toggleMenu = function(submenu) {
if (submenu.className == "collapsed")
this.expandMenu(submenu);
else
this.collapseMenu(submenu);
};
//展開所有菜單函數
JSMenu.prototype.expandMenu = function(submenu) {
var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
var links = submenu.getElementsByTagName("a");
for (var i = 0; i < links.length; i++)
fullHeight += links[i].offsetHeight;
var moveBy = Math.round(5 * links.length);
var mainInstance = this;
var intId = setInterval(function() {
var curHeight = submenu.offsetHeight;
var newHeight = curHeight + moveBy;
if (newHeight < fullHeight)
submenu.style.height = newHeight + "px";
else {
clearInterval(intId);
submenu.style.height = "";
submenu.className = "";
}
}, 30);
this.collapseOthers(submenu);
};
//折疊菜單函數
JSMenu.prototype.collapseMenu = function(submenu) {
var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
var moveBy = Math.round(5 * submenu.getElementsByTagName("a").length);
var mainInstance = this;
var intId = setInterval(function() {
var curHeight = submenu.offsetHeight;
var newHeight = curHeight - moveBy;
if (newHeight > minHeight)
submenu.style.height = newHeight + "px";
else {
clearInterval(intId);
submenu.style.height = "";
submenu.className = "collapsed";
}
}, 30);
};
//折疊其他菜單函數
JSMenu.prototype.collapseOthers = function(submenu) {
for (var i = 0; i < this.submenus.length; i++)
if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
this.collapseMenu(this.submenus[i]);
};