无限级别的菜单(侧拉菜单)
我的导航菜单_我们
body{
font-size:12px
}
//filter:shadow(color=#aaaaaa,direction=135)
var itemsNum=0;
var displayFlags = 'block';
function menu(menuId,menuWidth,menuHeight,menuTop,menuLeft,itemBranchNum,parentMenuId){
this.itemBranchs = new Array();
this.width=menuWidth;
this.height=menuHeight;
this.top=menuTop;
this.left=menuLeft;
this.itemBranchNum=itemBranchNum;
this.parentMenuId=parentMenuId;
this.displayFlags = displayFlags;
//this.parenItemId=parenItemId;
this.menuId=menuId;
this.add = addItemBranch;
this.writeMenu = writeMenu;
this.setTop=setMenuTop;
this.setLeft=setMenuLeft;
this.setWidth=setMenuWidth;
this.setHeight=setMenuHeight;
itemsNum=0;
}
function setMenuTop(top){
this.top=top;
}
function setMenuLeft(left){
this.left=left
}
function setMenuWidth(width){
this.width=width
}
function setMenuHeight(height){
this.height=height
}
function addItemBranch(itemBranch){
this.itemBranchs[itemsNum] = itemBranch
var cuttrutItemHeigt=(this.height)/this.itemBranchNum;
var cuttrutItemWidth=this.width;
var cuttrutItemTop=cuttrutItemHeigt*itemsNum-1;
var cuttrutItemLeft=-1;
itemBranch.setTop(cuttrutItemTop);
itemBranch.setLeft(cuttrutItemLeft);
itemBranch.setWidth(cuttrutItemWidth);
itemBranch.setHeight(cuttrutItemHeigt);
itemsNum++;
//alert(itemBranch.top+"--"+itemBranch.left+"--"+itemBranch.width+"--"+itemBranch.height)
//alert(this.itemBranchs.length)
}
function writeMenu(){
var menuWidth=this.width;
var menuHeight=this.height;
var menuTop=this.top;
var menuLeft=this.left;
var menuId=this.menuId;
var parentMenuId = this.parentMenuId;
var menuString = '';
menuString +='
'
var nums = this.itemBranchs.length;
for (var i=0;i'
document.write(menuString);
}
function itemBranch(itemId,subMenuId,name,link,parentObj){
this.subMenu = new Object();
this.name=name;
this.link=link;
this.parentObj=parentObj;
this.itemId=itemId;
this.subMenuId=subMenuId;
this.width=0;
this.height=0;
this.top=0;
this.left=0;
this.setTop=setItemTop;
this.setLeft=setItemLeft;
this.setWidth=setItemWidth;
this.setHeight=setItemHeight;
this.writeItemBranch = writeItemBranch;
this.add = addSubMenu;
}
function writeItemBranch(){
var itemStr='';
var link=this.link;
if(link==''){
itemStr+='
';
itemStr+=''+this.name+''
itemStr+='8'
itemStr+='
';
}else{
itemStr+='
';
itemStr+=''+this.name+''
itemStr+='
';
}
return itemStr;
}
function showSubMenu(obj){
var objSubMenu = document.getElementById(obj).style;
objSubMenu.display='block'
}
function hiddenSubMenu(obj){
document.getElementById(obj).style.display='none';
}
function keepSubMenu(obj,parentId){
obj.style.display = 'block';
if(parentId!=''){
document.getElementById(parentId).style.display='block';
}else{
//
}
}
function removeSubMenu(obj,parentId){
if(parentId==''){
//
}else{
if(obj.id!="mainMain"){
obj.style.display = 'none';
}
if(parentId!="mainMain"){
document.getElementById(parentId).style.display='none';
}
}
}
function addSubMenu(subMenuObj){
this.subMenu = subMenuObj
var top=this.parentObj.top+this.top;
var width=subMenuObj.width;
var left=this.parentObj.left+this.width;
var height=subMenuObj.height;
subMenuObj.displayFlags = 'none';
subMenuObj.setTop(top);
subMenuObj.setLeft(left);
subMenuObj.setWidth(width);
subMenuObj.setHeight(height);
subMenuObj.writeMenu();
//subMenuObj.style.diplay='none'
//alert(left);
//alert(subMenuObj.width)
}
function setItemTop(top){
this.top=top;
}
function setItemLeft(left){
this.left=left;
}
function setItemWidth(width){
this.width=width;
}
function setItemHeight(height){
this.height=height;
}
/*
建立菜单的规则,菜单的内容要一组一组的写,要有顺序。
例如:
先建立一个主菜单,menu是一个对象,在建立对象的时候,必须生成一个实例;
菜单实例里面有几个参数,分别是:菜单的id、菜单的宽度(menuWidth)、菜单的高度(menuHeight)、
菜单的纵坐标(menuTop)、菜单的横坐标(menuLeft)和含有子菜单的个数(这个的主要作用是将菜单的高度按子项平分,
最后把这个平分之后的值作为子项的高度)
注意:如果是主菜单,那么这四项就必须全部填写。如果是子菜单,那么纵坐标和横坐标可以填写任意值
var mainMenu=new menu('',100,100,50,100,5);
菜单里面的子项也是一个对象,所以在添加之前也必须先生成实例。
子项的参数有itemId(子项的id),subMenuId(添加子菜单的id),name(子项显示的名字),link(子项的连接),parentObj(子项的父菜单的对象)
*/
var mainMain= new menu('mainMain',100,100,50,50,4,'');
var itemBranch_1 = new itemBranch('itemBranch_1','subMenu_1','考试类','',mainMain);//********* itemBranch_1
var itemBranch_2 = new itemBranch('itemBranch_2','subMenu_2','娱乐类','',mainMain);//********** itemBranch_2
mainMain.add(itemBranch_1);
mainMain.add(itemBranch_2);
mainMain.add(new itemBranch('','','知识技能类','http://www.baidu.com',null));
mainMain.add(new itemBranch('','','交互类','http://www.baidu.com',null));
mainMain.writeMenu();
//二级菜单
var subMenu_1 = new menu('subMenu_1',100,100,50,50,4,'mainMain');
var itemBranch_3 = new itemBranch('itemBranch_3','subMenu_1_1','知识技能类','',subMenu_1)//************ itemBranch_3
subMenu_1.add(itemBranch_3);
subMenu_1.add(new itemBranch('','','交互类','http://www.baidu.com',null));
subMenu_1.add(new itemBranch('','','知识技能类','http://www.jb51.net',null));
subMenu_1.add(new itemBranch('','','交互类','http://www.baidu.com',null));
itemBranch_1.add(subMenu_1);
var subMenu_2 = new menu('subMenu_2',100,100,50,50,5,'mainMain');
subMenu_2.add(new itemBranch('','','知识技能类','http://www.baidu.com',null));
subMenu_2.add(new itemBranch('','','交互类','http://www.baidu.com',null));
subMenu_2.add(new itemBranch('','','知识技能类','http://www.baidu.com',null));
subMenu_2.add(new itemBranch('','','交互类','http://www.jb51.net',null));
subMenu_2.add(new itemBranch('','','交互类','http://www.baidu.com',null));
itemBranch_2.add(subMenu_2);
//三级菜单
var subMenu_1_1 = new menu('subMenu_1_1',100,100,50,50,4,'subMenu_1');
subMenu_1_1.add(new itemBranch('','','知识技能类','http://www.baidu.com',null));
subMenu_1_1.add(new itemBranch('','','交互类','http://www.jb51.net',null));
subMenu_1_1.add(new itemBranch('','','知识技能类','http://www.baidu.com',null));
subMenu_1_1.add(new itemBranch('','','交互类','http://www.baidu.com',null));
itemBranch_3.add(subMenu_1_1);
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]