找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1412|回复: 0

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

[复制链接]
发表于 2018-3-13 13:32:15 | 显示全部楼层 |阅读模式 来自 中国–河南–驻马店
可以设置一个一直都显示的二级菜单,修复了没有二级菜单时鼠标移上去仍然显示上一个二级菜单的问题.支持一级菜单鼠标离开事件
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.188163.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;& amp;nbsp;|&nbsp;&nbsp;</li>
  64. <li><a href="/a/yewushenqingtongdao/gongsiyewu/">公司业务咨询</a>&nbsp;& amp;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

  77. 代码如下:
  78. var cssdropdown = {
  79. disappeardelay: 250,
  80. disablemenuclick: false,
  81. enableswipe: 1,
  82. enableiframeshim: 1,
  83. dropmenuobj: null,
  84. ie: document.all,
  85. firefox: document.getElementById && !document.all,
  86. swipetimer: undefined,
  87. bottomclip: 0,
  88. getposOffset: function(what, offsettype) {
  89. var totaloffset = (offsettype == "left") ? what.offsetLeft: what.offsetTop;
  90. var parentEl = what.offsetParent;
  91. while (parentEl != null) {
  92. totaloffset = (offsettype == "left") ? totaloffset + parentEl.offsetLeft: totaloffset + parentEl.offsetTop;
  93. parentEl = parentEl.offsetParent;
  94. }
  95. return totaloffset;
  96. },
  97. swipeeffect: function() {
  98. if (this.bottomclip < parseInt(this.dropmenuobj.offsetHeight)) {
  99. this.bottomclip += 10 + (this.bottomclip / 10);
  100. this.dropmenuobj.style.clip = "rect(0 auto " + this.bottomclip + "px 0)";
  101. } else return;
  102. this.swipetimer = setTimeout("cssdropdown.swipeeffect()", 10);
  103. },
  104. //隐藏或者显示二级菜单
  105. showhide: function(obj, e) {
  106. if (this.ie || this.firefox) this.dropmenuobj.style.left = this.dropmenuobj.style.top = "-500px";
  107. if (e.type == "click" && obj.visibility == hidden || e.type == "mouseover") {
  108. if (this.enableswipe == 1) {
  109. if (typeof this.swipetimer != "undefined") clearTimeout(this.swipetimer);
  110. obj.clip = "rect(0 auto 0 0)";
  111. this.bottomclip = 0;
  112. this.swipeeffect();
  113. }
  114. obj.visibility = "visible";
  115. } else if (e.type == "click") obj.visibility = "hidden";
  116. },
  117. iecompattest: function() {
  118. return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement: document.body;
  119. },
  120. clearbrowseredge: function(obj, whichedge) {
  121. var edgeoffset = 0;
  122. if (whichedge == "rightedge") {
  123. var windowedge = this.ie && !window.opera ? this.iecompattest().scrollLeft + this.iecompattest().clientWidth - 15 : window.pageXOffset + window.innerWidth - 15;
  124. this.dropmenuobj.contentmeasure = this.dropmenuobj.offsetWidth;
  125. if (windowedge - this.dropmenuobj.x < this.dropmenuobj.contentmeasure) edgeoffset = this.dropmenuobj.contentmeasure - obj.offsetWidth;
  126. } else {
  127. var topedge = this.ie && !window.opera ? this.iecompattest().scrollTop: window.pageYOffset;
  128. var windowedge = this.ie && !window.opera ? this.iecompattest().scrollTop + this.iecompattest().clientHeight - 15 : window.pageYOffset + window.innerHeight - 18;
  129. this.dropmenuobj.contentmeasure = this.dropmenuobj.offsetHeight;
  130. if (windowedge - this.dropmenuobj.y < this.dropmenuobj.contentmeasure) {
  131. edgeoffset = this.dropmenuobj.contentmeasure + obj.offsetHeight;
  132. if ((this.dropmenuobj.y - topedge) < this.dropmenuobj.contentmeasure) edgeoffset = this.dropmenuobj.y + obj.offsetHeight - topedge;
  133. }
  134. }
  135. return edgeoffset;
  136. },
  137. //鼠标移动到一级菜单上执行
  138. dropit: function(obj, e, dropmenuID) {
  139. //隐藏上次显示的二级菜单
  140. if (this.dropmenuobj != null) this.dropmenuobj.style.visibility = "hidden";
  141. this.clearhidemenu();
  142. if (this.ie || this.firefox) {
  143. var me = this;
  144. //一级菜单鼠标离开事件
  145. obj.onmouseout = function() {
  146. cssdropdown.delayhidemenu();
  147. };
  148. //一级菜单鼠标点击事件
  149. obj.onclick = function() {
  150. return ! cssdropdown.disablemenuclick
  151. };
  152. if (!dropmenuID) {
  153. return;
  154. }
  155. this.dropmenuobj = document.getElementById(dropmenuID);
  156. if (!this.dropmenuobj) return;
  157. //为二级菜单绑定鼠标移动到事件
  158. this.dropmenuobj.onmouseover = function() {
  159. //移除从一级菜单离开时触发的方法,防止误执行
  160. cssdropdown.clearhidemenu();
  161. }
  162. //为二级菜单绑定鼠标离开事件
  163. this.dropmenuobj.onmouseout = function(e) {
  164. cssdropdown.dynamichide(e);
  165. }
  166. //为二级菜单绑定鼠标点击事件
  167. this.dropmenuobj.onclick = function() {
  168. cssdropdown.delayhidemenu();
  169. }
  170. this.showhide(this.dropmenuobj.style, e);
  171. //调整二级菜单位置,以免位置错乱
  172. this.dropmenuobj.x = this.getposOffset(obj, "left");
  173. this.dropmenuobj.y = this.getposOffset(obj, "top");
  174. this.dropmenuobj.style.left = this.dropmenuobj.x - this.clearbrowseredge(obj, "rightedge") + "px";
  175. this.dropmenuobj.style.top = this.dropmenuobj.y - this.clearbrowseredge(obj, "bottomedge") + obj.offsetHeight + 1 + "px";
  176. this.positionshim();
  177. }
  178. },
  179. positionshim: function() {
  180. if (this.enableiframeshim && typeof this.shimobject != "undefined") {
  181. if (this.dropmenuobj.style.visibility == "visible") {
  182. this.shimobject.style.width = this.dropmenuobj.offsetWidth + "px";
  183. this.shimobject.style.height = this.dropmenuobj.offsetHeight + "px";
  184. this.shimobject.style.left = this.dropmenuobj.style.left;
  185. this.shimobject.style.top = this.dropmenuobj.style.top;
  186. }
  187. this.shimobject.style.display = (this.dropmenuobj.style.visibility == "visible") ? "block": "none";
  188. }
  189. },
  190. hideshim: function() {
  191. if (this.enableiframeshim && typeof this.shimobject != "undefined") this.shimobject.style.display = 'none';
  192. },
  193. contains_firefox: function(a, b) {
  194. while (b.parentNode) if ((b = b.parentNode) == a) return true;
  195. return false;
  196. },
  197. //隐藏二级菜单,兼容性处理
  198. dynamichide: function(e) {
  199. var evtobj = window.event ? window.event: e;
  200. //确认是鼠标离开事件,避免误触发
  201. if (this.ie && !this.dropmenuobj.contains(evtobj.toElement)) this.delayhidemenu();
  202. else if (this.firefox && e.currentTarget != evtobj.relatedTarget && !this.contains_firefox(evtobj.currentTarget, evtobj.relatedTarget)) this.delayhidemenu();
  203. },
  204. //鼠标点击菜单,或者离开菜单时执行
  205. delayhidemenu: function() {
  206. this.delayhide = setTimeout(function() {
  207. cssdropdown.dropmenuobj.style.visibility = 'hidden';
  208. cssdropdown.hideshim();
  209. cssdropdown.showAlways();
  210. },
  211. this.disappeardelay);
  212. },
  213. //移除定时方法
  214. clearhidemenu: function() {
  215. if (this.delayhide != "undefined") clearTimeout(this.delayhide);
  216. },
  217. //显示默认被选中的一级菜单
  218. showAlways: function() {
  219. if (this.always) {
  220. this.always.onmouseover({
  221. type: "mouseover"
  222. });
  223. }
  224. },
  225. //初始化
  226. startchrome: function() {
  227. for (var ids = 0; ids < arguments.length; ids++) {
  228. var menuitems = document.getElementById(arguments[ids]).getElementsByTagName("a");
  229. for (var i = 0; i < menuitems.length; i++) {
  230. var relvalue = menuitems[i].getAttribute("rel");
  231. //绑定鼠标移动到事件
  232. menuitems[i].onmouseover = function(e) {
  233. var event = typeof e != "undefined" ? e: window.event;
  234. cssdropdown.dropit(this, event, this.getAttribute("rel"));
  235. };
  236. //显示默认被选中的一级菜单
  237. if (!this.always && menuitems[i].parentNode.getAttribute("class") == "navselect") {
  238. this.always = menuitems[i];
  239. this.showAlways();
  240. }
  241. }
  242. }
  243. if (window.createPopup && !window.XmlHttpRequest) {
  244. 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>');
  245. this.shimobject = document.getElementById("iframeshim");
  246. }
  247. }
  248. }
复制代码

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

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

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

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

关闭

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

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

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

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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