首页 > 代码库 > js二级下拉菜单
js二级下拉菜单
看似简单的一个菜单,确需要不少的知识点
1. getByClass
getElementsByClassName 已经有大部分现代浏览器支持了,只有ie6,ie7,ie8是不支持的。所以对ie6,7,8做特别的处理就行,而ie里边有个内置的属性一直被我们所忽略,document.all,这个比一般的document.getElementsByTagName(‘*‘)或许要快一点。
2. setTimeout 的巧妙用法。
当鼠标离开某个区域时,延时隐藏另外一个区域,而到另外一个区域是清除这个定时器。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | <!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" xml:lang= "en" > <head> <meta http-equiv= "Content-Type" content= "text/html;charset=UTF-8" /> <title>二级延迟菜单 - 豪情</title> <style type= "text/css" > *{margin:0;padding:0;} body{font:12px/1.125 Arial,Helvetica,sans-serif;background: #333;} table{border-collapse:collapse;border-spacing:0;} li{list-style:none;} a:focus,input,textarea{outline-style:none;} input[type= "text" ],input[type= "password" ],textarea{outline-style:none;-webkit-appearance:none;} a{color: #0a8cd2;text-decoration:none;} a:hover{text-decoration:underline;} .none{display:none} .demo{width:600px;height:500px;margin:30px auto;padding:30px;border:5px solid #ccc;background:#fff;} .main_nav{width:404px;height:30px;margin:0 auto;} .main_nav ul li{float:left;width:100px;height:30px;border-right:1px solid #666;position:relative;} .main_nav ul li a{display:block;height:30px;line-height:30px;text-align:center;background: #333;color:#ccc;text-decoration:none;} .submenu{position:absolute;left:0;top:30px;width:100px;display:none;} .submenu dd{height:31px;} .main_nav ul li dd a{display:block;height:30px;line-height:30px;text-align:center;color: #ccc;background:#333;border-bottom:1px solid #666;} .main_nav ul li dd a:hover{background: #555;color:#fff;} </style> </head> <body> <div id= "content" class = "demo" > <div class = "main_nav hover" > <ul> <li><a class = "link" href=http://www.mamicode.com/ "javascript:" title= "" >关于我们</a> <dl class = "submenu" > <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航1</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航1</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航1</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航1</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航1</a></dd> </dl> </li> <li><a class = "link" href=http://www.mamicode.com/ "javascript:" title= "" >新闻中心</a> <dl class = "submenu" > <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航2</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航2</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航2</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航2</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航2</a></dd> </dl> </li> <li><a class = "link" href=http://www.mamicode.com/ "javascript:" title= "" >联系我们</a> <dl class = "submenu" > <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航3</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航3</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航3</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航3</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航3</a></dd> </dl> </li> <li><a class = "link" href=http://www.mamicode.com/ "javascript:" title= "" >雁过留声</a> <dl class = "submenu" > <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航4</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航4</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航4</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航4</a></dd> <dd><a href=http://www.mamicode.com/ "javascript:" title= "" >栏目子导航4</a></dd> </dl> </li> </ul> </div> </div> <script type= "text/javascript" > ( function (window){ /** * getElementsByClassname * @param oParent parent ele * @param cls classname * @return {*} */ function getByClass(oParent, cls){ if (document.getElementsByClassName){ return (oParent || document).getElementsByClassName(cls); } else { oParent = oParent || document; var res = [], child = null , reg = new RegExp( ‘(^|\\s)‘ + cls + ‘(\\s|$)‘ , ‘i‘ ); for ( var i = 0, len = oParent.all.length; i < len; i ++){ child = oParent.all[i]; if (reg.test(child.className)){ res.push(child); } } return res; } } var content = document.getElementById( ‘content‘ ), oA = getByClass(content, ‘link‘ ), child = null , timer = null , oDl = getByClass(content, ‘submenu‘ ); for ( var i = 0, len = oA.length; i < len; i++){ child = oA[i]; child.index = i; child.onmouseover = function (){ for ( var j = 0; j < len; j++){ oDl[j].style.display = ‘none‘ ; } oDl[ this .index].style.display = ‘block‘ ; clearTimeout(timer); // 清除从 dl 移回时的定时器 } child.onmouseout = function (){ var that = this ; timer = setTimeout( function (){ oDl[that.index].style.display = ‘none‘ ; }, 500); } oDl[i].onmouseover = function (){ clearTimeout(timer); } oDl[i].onmouseout = function (){ var that = this ; timer = setTimeout( function (){ that.style.display = ‘none‘ ; }, 500); } } }(window)); </script> </body> </html> |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。