javascript封装的下拉导航菜单渐显效果
作者:littlephenix 来源:蓝色理想 发布时间:2007-08-04 20:11:00
标签:下拉菜单,导航,javascript
内容摘要:本文介绍了使用js来实现下拉伸缩导航菜单的功能,并带有渐显的效果,值得收藏。
正好这几天公司不忙,学校又没有事情,所以想抽空架一个个人主页,设计的时候想在主页做一个酷酷的导航菜单,于是就上网找灵感。有一个网站的导航栏给我的印象不错,于是就把网页保存下来想研究一下它的js代码,没想到的是竟然是用.NET的自定义控件生成的!上面的代码差点没把我看晕过去!(有兴趣的话可以试一试哦,里面N多变量的~~~汗),还好大三时学过c#(垃圾)加上它的控件可以试用,就下下来用了,感觉还真的不错,简单易用,可是~测试的时候差点没昏了!NND试用版的竟然只能再本机测试,别人的PC访问不但显示不了网页还警告说要注册购买!!!!大哥的竟然还要$(本少爷每月实习补助才1千¥啊),一怒之下决定自己封装一个。参考了树型菜单的js源码,花了3天时间,终于第一版写好了^_^
由于imags文件夹传不上来,所以菜单背景的切换效果显示不了^_^大家定制自己喜欢的样式^_^
下面是主要的js代码,方便大家研究,呵呵:
<script language="javascript">
function PhenItem(id,pid,label,url,type,img,over,img2,over2,title,target){
this.id=id;
this.pid=pid;
this.label=label;
this.url=url;
this.title=title;
this.target=target;
this.img=img;
this.over=over;
this.img2=img2;
this.over2=over2;
this.type=type;
//this._ih = false; //is it the head item?
this._hc = false; //has the child item?
this._ls = false; //has sibling item?
this._io = false; //whether the panelbar is open?
};
//menu object
function PhenMenu(objName) {
this.config = {
closeSameLevel : true
};
//alert("asdsdf");
this.obj = objName;
this.items = [];
this.root = new PhenItem(-1);
};
//add a new item to the item array
PhenMenu.prototype.add = function(id,pid,label,url,type,img,over,img2,over2,title,target){
this.items[this.items.length] = new PhenItem(id,pid,label,url,type,img,over,img2,over2,title,target);
};
// Outputs the menu to the page
PhenMenu.prototype.toString = function() {
//alert("arrived here");
var str = '<div>\n';
if (document.getElementById) {
str += this.addItem(this.root);
} else str += 'Browser not supported.';
str += '\n</div>';
//alert(str);
//document.write(str);
//alert(this.items[0]._hc);
return str;
};
// Creates the menu structure
PhenMenu.prototype.addItem = function(pItem) {
var str = '';
//var n=0;
for (var n=0; n<this.items.length; n++) {
if(this.items[n].pid == pItem.id){
var ci = this.items[n];
//alert(ci.pid);
//alert(ci.id);
this.setHS(ci);
//alert("item:"+ci._hc);
//alert(ci._ls);
str += this.itemCreate(ci, n);
if(ci._ls) break;
}
}
return str;
};
// Creates the node icon, url and text
PhenMenu.prototype.itemCreate = function(pItem, itemId) {
//alert(pItem.type.toLowerCase());
var str = '';
if(pItem.type == 'header')
str = '<table width="100%" class="header" valign="middle" onmouseover="this.className=\'headerSelected\'" onmouseout="this.className=\'header\'" onclick="'+this.obj+'.o('+itemId+')"><tr><td>';
else
str = '<table width="100%" class="item" valign="middle" onmouseover="this.className=\'itemOver\'" onmouseout="this.className=\'item\'" onclick="'+this.obj+'.o('+itemId+')"><tr><td>';
if (pItem.img) {
str += ' <img id="i' + this.obj + itemId + '" src="' + pItem.img + '" alt="" />';
}
if (pItem.url) {
str += '<a id="s' + this.obj + itemId + '" class="navigation_item" href="' + pItem.url + '"';
if (pItem.title) str += ' title="' + pItem.title + '"';
if (pItem.target) str += ' target="' + pItem.target + '"';
str += ' onmouseover="window.status=\'' + pItem.label + '\';return true;" onmouseout="window.status=\'\';return true;"';
str += '>';
}
str += ' ' + pItem.label;
if (pItem.url) str += '</a>';
str += '</td></tr></table>';
//alert(pItem.url);
//alert(str);
if (pItem._hc) {
str += '<table id="ct' + this.obj + itemId + '" width="100%" style="display:' + ((pItem._io) ? 'block' : 'none') + '; FILTER: blendTrans(Duration=3.0); VISIBILITY: hidden"><tr><td>';
str += this.addItem(pItem);
str += '</td></tr></table>';
//alert(str);
//document.write(str);
}
return str;
};
// Checks whether a item has child and if it is the last sibling
PhenMenu.prototype.setHS = function(pItem) {
var lastId;
for (var n=0; n<this.items.length; n++) {
if (this.items[n].pid == pItem.id) pItem._hc = true;
if (this.items[n].pid == pItem.pid) lastId = this.items[n].id;
}
if (lastId==pItem.id) pItem._ls = true;
};
// Toggle Open or close
PhenMenu.prototype.o = function(id) {
//alert(this.items.length);
var ci = this.items[id];
//alert(ci);
//this.setHS(ci);
//alert(this.items[id]._hc);
this.itemStatus(!ci._io, id);
ci._io = !ci._io;
if (this.config.closeSameLevel) this.closeLevel(ci);
};
// Change the status of a item(open or closed)
PhenMenu.prototype.itemStatus = function(status, id) {
cTable = document.getElementById('ct' + this.obj + id);
if(status){
cTable.filters.item(0).Apply();
cTable.style.display = 'block';
cTable.style.visibility = "";
cTable.filters.item(0).Play();
}
else
cTable.style.display = 'none';
//cDiv.style.display = (status) ? 'block': 'none';
};
// Closes all items on the same level as certain item
PhenMenu.prototype.closeLevel = function(pItem) {
//alert(this.items[0]._hc);
for (var n=0; n<this.items.length; n++) {
//alert(this.items[n]._hc);
if ((this.items[n].pid == pItem.pid) && (this.items[n].id != pItem.id) && this.items[n]._hc) {
this.itemStatus(false, n);
this.items[n]._io = false;
this.closeAllChildren(this.items[n]);
}
}
};
PhenMenu.prototype.closeAllChildren = function(pItem) {
for (var n=0; n<this.items.length; n++) {
if (this.items[n].pid == pItem.id && this.items[n]._hc) {
if (this.items[n]._io) this.itemStatus(false, n);
this.items[n]._io = false;
this.closeAllChildren(this.items[n]);
}
}
};
</script>
PS:里面的script代码和style样式可以写在单独的js和css文件里。


猜你喜欢
- 一、前言我们一般在做接口关联时,会通过保存中间变量实现接口关联,在关联时就需要用到变量提取,那今天我们就介绍接口自动化中变量提取的两大神器:
- 有时候有很多逗号,这样我们就不好处理了,下面的函数就是将多个逗号替换为一个逗号,方便后面的处理。<script language=&q
- jemalloc源于Jason Evans 2006年在BSDcan conference发表的论文:《A Scalable Concurr
- 本文主要介绍如何通过python生成ppt文件,以及借助ppt模板来生成ppt环境python 3python-pptx安装pip3 ins
- 本文为大家分享了python搭建服务器实现两个Android客户端间收发消息,供大家参考,具体内容如下python服务器# coding:u
- ……最近在学习yolo1、yolo2和yolo3,事实上它们和SSD网络有一定的相似性,我准备汇
- 警告信息:浏览器控制台警告:Write operation failed: computed value is readonly使用环境:V
- 1. 用 Numpy ndarray 作为数据传入 plyimport numpy as npimport matplotlib as mp
- python列表变量可以存储一个元素,而列表是一个大容器,可以存储N多个元素,程序可以方便的对这些数据进行整体操作(可以存储多个不同的数据类
- PHP mysqli_select_db() 函数更改连接的默认数据库:删除数据库<?php // 假定数据库用户名:root,密码:
- keras 深度学习框架中get_value函数运行越来越慢,内存消耗越来越大问题问题描述如上图所示,经过时间和内存消耗跟踪测试,发现是ke
- 1:strip()方法去除字符串开头或者结尾的空格>>> a = " a b c ">>&
- 1.Access数据库的DSN-less连接方法: set adocon=Server.Createobject(&q
- 访问phpmyadmin时总是出现 “无法载入 mysql 扩展,请检查 PHP 配置”。
- 原因大致是主键必须是唯一的,也就是数据库里可能存在和现还原数据库有重复的地方,a restriction that Foreign Keys
- 四种基本的函数类型局部变量 就是在函数内部定义的变量【作用域仅局限于函数内部】不同的函数 可以定义相同的局部变量,但是各自用各自的 不会产生
- 第一种: php部分 <?php if($_FILES['file']['error']&
- 〝 古人学问遗无力,少壮功夫老始成 〞python之tkinter库弹窗messagebox,常码字不易,出精品更难,没有特别幸运,那么请先
- SQL Server内存会不断增加当 SQL Server 数据库引擎在 Microsoft? Windows NT? 或 Windows?
- YOLOv5的Backbone设计在上一篇文章《YOLOV5的anchor设定》中我们讨论了anchor的产生原理和检测过程,对YOLOv5