javascript 可控速度的上下拉菜单
样式部分
<style type="text/css"> *{ margin:0; padding:0; border:none; background:none;} ul,li{ list-style:none;} .side-nav-list{ width:200px; background:#eee; border:#666 1px solid; padding:5px 10px; margin:20px auto;} .side-nav-list li{ border-bottom:#666 1px solid;} .side-nav-list-2,.side-nav-list-3{ display:none;} .side-nav-list-2 li{ border-bottom:#999 1px dotted;} .side-nav-list-3 li{ border-bottom:none;} .side-nav-list .side-nav-list-2 a{ text-indent:1em;} .side-nav-list .side-nav-list-3 a{ text-indent:2em;} .side-nav-list a,.side-nav-list .slide-btn{color:#000; display:inline-block; height:24px; line-height:24px; overflow:hidden;} .side-nav-list a{ text-decoration:none; width:80%; font-size:12px;} .side-nav-list a:hover{ color:#C00;} .side-nav-list .slide-btn{width:20%;text-align:center; cursor:pointer; font-weight:bold; font-size:16px;} .side-nav-list .active-li{ background:#FFF; font-weight:bold;} </style>
HTML部分
<ul class="side-nav-list" id="side-nav-list"> <li><a href="javascript:;">家居灯具</a><span class="slide-btn">+</span> <ul class="side-nav-list-2"> <li><a href="slide.htm">客厅</a><span class="slide-btn">+</span> <ul class="side-nav-list-3"> <li><a href="http://test.php?id=12">吸顶灯<span>(146)</span></a></li> <li><a href="http://test.php?id=13">落地灯<span>(146)</span></a></li> <li><a href="http://test.php?id=14">台灯<span>(146)</span></a></li> <li><a href="http://test.php?id=15">壁灯<span>(146)</span></a></li> <li><a href="http://test.php?id=16">筒灯<span>(146)</span></a></li> <li><a href="http://test.php?id=17">天花灯<span>(146)</span></a></li> </ul> </li> <li><a href="http://test.php?id=7">卧室</a><span class="slide-btn">+</span> <ul class="side-nav-list-3"> <li><a href="http://test.php?id=23">吸顶灯<span>(146)</span></a></li> <li><a href="http://test.php?id=24">落地灯<span>(146)</span></a></li> <li><a href="http://test.php?id=25">台灯<span>(146)</span></a></li> <li><a href="http://test.php?id=26">壁灯<span>(146)</span></a></li> <li><a href="http://test.php?id=27">镜前灯<span>(146)</span></a></li> <li><a href="http://test.php?id=28">天花灯<span>(146)</span></a></li> </ul> </li> <li><a href="http://test.php?id=8">儿童房</a><span class="slide-btn">+</span> <ul class="side-nav-list-3"> <li><a href="http://test.php?id=29">吸顶灯<span>(146)</span></a></li> <li><a href="http://test.php?id=30">落地灯<span>(146)</span></a></li> <li><a href="http://test.php?id=31">儿童护眼灯<span>(146)</span></a></li> <li><a href="http://test.php?id=32">吊线灯<span>(146)</span></a></li> </ul> </li> <li><a href="http://test.php?id=10">厨卫</a><span class="slide-btn">+</span> <ul class="side-nav-list-3"> <li><a href="http://test.php?id=37">厨卫吸顶灯<span>(146)</span></a></li> </ul> </li> <li><a href="http://test.php?id=11">阳台</a><span class="slide-btn">+</span> <ul class="side-nav-list-3"> <li><a href="http://test.php?id=38">吸顶灯<span>(146)</span></a></li> <li><a href="http://test.php?id=39">感应灯<span>(146)</span></a></li> </ul> </li> </ul> </li> <li><a href="javascript:;">光源电器</a><span class="slide-btn">+</span> <ul class="side-nav-list-2"> <li><a href="http://test.php?id=42">3U灯</a><span class="slide-btn">+</span> <ul class="side-nav-list-3"> </ul> </li> </ul> </li> </ul> <script type="text/javascript" src="slide.js"></script> <script type="text/javascript"> var slideA = sideNavList.getElementsByTagName('a'); var lhref = location.href; var ids = lhref.match(/id=\d+/); if(ids){ for(var i=0; i < slideA.length; i++){ var tlink = slideA[i].href.match(/id=\d+/); if(tlink && (tlink[0] === ids[0]) ){ var pn = slideA[i].parentNode.parentNode; if(pn.tagName.toLowerCase()=='ul'){ show_nav_sel(pn); } slideA[i].parentNode.className = 'active-li'; } } } function show_nav_sel(o){ var p_t = o.parentNode.parentNode; if(p_t.tagName.toLowerCase()=='ul'){ arguments.callee(p_t); } var pen = getPrevEleNode(o); if(pen!=null && pen.tagName.toLowerCase()=='span' && pen.className=='slide-btn'){ pen.click(); } } </script>
Slide.js部分
// JavaScript Document var sideNavList = document.getElementById('side-nav-list'), sideSpanBtn = sideNavList.getElementsByTagName('span'), spanLength = sideSpanBtn.length, i = 0; for(; i < spanLength ; i++){ if(sideSpanBtn[i].className == 'slide-btn'){ var next_ul = getNextEleNode(sideSpanBtn[i]); var ul_in_li_length = next_ul && next_ul.tagName.toLowerCase()=='ul' ? next_ul.getElementsByTagName('li').length : 0; if(ul_in_li_length){ sideSpanBtn[i].onclick = function(){ul_slide(this,'ul');} }else{ sideSpanBtn[i].style.display = 'none'; } } } function ul_slide(o,s_tag){ o.innerHTML = o.innerHTML=='+'?'-':'+'; var n_s = getNextEleNode(o); if(!n_s){return false;} if(n_s.tagName.toLowerCase()==s_tag){ var n_s_display = n_s.style.display; if(n_s_display =='none' || !n_s_display || (n_s_display=='block' && !n_s.offsetHeight)){ slideDown(n_s,500); }else{ slideUp(n_s,500); } } } function getNextEleNode(n){ var x=n.nextSibling; while ( x!=null && x.nodeType!=1){ x=x.nextSibling; } return x; } function getPrevEleNode(n){ var x=n.previousSibling; while ( x!=null && x.nodeType!=1){ x=x.previousSibling; } return x; } function slideDown(o,t){ o.style.display = 'block'; o.style.overflow = 'hidden'; var h = o.scrollHeight; if(h>0){ var m = h / t * 10; set_slide_down(o,h,m,t,1); } } function slideUp(o,t){ var h = o.scrollHeight; if(h>0){ var m = h / t * 10; set_slide_up(o,h,m,t,1); } } function set_slide_down(o,h,m,t,n){ if( m * n < h ){ o.style.height = (m*n)+'px'; setTimeout(function(){set_slide_down(o,h,m,t,n+1)},1); }else{ o.style.height = "auto"; } } function set_slide_up(o,h,m,t,n){ if( ( h-( m * n ) ) > 0 ){ o.style.height = h-(m*n)+'px'; setTimeout(function(){set_slide_up(o,h,m,t,n+1)},1); }else{ o.style.height = '0px'; } }