Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

DZ官方下拉菜单的代码

[复制链接]
hufanyun 发表于 2005-10-4 17:01:04 | 显示全部楼层 |阅读模式
演示地址: http://www.comsenz.com/

上传附件里的文件到需要的目录下或你自己修改目录名也可以的
注意在<body>里加上onload=init();
下面代码加在JS或则CSS或则需要的地方
  1. <SCRIPT language=javascript>
  2. <!--

  3. mtDropDown.spacerGif = "";
  4. mtDropDown.dingbatOn = "";
  5. mtDropDown.dingbatOff = "";
  6. mtDropDown.dingbatSize = 14;
  7. mtDropDown.menuPadding = 1;
  8. mtDropDown.itemPadding = 4;
  9. mtDropDown.shadowSize = 2;
  10. mtDropDown.shadowOffset = 3;
  11. mtDropDown.shadowColor = "#888";
  12. mtDropDown.shadowPng = "";
  13. mtDropDown.backgroundColor = "#ffffff";
  14. mtDropDown.backgroundPng = "";
  15. mtDropDown.hideDelay = 200;
  16. mtDropDown.slideTime = 300;

  17. mtDropDown.reference = {topLeft:1,topRight:2,bottomLeft:3,bottomRight:4};
  18. mtDropDown.direction = {down:1,right:2};
  19. mtDropDown.registry = [];
  20. mtDropDown._maxZ = 100;

  21. mtDropDown.isSupported = function() {
  22. if (typeof mtDropDown.isSupported.r == "boolean")
  23. return mtDropDown.isSupported.r;
  24. var ua = navigator.userAgent.toLowerCase();
  25. var an = navigator.appName;
  26. var r = false;
  27. if (ua.indexOf("gecko") > -1) r = true;
  28. else if (an == "Microsoft Internet Explorer") {
  29. if (document.getElementById) r = true;
  30. }
  31. mtDropDown.isSupported.r = r;
  32. return r;
  33. }

  34. mtDropDown.initialize = function() {
  35. for (var i = 0, menu = null; menu = this.registry[i]; i++) {
  36. menu.initialize();
  37. }
  38. }

  39. mtDropDown.renderAll = function() {
  40. var aMenuHtml = [];
  41. for (var i = 0, menu = null; menu = this.registry[i]; i++) {
  42. aMenuHtml[i] = menu.toString();
  43. }
  44. document.write(aMenuHtml.join(""));
  45. }

  46. function mtDropDown(oActuator, iDirection, iLeft, iTop, iReferencePoint, parentMenuSet) {

  47. this.addItem = addItem;
  48. this.addMenu = addMenu;
  49. this.toString = toString;
  50. this.initialize = initialize;
  51. this.isOpen = false;
  52. this.show = show;
  53. this.hide = hide;
  54. this.items = [];

  55. this.onactivate = new Function();
  56. this.ondeactivate = new Function();
  57. this.onmouseover = new Function();
  58. this.onqueue = new Function();

  59. this.index = mtDropDown.registry.length;
  60. mtDropDown.registry[this.index] = this;
  61. var id = "mtDropDown" + this.index;
  62. var contentHeight = null;
  63. var contentWidth = null;
  64. var childMenuSet = null;
  65. var animating = false;
  66. var childMenus = [];
  67. var slideAccel = -1;
  68. var elmCache = null;
  69. var ready = false;
  70. var _this = this;
  71. var a = null;
  72. var pos = iDirection == mtDropDown.direction.down ? "top" : "left";
  73. var dim = null;

  74. function addItem(sText, sUrl) {
  75. var item = new mtDropDownItem(sText, sUrl, this);
  76. item._index = this.items.length;
  77. this.items[item._index] = item;
  78. }
  79. function addMenu(oMenuItem) {
  80. if (!oMenuItem.parentMenu == this) throw new Error("Cannot add a menu here");
  81. if (childMenuSet == null) childMenuSet = new mtDropDownSet(mtDropDown.direction.right, -5, 2, mtDropDown.reference.topRight);
  82. var m = childMenuSet.addMenu(oMenuItem);
  83. childMenus[oMenuItem._index] = m;
  84. m.onmouseover = child_mouseover;
  85. m.ondeactivate = child_deactivate;
  86. m.onqueue = child_queue;
  87. return m;
  88. }
  89. function initialize() {
  90. initCache();
  91. initEvents();
  92. initSize();
  93. ready = true;
  94. }
  95. function show() {

  96. if (ready) {
  97. _this.isOpen = true;
  98. animating = true;
  99. setContainerPos();
  100. elmCache["clip"].style.visibility = "visible";
  101. elmCache["clip"].style.zIndex = mtDropDown._maxZ++;

  102. slideStart();
  103. _this.onactivate();
  104. }
  105. }
  106. function hide() {
  107. if (ready) {
  108. _this.isOpen = false;
  109. animating = true;
  110. for (var i = 0, item = null; item = elmCache.item[i]; i++)
  111. dehighlight(item);
  112. if (childMenuSet) childMenuSet.hide();
  113. slideStart();
  114. _this.ondeactivate();
  115. }
  116. }
  117. function setContainerPos() {
  118. var sub = oActuator.constructor == mtDropDownItem;
  119. var act = sub ? oActuator.parentMenu.elmCache["item"][oActuator._index] : oActuator;
  120. var el = act;
  121. var x = 0;
  122. var y = 0;
  123. var minX = 0;
  124. var maxX = (window.innerWidth ? window.innerWidth : document.body.clientWidth) - parseInt(elmCache["clip"].style.width);
  125. var minY = 0;
  126. var maxY = (window.innerHeight ? window.innerHeight : document.body.clientHeight) - parseInt(elmCache["clip"].style.height);

  127. while (sub ? el.parentNode.className.indexOf("mtDropdownMenu") == -1 : el.offsetParent) {
  128. x += el.offsetLeft;
  129. y += el.offsetTop;
  130. if (el.scrollLeft) x -= el.scrollLeft;
  131. if (el.scrollTop) y -= el.scrollTop;
  132. el = el.offsetParent;
  133. }

  134. if (oActuator.constructor == mtDropDownItem) {
  135. x += parseInt(el.parentNode.style.left);
  136. y += parseInt(el.parentNode.style.top);
  137. }
  138. switch (iReferencePoint) {
  139. case mtDropDown.reference.topLeft:
  140. break;
  141. case mtDropDown.reference.topRight:
  142. x += act.offsetWidth;
  143. break;
  144. case mtDropDown.reference.bottomLeft:
  145. y += act.offsetHeight;
  146. break;
  147. case mtDropDown.reference.bottomRight:
  148. x += act.offsetWidth;
  149. y += act.offsetHeight;
  150. break;
  151. }
  152. x += iLeft;
  153. y += iTop;
  154. x = Math.max(Math.min(x, maxX), minX);
  155. y = Math.max(Math.min(y, maxY), minY);
  156. elmCache["clip"].style.left = x + "px";
  157. elmCache["clip"].style.top = y + "px";
  158. }
  159. function slideStart() {
  160. var x0 = parseInt(elmCache["content"].style[pos]);
  161. var x1 = _this.isOpen ? 0 : -dim;
  162. if (a != null) a.stop();
  163. a = new Accelimation(x0, x1, mtDropDown.slideTime, slideAccel);
  164. a.onframe = slideFrame;
  165. a.onend = slideEnd;
  166. a.start();
  167. }
  168. function slideFrame(x) {
  169. elmCache["content"].style[pos] = x + "px";
  170. }
  171. function slideEnd() {
  172. if (!_this.isOpen) elmCache["clip"].style.visibility = "hidden";
  173. animating = false;
  174. }
  175. function initSize() {

  176. var ow = elmCache["items"].offsetWidth;
  177. var oh = elmCache["items"].offsetHeight;
  178. var ua = navigator.userAgent.toLowerCase();

  179. elmCache["clip"].style.width = ow + mtDropDown.shadowSize + 2 + "px";
  180. elmCache["clip"].style.height = oh + mtDropDown.shadowSize + 2 + "px";

  181. elmCache["content"].style.width = ow + mtDropDown.shadowSize + "px";
  182. elmCache["content"].style.height = oh + mtDropDown.shadowSize + "px";
  183. contentHeight = oh + mtDropDown.shadowSize;
  184. contentWidth = ow + mtDropDown.shadowSize;
  185. dim = iDirection == mtDropDown.direction.down ? contentHeight : contentWidth;

  186. elmCache["content"].style[pos] = -dim - mtDropDown.shadowSize + "px";
  187. elmCache["clip"].style.visibility = "hidden";

  188. if (ua.indexOf("mac") == -1 || ua.indexOf("gecko") > -1) {

  189. elmCache["background"].style.width = ow + "px";
  190. elmCache["background"].style.height = oh + "px";
  191. elmCache["background"].style.backgroundColor = mtDropDown.backgroundColor;

  192. elmCache["shadowRight"].style.left = ow + "px";
  193. elmCache["shadowRight"].style.height = oh - (mtDropDown.shadowOffset - mtDropDown.shadowSize) + "px";
  194. elmCache["shadowRight"].style.backgroundColor = mtDropDown.shadowColor;



  195. elmCache["shadowBottom"].style.top = oh + "px";
  196. elmCache["shadowBottom"].style.width = ow - mtDropDown.shadowOffset + "px";
  197. elmCache["shadowBottom"].style.backgroundColor = mtDropDown.shadowColor;
  198. }

  199. else {

  200. elmCache["background"].firstChild.src = mtDropDown.backgroundPng;
  201. elmCache["background"].firstChild.width = ow;
  202. elmCache["background"].firstChild.height = oh;

  203. elmCache["shadowRight"].firstChild.src = mtDropDown.shadowPng;
  204. elmCache["shadowRight"].style.left = ow + "px";
  205. elmCache["shadowRight"].firstChild.width = mtDropDown.shadowSize;
  206. elmCache["shadowRight"].firstChild.height = oh - (mtDropDown.shadowOffset - mtDropDown.shadowSize);



  207. elmCache["shadowBottom"].firstChild.src = mtDropDown.shadowPng;
  208. elmCache["shadowBottom"].style.top = oh + "px";
  209. elmCache["shadowBottom"].firstChild.height = mtDropDown.shadowSize;
  210. elmCache["shadowBottom"].firstChild.width = ow - mtDropDown.shadowOffset;
  211. }
  212. }
  213. function initCache() {
  214. var menu = document.getElementById(id);
  215. var all = menu.all ? menu.all : menu.getElementsByTagName("*");
  216. elmCache = {};
  217. elmCache["clip"] = menu;
  218. elmCache["item"] = [];
  219. for (var i = 0, elm = null; elm = all[i]; i++) {
  220. switch (elm.className) {
  221. case "items":
  222. case "content":
  223. case "background":
  224. case "shadowRight":
  225. case "shadowBottom":
  226. elmCache[elm.className] = elm;
  227. break;
  228. case "item":
  229. elm._index = elmCache["item"].length;
  230. elmCache["item"][elm._index] = elm;
  231. break;
  232. }
  233. }

  234. _this.elmCache = elmCache;
  235. }
  236. function initEvents() {

  237. for (var i = 0, item = null; item = elmCache.item[i]; i++) {
  238. item.onmouseover = item_mouseover;
  239. item.onmouseout = item_mouseout;
  240. item.onclick = item_click;
  241. }

  242. if (typeof oActuator.tagName != "undefined") {
  243. oActuator.onmouseover = actuator_mouseover;
  244. oActuator.onmouseout = actuator_mouseout;
  245. }

  246. elmCache["content"].onmouseover = content_mouseover;
  247. elmCache["content"].onmouseout = content_mouseout;
  248. }
  249. function highlight(oRow) {
  250. oRow.className = "item hover";
  251. if (childMenus[oRow._index])
  252. oRow.lastChild.firstChild.src = mtDropDown.dingbatOn;
  253. }
  254. function dehighlight(oRow) {
  255. oRow.className = "item";
  256. if (childMenus[oRow._index])
  257. oRow.lastChild.firstChild.src = mtDropDown.dingbatOff;
  258. }
  259. function item_mouseover() {
  260. if (!animating) {
  261. highlight(this);
  262. if (childMenus[this._index])
  263. childMenuSet.showMenu(childMenus[this._index]);
  264. else if (childMenuSet) childMenuSet.hide();
  265. }
  266. }
  267. function item_mouseout() {
  268. if (!animating) {
  269. if (childMenus[this._index])
  270. childMenuSet.hideMenu(childMenus[this._index]);
  271. else
  272. dehighlight(this);
  273. }
  274. }
  275. function item_click() {
  276. if (!animating) {
  277. if (_this.items[this._index].url)
  278. location.href = _this.items[this._index].url;
  279. }
  280. }
  281. function actuator_mouseover() {
  282. parentMenuSet.showMenu(_this);
  283. }
  284. function actuator_mouseout() {
  285. parentMenuSet.hideMenu(_this);
  286. }
  287. function content_mouseover() {
  288. if (!animating) {
  289. parentMenuSet.showMenu(_this);
  290. _this.onmouseover();
  291. }
  292. }
  293. function content_mouseout() {
  294. if (!animating) {
  295. parentMenuSet.hideMenu(_this);
  296. }
  297. }
  298. function child_mouseover() {
  299. if (!animating) {
  300. parentMenuSet.showMenu(_this);
  301. }
  302. }
  303. function child_deactivate() {
  304. for (var i = 0; i < childMenus.length; i++) {
  305. if (childMenus[i] == this) {
  306. dehighlight(elmCache["item"][i]);
  307. break;
  308. }
  309. }
  310. }
  311. function child_queue() {
  312. parentMenuSet.hideMenu(_this);
  313. }
  314. function toString() {
  315. var aHtml = [];
  316. var sClassName = "mtDropdownMenu" + (oActuator.constructor != mtDropDownItem ? " top" : "");
  317. for (var i = 0, item = null; item = this.items[i]; i++) {
  318. aHtml[i] = item.toString(childMenus[i]);
  319. }
  320. return '<div id="' + id + '" class="' + sClassName + '">' +
  321. '<div class="content"><table class="items" cellpadding="0" cellspacing="0" border="0">' +
  322. '<tr><td colspan="2"><img src="' + mtDropDown.spacerGif + '" width="1" height="' + mtDropDown.menuPadding + '"></td></tr>' +
  323. aHtml.join('') +
  324. '<tr><td colspan="2"><img src="' + mtDropDown.spacerGif + '" width="1" height="' + mtDropDown.menuPadding + '"></td></tr></table>' +
  325. '<div class="shadowBottom"><img src="' + mtDropDown.spacerGif + '" width="1" height="1"></div>' +
  326. '<div class="shadowRight"><img src="' + mtDropDown.spacerGif + '" width="1" height="1"></div>' +
  327. '<div class="background"><img src="' + mtDropDown.spacerGif + '" width="1" height="1"></div>' +
  328. '</div></div>';
  329. }
  330. }

  331. mtDropDownSet.registry = [];
  332. function mtDropDownSet(iDirection, iLeft, iTop, iReferencePoint) {

  333. this.addMenu = addMenu;
  334. this.showMenu = showMenu;
  335. this.hideMenu = hideMenu;
  336. this.hide = hide;

  337. var menus = [];
  338. var _this = this;
  339. var current = null;
  340. this.index = mtDropDownSet.registry.length;
  341. mtDropDownSet.registry[this.index] = this;

  342. function addMenu(oActuator) {
  343. var m = new mtDropDown(oActuator, iDirection, iLeft, iTop, iReferencePoint, this);
  344. menus[menus.length] = m;
  345. return m;
  346. }
  347. function showMenu(oMenu) {
  348. if (oMenu != current) {

  349. if (current != null) hide(current);

  350. current = oMenu;

  351. oMenu.show();
  352. }
  353. else {

  354. cancelHide(oMenu);
  355. }
  356. }
  357. function hideMenu(oMenu) {

  358. if (current == oMenu && oMenu.isOpen) {

  359. if (!oMenu.hideTimer) scheduleHide(oMenu);
  360. }
  361. }
  362. function scheduleHide(oMenu) {

  363. oMenu.onqueue();
  364. oMenu.hideTimer = window.setTimeout("mtDropDownSet.registry[" + _this.index + "].hide(mtDropDown.registry[" + oMenu.index + "])", mtDropDown.hideDelay);
  365. }
  366. function cancelHide(oMenu) {

  367. if (oMenu.hideTimer) {
  368. window.clearTimeout(oMenu.hideTimer);
  369. oMenu.hideTimer = null;
  370. }
  371. }
  372. function hide(oMenu) {
  373. if (!oMenu && current) oMenu = current;
  374. if (oMenu && current == oMenu && oMenu.isOpen) {

  375. cancelHide(oMenu);
  376. current = null;
  377. oMenu.hideTimer = null;
  378. oMenu.hide();
  379. }
  380. }
  381. }

  382. function mtDropDownItem(sText, sUrl, oParent) {
  383. this.toString = toString;
  384. this.text = sText;
  385. this.url = sUrl;
  386. this.parentMenu = oParent;
  387. function toString(bDingbat) {
  388. var sDingbat = bDingbat ? mtDropDown.dingbatOff : mtDropDown.spacerGif;
  389. var iEdgePadding = mtDropDown.itemPadding + mtDropDown.menuPadding;
  390. var sPaddingLeft = "padding:" + mtDropDown.itemPadding + "px; padding-left:" + iEdgePadding + "px;"
  391. var sPaddingRight = "padding:" + mtDropDown.itemPadding + "px; padding-right:" + iEdgePadding + "px;"
  392. return '<tr class="item"><td nowrap style="' + sPaddingLeft + '">' +
  393. sText + '</td></tr>';

  394. //sText + '</td><td width="14" style="' + sPaddingRight + '">' +
  395. //'</td></tr>';
  396. }
  397. }


  398. function Accelimation(from, to, time, zip) {
  399. if (typeof zip == "undefined") zip = 0;
  400. if (typeof unit == "undefined") unit = "px";
  401. this.x0 = from;
  402. this.x1 = to;
  403. this.dt = time;
  404. this.zip = -zip;
  405. this.unit = unit;
  406. this.timer = null;
  407. this.onend = new Function();
  408. this.onframe = new Function();
  409. }




  410. Accelimation.prototype.start = function() {
  411. this.t0 = new Date().getTime();
  412. this.t1 = this.t0 + this.dt;
  413. var dx = this.x1 - this.x0;
  414. this.c1 = this.x0 + ((1 + this.zip) * dx / 3);
  415. this.c2 = this.x0 + ((2 + this.zip) * dx / 3);
  416. Accelimation._add(this);
  417. }

  418. Accelimation.prototype.stop = function() {
  419. Accelimation._remove(this);
  420. }




  421. Accelimation.prototype._paint = function(time) {
  422. if (time < this.t1) {
  423. var elapsed = time - this.t0;
  424. this.onframe(Accelimation._getBezier(elapsed/this.dt,this.x0,this.x1,this.c1,this.c2));
  425. }
  426. else this._end();
  427. }

  428. Accelimation.prototype._end = function() {
  429. Accelimation._remove(this);
  430. this.onframe(this.x1);
  431. this.onend();
  432. }




  433. Accelimation._add = function(o) {
  434. var index = this.instances.length;
  435. this.instances[index] = o;

  436. if (this.instances.length == 1) {
  437. this.timerID = window.setInterval("Accelimation._paintAll()", this.targetRes);
  438. }
  439. }

  440. Accelimation._remove = function(o) {
  441. for (var i = 0; i < this.instances.length; i++) {
  442. if (o == this.instances[i]) {
  443. this.instances = this.instances.slice(0,i).concat( this.instances.slice(i+1) );
  444. break;
  445. }
  446. }

  447. if (this.instances.length == 0) {
  448. window.clearInterval(this.timerID);
  449. this.timerID = null;
  450. }
  451. }

  452. Accelimation._paintAll = function() {
  453. var now = new Date().getTime();
  454. for (var i = 0; i < this.instances.length; i++) {
  455. this.instances[i]._paint(now);
  456. }
  457. }

  458. Accelimation._B1 = function(t) { return t*t*t }
  459. Accelimation._B2 = function(t) { return 3*t*t*(1-t) }
  460. Accelimation._B3 = function(t) { return 3*t*(1-t)*(1-t) }
  461. Accelimation._B4 = function(t) { return (1-t)*(1-t)*(1-t) }

  462. Accelimation._getBezier = function(percent,startPos,endPos,control1,control2) {
  463. return endPos * this._B1(percent) + control2 * this._B2(percent) + control1 * this._B3(percent) + startPos * this._B4(percent);
  464. }



  465. Accelimation.instances = [];
  466. Accelimation.targetRes = 10;
  467. Accelimation.timerID = null;
  468. //-->
  469. </SCRIPT>
  470. <SCRIPT language=javascript>
  471. <!--
  472. //dropdown_initialize.js
  473.                 var preloaded = [];

  474.                 for (var i = 1; i <= 8; i++) {
  475.                         preloaded[i] = [loadImage(i + "-1.gif"), loadImage(i + "-1.gif")];                //设置图片的OnMouseOver和OnMouseOut的路径
  476.                 }

  477.                 function init() {

  478.                         if (mtDropDown.isSupported()) {
  479.                                 mtDropDown.initialize();

  480.                         }
  481.                 }

  482.                 function loadImage(sFilename) {
  483.                         var img = new Image();
  484.                         img.src ="" + sFilename;
  485.                         return img;
  486.                 }

  487.                 function swapImage(imgName, sFilename) {
  488.                         document.images[imgName].src = sFilename;
  489.                 }
  490. //-->
  491. </SCRIPT>
  492. <link href="jsimg/welab.css" type=text/css rel=stylesheet>
复制代码


以下是显示时需要的代码
  1. <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  2.   <tr>
  3.     <td valign="bottom" ><a id=menu1 href="#">网页特效</a></td>
  4.     <td valign="bottom"><a id=menu2 href="#">插件中心</a></td>
  5.     <td valign="bottom"><a id=menu3 href="#">网络学院</a></td>
  6.     <td valign="bottom"><a id=menu4 href="#">下载基地</a></td>
  7.     <td valign="bottom"><a id=menu5 href="#">网站联盟</a></td>
  8.     <td valign="bottom"><a id=menu6 href="#">网站建设</a></td>
  9.   </tr>

  10. </table><script language=javascript src="jsimg/dropdown_content.js"></script>
复制代码

[ 本帖最后由 hufanyun 于 2005-10-4 17:02 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
jimmyjimmyqqq 发表于 2005-10-4 17:04:16 | 显示全部楼层
沙發!我搶到了.. = = "
回复

使用道具 举报

七喜仔 发表于 2005-10-4 17:04:27 | 显示全部楼层
慢一步~頂.
回复

使用道具 举报

 楼主| hufanyun 发表于 2005-10-4 17:05:20 | 显示全部楼层
原帖由 jimmyjimmyqqq 于 2005-10-4 17:04 发表
沙發!我搶到了.. = = "

本来不想发这的哈.但是昨天被B的只好发一个了
回复

使用道具 举报

jimmyjimmyqqq 发表于 2005-10-4 17:06:22 | 显示全部楼层
哈哈,沒緊要,最重要是好用... @@"
回复

使用道具 举报

 楼主| hufanyun 发表于 2005-10-4 17:07:35 | 显示全部楼层
原帖由 jimmyjimmyqqq 于 2005-10-4 17:06 发表
哈哈,沒緊要,最重要是好用... @@"

这个和官方的是一样的.好看多了吧
回复

使用道具 举报

bydy 发表于 2005-10-4 17:12:08 | 显示全部楼层
css里加的代码好长啊
回复

使用道具 举报

 楼主| hufanyun 发表于 2005-10-4 17:12:42 | 显示全部楼层
原帖由 bydy 于 2005-10-4 17:12 发表
css里加的代码好长啊

可以用JS调用哈
回复

使用道具 举报

AgFx 发表于 2005-10-4 18:10:30 | 显示全部楼层
哇,不错不错。。和freddy的有什么分别呢?
回复

使用道具 举报

lu5266 发表于 2005-10-4 18:25:12 | 显示全部楼层
下拉自然了
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-12-22 20:36 , Processed in 0.029536 second(s), 2 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

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