找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1378|回复: 0

[分享] 织梦(dedecms)导航条dropdown.js的改进(附演示demo)

[复制链接]
发表于 2018-3-10 07:56:52 | 显示全部楼层 |阅读模式 来自 中国–河南–新乡
可以设置一个一直都显示的二级菜单,修复了没有二级菜单时鼠标移上去仍然显示上一个二级菜单的问题.支持一级菜单鼠标离开事件
html代码

代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>我的网站</title>
  6. <meta name="description" content="" />
  7. <meta name="keywords" content="" />
  8. <link type="text/css" rel="stylesheet" href="http://www.dedecode.com/img/fnews/css/style.css" />
  9. </head>
  10. <body>
  11. <div class="w1000">
  12. <div class="w960">
  13. <!--f1-->
  14. <!-- 菜单 -->
  15. <div id="menu" class="menus">
  16. <ul>
  17. <!-- class='navselect'根据此属性来判断需要默认显示的二级菜单,以便在织梦模板中通用 -->
  18. <li class='navselect'><a href="/" rel='dropmenu1'>主 页</a></li>
  19. <li><a href='/a/jinrongzixun/'>金融资讯</a></li>
  20. <li><a href='/a/gerenjinrong/' rel='dropmenu2'>个人金融</a></li>
  21. <li><a href='/a/qiyejinrong/' rel='dropmenu4'>企业金融</a></li>
  22. <li><a href='/a/caijingpindao/' rel='dropmenu5'>财经频道</a></li>
  23. <li><a href='/a/zhifujiesuan/' rel='dropmenu6'>支付结算</a></li>
  24. <li><a href='/a/yewushenqingtongdao/' rel='dropmenu7'>业务申请</a></li>
  25. <li><a href='/a/zhongyaogonggao/' >重要公告</a></li>
  26. <li><a href='/a/xinyongka/' >信用卡</a></li>
  27. </ul>
  28. </div>
  29. <div class="menus_bottom">
  30. </div>
  31. <!-- //二级子类下拉菜单 -->
  32. <script type="text/javascript" src="dropdown.js"></script>
  33. <ul id="dropmenu1" class="dropMenu">
  34. <li><a href="/a/gerenjinrong/touzilicai/">首页列表</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  35. </ul>
  36. <ul id="dropmenu2" class="dropMenu">
  37. <li><a href="/a/gerenjinrong/touzilicai/">投资理财</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  38. <li><a href="/a/gerenjinrong/grdk/">个人贷款</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  39. <li><a href="/a/gerenjinrong/gerenxiaofei/">个人消费贷款</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  40. <li><a href="/a/gerenjinrong/xinyong/">信用贷款</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  41. <li><a href="/a/gerenjinrong/qichedaikuan/">汽车贷款</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  42. <li><a href="/a/gerenjinrong/yishoulou/">一手楼按揭</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  43. <li><a href="/a/gerenjinrong/ershoulou/">二手楼按揭</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  44. <li><a href="/a/gerenjinrong/grjy/">个人经营贷款</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  45. </ul>
  46. <ul id="dropmenu4" class="dropMenu">
  47. <li><a href="/a/qiyejinrong/gongsirongzi/">公司融资</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  48. <li><a href="/a/qiyejinrong/maoyirongzi/">贸易融资</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  49. <li><a href="/a/qiyejinrong/gongsilicai/">公司理财</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  50. <li><a href="/a/qiyejinrong/piaojuyewu/">票据业务</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  51. <li><a href="/a/qiyejinrong/xiangmurongzi/">项目融资</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  52. </ul>
  53. <ul id="dropmenu5" class="dropMenu">
  54. <li><a href="/a/caijingpindao/huangjin/">黄金</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  55. <li><a href="/a/caijingpindao/zhaiquan/">债券</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  56. <li><a href="/a/caijingpindao/waihui/">外汇</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  57. <li><a href="/a/caijingpindao/jijin/">基金</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  58. </ul>
  59. <ul id="dropmenu6" class="dropMenu">
  60. <li><a href="/a/zhifujiesuan/pos/">P0S支付</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  61. </ul>
  62. <ul id="dropmenu7" class="dropMenu">
  63. <li><a href="/a/yewushenqingtongdao/gerenyewu/">个人业务咨询</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  64. <li><a href="/a/yewushenqingtongdao/gongsiyewu/">公司业务咨询</a>&nbsp;&nbsp;|&nbsp;&nbsp;</li>
  65. </ul>
  66. <ul id="dropmenu8" class="dropMenu">
  67. </ul>
  68. <ul id="dropmenu3" class="dropMenu">
  69. </ul>
  70. <script type="text/javascript"> cssdropdown.startchrome("menu")</script>
  71. </div>
  72. </div>
  73. <!--1000 end -->
  74. </body>
  75. </html>

  76. dropdown.js
复制代码


代码如下:

  1. var cssdropdown = {
  2. disappeardelay: 250,
  3. disablemenuclick: false,
  4. enableswipe: 1,
  5. enableiframeshim: 1,
  6. dropmenuobj: null,
  7. ie: document.all,
  8. firefox: document.getElementById && !document.all,
  9. swipetimer: undefined,
  10. bottomclip: 0,
  11. getposOffset: function(what, offsettype) {
  12. var totaloffset = (offsettype == "left") ? what.offsetLeft: what.offsetTop;
  13. var parentEl = what.offsetParent;
  14. while (parentEl != null) {
  15. totaloffset = (offsettype == "left") ? totaloffset + parentEl.offsetLeft: totaloffset + parentEl.offsetTop;
  16. parentEl = parentEl.offsetParent;
  17. }
  18. return totaloffset;
  19. },
  20. swipeeffect: function() {
  21. if (this.bottomclip < parseInt(this.dropmenuobj.offsetHeight)) {
  22. this.bottomclip += 10 + (this.bottomclip / 10);
  23. this.dropmenuobj.style.clip = "rect(0 auto " + this.bottomclip + "px 0)";
  24. } else return;
  25. this.swipetimer = setTimeout("cssdropdown.swipeeffect()", 10);
  26. },
  27. //隐藏或者显示二级菜单
  28. showhide: function(obj, e) {
  29. if (this.ie || this.firefox) this.dropmenuobj.style.left = this.dropmenuobj.style.top = "-500px";
  30. if (e.type == "click" && obj.visibility == hidden || e.type == "mouseover") {
  31. if (this.enableswipe == 1) {
  32. if (typeof this.swipetimer != "undefined") clearTimeout(this.swipetimer);
  33. obj.clip = "rect(0 auto 0 0)";
  34. this.bottomclip = 0;
  35. this.swipeeffect();
  36. }
  37. obj.visibility = "visible";
  38. } else if (e.type == "click") obj.visibility = "hidden";
  39. },
  40. iecompattest: function() {
  41. return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement: document.body;
  42. },
  43. clearbrowseredge: function(obj, whichedge) {
  44. var edgeoffset = 0;
  45. if (whichedge == "rightedge") {
  46. var windowedge = this.ie && !window.opera ? this.iecompattest().scrollLeft + this.iecompattest().clientWidth - 15 : window.pageXOffset + window.innerWidth - 15;
  47. this.dropmenuobj.contentmeasure = this.dropmenuobj.offsetWidth;
  48. if (windowedge - this.dropmenuobj.x < this.dropmenuobj.contentmeasure) edgeoffset = this.dropmenuobj.contentmeasure - obj.offsetWidth;
  49. } else {
  50. var topedge = this.ie && !window.opera ? this.iecompattest().scrollTop: window.pageYOffset;
  51. var windowedge = this.ie && !window.opera ? this.iecompattest().scrollTop + this.iecompattest().clientHeight - 15 : window.pageYOffset + window.innerHeight - 18;
  52. this.dropmenuobj.contentmeasure = this.dropmenuobj.offsetHeight;
  53. if (windowedge - this.dropmenuobj.y < this.dropmenuobj.contentmeasure) {
  54. edgeoffset = this.dropmenuobj.contentmeasure + obj.offsetHeight;
  55. if ((this.dropmenuobj.y - topedge) < this.dropmenuobj.contentmeasure) edgeoffset = this.dropmenuobj.y + obj.offsetHeight - topedge;
  56. }
  57. }
  58. return edgeoffset;
  59. },
  60. //鼠标移动到一级菜单上执行
  61. dropit: function(obj, e, dropmenuID) {
  62. //隐藏上次显示的二级菜单
  63. if (this.dropmenuobj != null) this.dropmenuobj.style.visibility = "hidden";
  64. this.clearhidemenu();
  65. if (this.ie || this.firefox) {
  66. var me = this;
  67. //一级菜单鼠标离开事件
  68. obj.onmouseout = function() {
  69. cssdropdown.delayhidemenu();
  70. };
  71. //一级菜单鼠标点击事件
  72. obj.onclick = function() {
  73. return ! cssdropdown.disablemenuclick
  74. };
  75. if (!dropmenuID) {
  76. return;
  77. }
  78. this.dropmenuobj = document.getElementById(dropmenuID);
  79. if (!this.dropmenuobj) return;
  80. //为二级菜单绑定鼠标移动到事件
  81. this.dropmenuobj.onmouseover = function() {
  82. //移除从一级菜单离开时触发的方法,防止误执行
  83. cssdropdown.clearhidemenu();
  84. }
  85. //为二级菜单绑定鼠标离开事件
  86. this.dropmenuobj.onmouseout = function(e) {
  87. cssdropdown.dynamichide(e);
  88. }
  89. //为二级菜单绑定鼠标点击事件
  90. this.dropmenuobj.onclick = function() {
  91. cssdropdown.delayhidemenu();
  92. }
  93. this.showhide(this.dropmenuobj.style, e);
  94. //调整二级菜单位置,以免位置错乱
  95. this.dropmenuobj.x = this.getposOffset(obj, "left");
  96. this.dropmenuobj.y = this.getposOffset(obj, "top");
  97. this.dropmenuobj.style.left = this.dropmenuobj.x - this.clearbrowseredge(obj, "rightedge") + "px";
  98. this.dropmenuobj.style.top = this.dropmenuobj.y - this.clearbrowseredge(obj, "bottomedge") + obj.offsetHeight + 1 + "px";
  99. this.positionshim();
  100. }
  101. },
  102. positionshim: function() {
  103. if (this.enableiframeshim && typeof this.shimobject != "undefined") {
  104. if (this.dropmenuobj.style.visibility == "visible") {
  105. this.shimobject.style.width = this.dropmenuobj.offsetWidth + "px";
  106. this.shimobject.style.height = this.dropmenuobj.offsetHeight + "px";
  107. this.shimobject.style.left = this.dropmenuobj.style.left;
  108. this.shimobject.style.top = this.dropmenuobj.style.top;
  109. }
  110. this.shimobject.style.display = (this.dropmenuobj.style.visibility == "visible") ? "block": "none";
  111. }
  112. },
  113. hideshim: function() {
  114. if (this.enableiframeshim && typeof this.shimobject != "undefined") this.shimobject.style.display = 'none';
  115. },
  116. contains_firefox: function(a, b) {
  117. while (b.parentNode) if ((b = b.parentNode) == a) return true;
  118. return false;
  119. },
  120. //隐藏二级菜单,兼容性处理
  121. dynamichide: function(e) {
  122. var evtobj = window.event ? window.event: e;
  123. //确认是鼠标离开事件,避免误触发
  124. if (this.ie && !this.dropmenuobj.contains(evtobj.toElement)) this.delayhidemenu();
  125. else if (this.firefox && e.currentTarget != evtobj.relatedTarget && !this.contains_firefox(evtobj.currentTarget, evtobj.relatedTarget)) this.delayhidemenu();
  126. },
  127. //鼠标点击菜单,或者离开菜单时执行
  128. delayhidemenu: function() {
  129. this.delayhide = setTimeout(function() {
  130. cssdropdown.dropmenuobj.style.visibility = 'hidden';
  131. cssdropdown.hideshim();
  132. cssdropdown.showAlways();
  133. },
  134. this.disappeardelay);
  135. },
  136. //移除定时方法
  137. clearhidemenu: function() {
  138. if (this.delayhide != "undefined") clearTimeout(this.delayhide);
  139. },
  140. //显示默认被选中的一级菜单
  141. showAlways: function() {
  142. if (this.always) {
  143. this.always.onmouseover({
  144. type: "mouseover"
  145. });
  146. }
  147. },
  148. //初始化
  149. startchrome: function() {
  150. for (var ids = 0; ids < arguments.length; ids++) {
  151. var menuitems = document.getElementById(arguments[ids]).getElementsByTagName("a");
  152. for (var i = 0; i < menuitems.length; i++) {
  153. var relvalue = menuitems[i].getAttribute("rel");
  154. //绑定鼠标移动到事件
  155. menuitems[i].onmouseover = function(e) {
  156. var event = typeof e != "undefined" ? e: window.event;
  157. cssdropdown.dropit(this, event, this.getAttribute("rel"));
  158. };
  159. //显示默认被选中的一级菜单
  160. if (!this.always && menuitems[i].parentNode.getAttribute("class") == "navselect") {
  161. this.always = menuitems[i];
  162. this.showAlways();
  163. }
  164. }
  165. }
  166. if (window.createPopup && !window.XmlHttpRequest) {
  167. document.write('<IFRAME id="iframeshim" src="" style="display: none; left: 0; top: 0; z-index: 90; position: absolute; filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" frameBorder="0" scrolling="no"></IFRAME>');
  168. this.shimobject = document.getElementById("iframeshim");
  169. }
  170. }
  171. }
复制代码


发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2025-1-10 19:57 , Processed in 0.038562 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表