滑动式折叠菜单 - Slashdot’s Menu

折叠菜单让你在尽可能小的地方放置尽可能多的内容,同时加大了操作的简便性,因此,深受前台设计师的喜爱。随着大家对动画效果的钟爱,折叠菜单也开始“动”起来了,本文介绍的就是 DimX 制作的滑动式折叠菜单效果-Slashdot’s Menu。

滑动式折叠菜单 - Slashdot’s Menu – 示例

 
<script type="text/javascript">
function SDMenu(id) {
	if (!document.getElementById || !document.getElementsByTagName)
		return false;
	this.menu = document.getElementById(id);
	this.submenus = this.menu.getElementsByTagName("div");
	this.remember = true;
	this.speed = 3;
	this.markCurrent = true;
	this.oneSmOnly = false;
}
SDMenu.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);
		};
	if (this.markCurrent) {
		var links = this.menu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++)
			if (links[i].href == document.location.href) {
				links[i].className = "current";
				break;
			}
	}
	if (this.remember) {
		var regex = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "=([01]+)");
		var match = regex.exec(document.cookie);
		if (match) {
			var states = match[1].split("");
			for (var i = 0; i < states.length; i++)
				this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
		}
	}
};
SDMenu.prototype.toggleMenu = function(submenu) {
	if (submenu.className == "collapsed")
		this.expandMenu(submenu);
	else
		this.collapseMenu(submenu);
};
SDMenu.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(this.speed * 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 = "";
			mainInstance.memorize();
		}
	}, 30);
	this.collapseOthers(submenu);
};
SDMenu.prototype.collapseMenu = function(submenu) {
	var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var moveBy = Math.round(this.speed * 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";
			mainInstance.memorize();
		}
	}, 30);
};
SDMenu.prototype.collapseOthers = function(submenu) {
	if (this.oneSmOnly) {
		for (var i = 0; i < this.submenus.length; i++)
			if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
				this.collapseMenu(this.submenus[i]);
	}
};
SDMenu.prototype.expandAll = function() {
	var oldOneSmOnly = this.oneSmOnly;
	this.oneSmOnly = false;
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className == "collapsed")
			this.expandMenu(this.submenus[i]);
	this.oneSmOnly = oldOneSmOnly;
};
SDMenu.prototype.collapseAll = function() {
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className != "collapsed")
			this.collapseMenu(this.submenus[i]);
};
SDMenu.prototype.memorize = function() {
	if (this.remember) {
		var states = new Array();
		for (var i = 0; i < this.submenus.length; i++)
			states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
		var d = new Date();
		d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
		document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
	}
};
</script>

调用方式

 
var myMenu = new SDMenu("main_menu"); // 菜单ID
 
// 默认参数
myMenu.speed = 3;                     // 折叠速度
myMenu.remember = true;               // 是否记录状态
myMenu.oneSmOnly = false;             // 一次只有一个菜单打开
myMenu.markCurrent = true;            // 是否高亮当前菜单
 
myMenu.init();
 
// 附加方法
var firstSubmenu = myMenu.submenus[0];
myMenu.expandMenu(firstSubmenu);      // 打开一个菜单
myMenu.collapseMenu(firstSubmenu);    // 关闭一个菜单
myMenu.toggleMenu(firstSubmenu);      // 当菜单关闭时打开,当菜单打开时关闭
 
myMenu.expandAll();                   // 打开所有菜单
myMenu.collapseAll();                 // 关闭所有菜单

用 Javascript 制作可折叠式导航菜单

当栏目级别较多时,很多人都会考虑使用可折叠式导航菜单,既方便点击又节省空间,本文介绍的是对可折叠式导航菜单的简单实现,希望对新手有帮助。

用 Javascript 制作可折叠式导航菜单 – 示例

 
<script type="text/javascript">
// 说明:用 Javascript 制作可折叠式导航菜单
// 整理:http://www.CodeBit.cn

// curr_id : 当前id ; total_num 总共有几个菜单
function switchMenu(curr_id, total_num)
{
	for(var i=0; i<total_num; i++)
	{
		//二级菜单
		var el = document.getElementById('menu_sub_'+i);

		if(!el) return;

		//一级菜单前面的图片
		var img = document.getElementById('menu_master_'+i);

		if(i == curr_id)
		{
			el.style.display = "block";
			img.src = "img/open.gif";
		}
		else
		{
			el.style.display = "none";
			img.src = "img/close.gif";
		}
	}
}
</script>
 
<style type="text/css">
<!--
#warp {
	width:200px;
	margin:100px auto;
}
.menu_master {
	border:1px solid #ccc;
	padding:5px 10px;
	margin-bottom:2px;
	cursor:pointer;
	font-weight:bold;
	color:#666;
}
.menu_master img { vertical-align:middle; }
.sub_menu { display:none; }
.sub_menu ul, .sub_menu ul li {
	margin:0;
	padding:0;
	list-style:none;
}
.sub_menu ul li a {
	margin:0 0 3px 10px;
	padding:3px 0 2px 25px;
	background:url('img/fw.gif') no-repeat 5px center;
	display:block;
	height:18px;
	border-bottom:1px solid #eee;
	color:#666;
	text-decoration:none;
}
-->
</style>


<div id="warp">

	<div class="menu_master" onclick="javascript:switchMenu(0, 4);">
		<img src="close.gif" alt="close" id="menu_master_0" /> PHP/MySQL
	</div>
	<div id="menu_sub_0" class="sub_menu">
		<ul>
			<li><a href="http://www.codebit.cn/pub/html/php_mysql/tutorial/" title="PHP/MySQL教程">PHP/MySQL教程</a></li>
			<li><a href="http://www.codebit.cn/pub/html/php_mysql/tip/" title="PHP/MySQL小技巧">PHP/MySQL小技巧</a></li>
			<li><a href="http://www.codebit.cn/pub/html/php_mysql/problem/" title="PHP/MySQL常见问题">PHP/MySQL常见问题</a></li>
		</ul>
	</div>

	<div class="menu_master" onclick="javascript:switchMenu(1, 4);">
		<img src="close.gif" alt="close" id="menu_master_1" /> (X)HTML/CSS
	</div>
	<div id="menu_sub_1" class="sub_menu">
		<ul>
			<li><a href="http://www.codebit.cn/pub/html/xhtml_css/tutorial/" title="(X)HTML/CSS教程">(X)HTML/CSS教程</a></li>
			<li><a href="http://www.codebit.cn/pub/html/xhtml_css/tip/" title="(X)HTML/CSS小技巧">(X)HTML/CSS小技巧</a></li>
		</ul>
	</div>

	<div class="menu_master" onclick="javascript:switchMenu(2, 4);">
		<img src="close.gif" alt="close" id="menu_master_2" /> Ajax
	</div>
	<div id="menu_sub_2" class="sub_menu">
		<ul>
			<li><a href="http://www.codebit.cn/pub/html/ajax/tutorial/" title="Ajax教程">Ajax教程</a></li>
			<li><a href="http://www.codebit.cn/pub/html/ajax/tip/" title="Ajax小技巧">Ajax小技巧</a></li>
		</ul>
	</div>

	<div class="menu_master" onclick="javascript:switchMenu(3, 4);">
		<img src="close.gif" alt="close" id="menu_master_3" /> Javascript
	</div>
	<div id="menu_sub_3" class="sub_menu">
		<ul>
			<li><a href="http://www.codebit.cn/pub/html/javascript/tutorial/" title="Javascript教程">Javascript教程</a></li>
			<li><a href="http://www.codebit.cn/pub/html/javascript/tip/" title="Javascript小技巧">Javascript小技巧</a></li>
		</ul>
	</div>

</div>