本文实例讲述了JS基于构造函数实现的菜单滑动显隐效果。分享给大家供大家参考,具体如下:

运行效果截图如下:

JS基于构造函数实现的菜单滑动显隐效果【测试可用】

具体代码如下:

<!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" />
<meta http-equiv="Content-Type" content="; charset=utf-8" />
<title>JS树型菜单</title>
<script language="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 = 1;
 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();
 }
 }, 10);
 //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();
 }
 }, 10);
};
/*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.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" "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
 }
};
var myMenu;
window.onload = function() {
 myMenu = new SDMenu("my_menu");
 myMenu.init();
};
</script>
<style type="text/css">
 html,body{
 height:100%;
 margin:0;
 font-size:12px;
 }
 span{
 background:#F0DFBE;
 border:1px solid #ffffff;
 border-left:6px solid #F2A31B;
 width:228px;
 height:23px;
 display:block;
 line-height:23px;
 padding-left:20px;
 }
 a{
 padding:3px 0 3px 40px;
 display:block;
 color:#636363;
 }
 #my_menu{
 width:255px;
 background:#F7F2E4;
 height:100%;
 }
 div.sdmenu div.collapsed {
 height: 25px;
}
 div.sdmenu div{
 overflow: hidden;
 }
</style>
</head>
<body>
 <div style="float:left" id="my_menu" class="sdmenu">
 <div>
  <span>菜单一</span>
  <a href="#"&nbsp;&nbsp;菜单一子内容</a>
  <a href="#"&nbsp;&nbsp;菜单一子内容</a>
  <a href="#"&nbsp;&nbsp;菜单一子内容</a>
  <a href="#"&nbsp;&nbsp;菜单一子内容</a>
  <a href="#"&nbsp;&nbsp;菜单一子内容</a>
 </div>
 <div>
  <span>菜单二</span>
  <a href="#"&nbsp;&nbsp;菜单二子内容</a>
  <a href="#"&nbsp;&nbsp;菜单二子内容</a>
  <a href="#"&nbsp;&nbsp;菜单二子内容</a>
 </div>
 <div>
  <span>菜单三</span>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
 </div>
 <div>
  <span>菜单三</span>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
 </div>
 <div>
  <span>菜单三</span>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
 </div>
 <div>
  <span>菜单x</span>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
  <a href="#"&nbsp;&nbsp;菜单三子内容</a>
 </div>
 </div>
</body>
</html>

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》

希望本文所述对大家JavaScript程序设计有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。