jQuery 無限級菜單實現

fmms 12年前發布 | 34K 次閱讀 jQuery jQuery菜單插件 jQuery插件

我將CSS完全分離出來用jQuery附加式樣,就是為了多級染色,并且生成目錄樹和控制式樣也很容易,生成時也不需要考慮式樣。數據表建議用事先Order排序的方式,不要讀取數據的時候才分級排序,這樣性能會較佳。

我把它做成了個.Net的控件,作為輕量級的無限目錄樹,還是相當好用的。只是還不完善,我先慢慢修改,等差不多了再發布出來。

<!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">
<head id="Head1" runat="server">
    
<title>jQuery 無限級菜單</title>
    
<style type="text/css">
    #menu a 
{
        color
:#fff;
    
}
    #menu div 
{
        
/* text-align:center; */
    
}
    #menu div a 
{
        padding-left
:20px;
    
}
    #menu div.root 
{
        display
:block;
    
}
    .list 
{ background:url(list.gif) no-repeat 6px 6px; }
    
</style>
    
<script type="text/javascript" language="javascript" src="jquery-1.2.3.min.js"></script>
    
<script type="text/javascript" language="javascript">
    $(
function(){
        
//顏色列表,如果想支持無限級,最好自動生成顏色列表,不過我的配色一項很差,用生成的就更慘不忍睹了……

        _cor = ['#003366''#0066CC''#3399FF''#990000''#CC0000''#FF3300''#FF9900''#FFCC66''#FFFFFF'];
        
//初始化類

        (function Init(i,obj){
            i
++
;
            
//查找子節點

            _obj = obj.children('div');
            
//若有子節點,則增加一個專有式樣

            if (_obj.length > 0)
                obj.addClass(
'list'
);
            $.each(_obj, 
function
(j,o){
                
//若是子目錄則隱藏

                if (i > 0)
                    $(o).hide();
                
//根據目錄級數查找顏色字典上背景色,可改為圖片什么的。

                $(o).css('background-color',_cor[i]);
                
//查找子目錄

                Init(i,$(o));
            });
        })(
-1,$('#menu'
));
    });
    
//跳轉鏈接

    function GotoURL(obj) {
        
//若鏈接最末一位不是符號“#”則跳轉鏈接,因為取href得到鏈接絕對路徑,所以只能取最后一位,其實可以傳值判斷或生成目錄樹時不產生onclick都是可以的

        if (obj.href.substring(obj.href.length - 1, obj.href.length) != "#"return true;
        
//拉出和縮進的特效

        $.each($(obj).parent().children('div'), function(i,o){
            $(o).slideToggle(
'slow'
);
        });
        
return false
;
    }
    
</script>

</head>
<body>

    
<div id="menu">
        
<div> <href="#" onclick="return GotoURL(this)">第一級</a> </div>
        
<div> <href="#" onclick="return GotoURL(this)">第一級</a>
            
<div> <href="#" onclick="return GotoURL(this)">第二級</a> </div>
            
<div> <href="#" onclick="return GotoURL(this)">第二級</a>
                
<div> <href="#" onclick="return GotoURL(this)">第三級</a>
                    
<div> <href="#" onclick="return GotoURL(this)">第四級</a> </div>
                    
<div> <href="#" onclick="return GotoURL(this)">第四級</a> </div>
                
</div>
                
<div> <href="#" onclick="return GotoURL(this)">第三級</a>
                    
<div> <href="#" onclick="return GotoURL(this)">第四級</a> </div>
                    
<div> <href="#" onclick="return GotoURL(this)">第四級</a>
                        
<div> <href="#" onclick="return GotoURL(this)">第五級</a> </div>
                        
<div> <href="#" onclick="return GotoURL(this)">第五級</a> </div>
                        
<div> <href="#" onclick="return GotoURL(this)">第五級</a>
                            
<div> <href="#" onclick="return GotoURL(this)">第六級</a> </div>
                            
<div> <href="#" onclick="return GotoURL(this)">第六級</a> </div>
                        
</div>
                    
</div>
                
</div>
            
</div>
            
<div> <href="#" onclick="return GotoURL(this)">第二級</a> </div>
        
</div>
        
<div> <href="#" onclick="return GotoURL(this)">第一級</a>
            
<div> <href="#" onclick="return GotoURL(this)">第二級</a> </div>
            
<div> <href="#" onclick="return GotoURL(this)">第二級</a> </div>
            
<div> <href="#" onclick="return GotoURL(this)">第二級</a>
                
<div> <href="#" onclick="return GotoURL(this)">第三級</a>
                    
<div> <href="#" onclick="return GotoURL(this)">第四級</a> </div>
                    
<div> <href="#" onclick="return GotoURL(this)">第四級</a> </div>
                
</div>
                
<div> <href="#" onclick="return GotoURL(this)">第三級</a>
                    
<div> <href="#" onclick="return GotoURL(this)">第四級</a> </div>
                    
<div> <href="#" onclick="return GotoURL(this)">第四級</a>
                        
<div> <href="#" onclick="return GotoURL(this)">第五級</a> </div>
                        
<div> <href="#" onclick="return GotoURL(this)">第五級</a> </div>
                        
<div> <href="#" onclick="return GotoURL(this)">第五級</a>
                            
<div> <href="#" onclick="return GotoURL(this)">第六級</a> </div>
                            
<div> <href="#" onclick="return GotoURL(this)">第六級</a> </div>
                        
</div>
                    
</div>
                
</div>
            
</div>
        
</div>
    
</div>

</body>
</html>
轉自:http://blog.csdn.net/jpr1990/article/details/7245713

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!