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文件里。
0
投稿
猜你喜欢
- 在后台处理数据时,前台页面同时计数显示进度条Proess Bar使用了layer来显示用法:1。引用<script lang
- 一个不错的文字放大特性源码。效果图:运行代码框<script for=document event=onmousemove>//
- 这个仿msn的右下角popup提示窗口效果很久以前收集的,现在整理出来给大家分享,需要的朋友可以拿去用,特点,提示窗口内容和js代码分离容易
- 这里列出了javascript 中的document.execCommand() 的各种参数说明:2D-Position 允许通过
- 元素的CSS样式,除了包括内联的(即通过style属性加上的)样式定义外,还有页面嵌入的css和外部引入的css两种方式。但在JS中通过el
- 随着互联网的飞速发展,很多主页都是越做越漂亮、越来越有个性。如果你是一个资深的网虫不会没有自己的主页吧!下面小弟就来谈谈如何使你的网上“小窝
- 如果程序中没有设置session的过期时间,那么session过期时间就会按照IIS设置的过期时间来执行,IIS中session默认过期时间
- 编号标准宗地编码(landCode)所在区段编码(sectCode)1131001BG001G0012131001BG002G0013131
- 大致功能:$() 取得所有元素$("div") 取得所有DIV$("#a1") 取得ID为a1的元素
- win7 +Navicat Lite 9+ VMware7在VMware中安装openSUSE11.x mysql5 Navicat Lit
- 孟宪会 由于XML(可扩展标记语言:eXtensible Markup Language)真正的平台无关性,它正在逐渐成为数据传
- 一些简单的代码简化下面是一个简单示例,它说明了 jQuery 对代码的影响。要执行一些真正简单和常见的任务,比方说为页面的某一区域中的每个链
- 定义列表和其他类型的列表稍有不同,它由两部分组成:名称和定义。DT 指定名称,为内联元素。DD 指定定义,为块级元素。标准属性id, cla
- 见下面的代码:<html><head><title>精彩春风之月份查询</title><
- 影响用户访问的最大部分是前端的页面。网站的划分一般为二:前端和后台。我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为
- 如何显示数据库中的图片和超级链接?代码见下:<% set conn=server.creatobject(&quo
- <%'***********************************************'函数
- 对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描述见7.3.6 日期和时间类型。 1、这里是一个使用日期函数的例子。
- CSS2.1 中规定了关于 CSS 规则 Specificity(特异性)的计算方式,用一个四位的数
- FrontPage服务器扩展可以做以下工作:(1),保存和检索Web服务器的文件;(2),远程执行所有文件管理操作;(3),改名或移动文件时