首页 > 代码库 > javascript 可控速度的上下拉菜单

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.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‘){
        sideSpanBtn[i].onclick = function(){ul_slide(this,‘ul‘);}
    }
}
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.nodeType!=1 && x!=null){
        x=x.nextSibling;
    }
    return x;
}

function getPrevEleNode(n){
    var x=n.previousSibling;
    while (x.nodeType!=1 && x!=null){
        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‘;
    }
    
}