代码简介:
【荐】JavaScript制作可伸缩的淡绿色菜单导航,运行一下,您就知道!
代码内容:
<!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制作可伸缩的淡绿色菜单导航_网页代码站(www.webdm.cn)</title>
<script type="text/javascript">
//在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]);
};
</script>
<style>
body {
font-size:12px;
}
p {
margin:0;
padding:0
}
.jsmenu {
width:160px;
background:url(http://www.webdm.cn/images/20091230/pm_leftbg.gif) repeat-y;
border-bottom:#C4DE9C solid 1px;
}
.jsmenu div {
background:url(http://www.webdm.cn/images/20091230/jm_tab.gif) no-repeat;
overflow:hidden;
}
.jsmenu div.collapsed {
height:28px;
}
.jsmenu div .fath {
height:11px;
overflow:hidden;
display:block;
padding:8px 0 8px 26px;
color:#2754bb;
font-size:14px;
cursor:default;
background:url(http://www.webdm.cn/images/20091230/jm_exp.gif) no-repeat 10px 10px;
border-top:#C4DE9C solid 1px;
cursor:pointer;
}
.jsmenu div .single {
height:27px;
overflow:hidden;
display:block;
padding:0;
font-size:14px;
background:url(http://www.webdm.cn/images/20091230/jm_sgl.gif) no-repeat 10px 10px;
border-top:#C4DE9C solid 1px;
}
.jsmenu div .single a {
padding:8px 0 8px 26px;
display:block;
text-decoration:none;
font-size:14px;
color:#2754bb;
cursor:pointer;
}
.jsmenu div .single a.current {
background:url(http://www.webdm.cn/images/20091230/jm_tabon.gif) no-repeat left top;
}
.jsmenu div.collapsed .fath {
background:url(http://www.webdm.cn/images/20091230/jm_col.gif) no-repeat 10px 10px;
}
.jsmenu div .son {
border-top:#C4DE9C solid 1px;
padding:8px 0;
}
.jsmenu a {
padding:7px 0 6px 34px;
display:block;
color:#565656;
text-decoration:none;
}
.jsmenu a:hover {
color:#F60;
text-decoration:none;
}
.jsmenu a.current {
background:url(http://www.webdm.cn/images/20091230/jm_li.gif) no-repeat;
}
</style>
</head>
<body onload = "initMenu()">
<div class="jsmenu" id="js_menu">
<div>
<span class="fath">网页代码站</span>
<p class="son">
<a class="current" href="#">业界新闻</a>
<a href="#">技术动态</a>
<a href="#">八卦评论</a>
</p>
</div>
<div class="collapsed">
<span class="fath">交互设计</span>
<p class="son">
<a href="#">交互设计思想</a>
<a href="#">用户行为研究</a>
<a href="#">设计教程</a>
</p>
</div>
<div class="collapsed">
<span class="fath">视觉设计</span>
<p class="son">
<a href="#">配色构图</a>
<a href="#">技术教程</a>
</p>
</div>
<div class="collapsed">
<span class="fath">JavaScript</span>
<p class="son">
<a href="#">JavaScript基础</a>
<a href="#">JavaScript框架</a>
<a href="#">JavaScript源码范例</a>
</p>
</div>
<div><span class="single"><a href="#">酷站</a></span></div>
<div><span class="single"><a href="#">专题</a></span></div>
</div>
</body>
</html>
<br>
<p><a href="http://www.webdm.cn">网页代码站</a> - 最专业的代码下载网站 - 致力为中国站长提供有质量的代码!</p>
代码来自:http://www.webdm.cn/webcode/ef9e7423-3141-4a78-aaf2-ba8d81b047e0.html