zoukankan      html  css  js  c++  java
  • jquery树形菜单完整代码

    本实例实现了树形的动态菜单,兼容IE8,火狐,Chrome等浏览器。使用了jQuery的toggle() 方法。效果和代码如下:

    http://keleyi.com/keleyi/phtml/jqmenu/6.htm

      1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      2 <html xmlns="http://www.w3.org/1999/xhtml">
      3 <head>
      4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      5 <title>jquery的树形菜单代码 - 计划 - 博客园 </title>
      6 <meta name="keywords" content="www.cnblogs.com/jihua"/>
      7 <style type="text/css">
      8 body { font-family:"宋体"; font-size: 12px; line-height: 1.5em; color:#7FB0C8; padding:0; margin:0; background: #336699;}
      9 ul,ol,li,dl,dt,dd { margin:0; padding:0; list-style-type:none;}
     10 h1,h2,h3,form,input,iframe,span { margin:0; padding:0;} 
     11 a { color:#7FB0C8;}
     12 a:link {color: #7FB0C8; TEXT-DECORATION: none;}
     13 a:visited {color: #7FB0C8; TEXT-DECORATION: none;}
     14 a:hover {color: #fff; TEXT-DECORATION: none;}
     15 .white { color:#fff;}
     16 .white a:link {color: #fff; TEXT-DECORATION: none;}
     17 .white a:visited {color: #fff; TEXT-DECORATION: none;}
     18 .white a:hover {color: #73E1F5; TEXT-DECORATION: none;}
     19 /* 树形菜单开始 */
     20 .close { float:right; clear:right; font-size:12px; font-weight:normal; cursor:pointer; padding-right:10px;}
     21 .title { font-size:14px; color:#fff; margin-bottom:10px; padding-left:5px; width:290px;}
     22 .menu { width:290px; height:330px; margin-bottom:10px;}
     23 
     24 .l1 { background:#000; font-size:13px; padding:5px 0 0 30px; height:20px; margin-bottom:5px; cursor:pointer;}
     25 .slist { margin:0 0 5px 0; display:none;}
     26 .l2 { padding:0 0 0 35px; font-size:13px;}
     27 .l2 a { padding:6px 0 0 5px; width:230px; height:21px; display:block;} 
     28 .currentl2 a,.l2 a:hover { background:#1E5A82; color:#fff;}
     29 .sslist { background:#156890; width:235px; overflow:hidden; margin:0 0 5px 35px; display:none;}
     30 .l3 a { padding:6px 0 0 5px; width:230px; height:20px; display:block;} 
     31 .currentl3 a,.l3 a:hover { color:#fff; font-weight:bold;}
     32 </style>
     33 <script type="text/javascript" src="http://keleyi.com/keleyi/pmedia/jquery/jquery-1.4.2.min.js"></script>
     34 <script type="text/javascript">
     35     // 树状菜单
     36     $(document).ready(function () {
     37         $(".l1").toggle(function () {
     38             $(".slist").animate({ height: 'toggle', opacity: 'hide' }, "slow");
     39             $(this).next(".slist").animate({ height: 'toggle', opacity: 'toggle' }, "slow");
     40         }, function () {
     41             $(".slist").animate({ height: 'toggle', opacity: 'hide' }, "slow");
     42             $(this).next(".slist").animate({ height: 'toggle', opacity: 'toggle' }, "slow");
     43         });
     44 
     45         $(".l2").toggle(function () {
     46             $(this).next(".sslist").animate({ height: 'toggle', opacity: 'toggle' }, "slow");
     47         }, function () {
     48             $(this).next(".sslist").animate({ height: 'toggle', opacity: 'toggle' }, "slow");
     49         });
     50 
     51         $(".l2").click(function () {
     52             $(".l3").removeClass("currentl3");
     53             $(".l2").removeClass("currentl2");
     54             $(this).addClass("currentl2");
     55         });
     56 
     57         $(".l3").click(function () {
     58             $(".l3").removeClass("currentl3");
     59             $(this).addClass("currentl3");
     60         });
     61 
     62         $(".close").toggle(function () {
     63             $(".slist").animate({ height: 'toggle', opacity: 'show' }, "fast");
     64             $(".sslist").animate({ height: 'toggle', opacity: 'show' }, "fast");
     65         }, function () {
     66             $(".slist").animate({ height: 'toggle', opacity: 'hide' }, "fast");
     67             $(".sslist").animate({ height: 'toggle', opacity: 'hide' }, "fast");
     68         });
     69     });
     70 </script>
     71 </head>
     72 <body>
     73 <h1 class="title"><span class="close">全部收起/展开</span>Jihua树形菜单</h1>
     74 <div class="menu">
     75   <h1 class="l1">一级菜单</h1>
     76   <div class="slist">
     77     <h2 class="l2"><a href="#">二级菜单</a></h2>
     78     <ul class="sslist">
     79       <li class="l3"><a href="#">·三级菜单</a></li>
     80       <li class="l3"><a href="#">·三级菜单</a></li>
     81       <li class="l3"><a href="http://jihua.cnblogs.com" target="_blank">·jihua.cnblogs.com</a></li>
     82       <li class="l3"><a href="#">·三级菜单</a></li>
     83     </ul>
     84     <h2 class="l2"><a href="#">二级菜单</a></h2>
     85     <ul class="sslist">
     86       <li class="l3"><a href="#">·三级菜单</a></li>
     87       <li class="l3"><a href="#">·三级菜单</a></li>
     88       <li class="l3"><a href="http://jihua.cnblogs.com" target="_blank">·三级菜单</a></li>
     89       <li class="l3"><a href="#">·三级菜单</a></li>
     90     </ul>
     91     <h2 class="l2"><a href="#">二级www.cnblogs.com/jihua</a></h2>
     92   </div>
     93   <h1 class="l1">一级博客园</h1>
     94   <div class="slist">
     95     <h2 class="l2"><a href="#">二级菜单计划</a></h2>
     96     <h2 class="l2"><a href="#">二级菜单</a></h2>
     97     <h2 class="l2"><a href="#">二级菜单</a></h2>
     98   </div>
     99   <h1 class="l1">一级菜单</h1>
    100   <div class="slist">
    101     <h2 class="l2"><a href="#">二级菜单</a></h2>
    102     <h2 class="l2"><a href="#">二级菜单</a></h2>
    103     <h2 class="l2"><a href="#">二级菜单</a></h2>
    104   </div>
    105 </div>
    106 </body>
    107 </html>

    本实例用到jquery的toggle() 方法,介绍如下:

    定义和用法

    toggle() 方法切换元素的可见状态。

    如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素。

    语法

    $(selector).toggle(speed,callback,switch)
    参数描述
    speed

    可选。规定元素从可见到隐藏的速度(或者相反)。默认为 "0"。

    可能的值:

    • 毫秒 (比如 1500)
    • "slow"
    • "normal"
    • "fast"

    在设置速度的情况下,元素从可见到隐藏的过程中,会逐渐地改变其高度、宽度、外边距、内边距和透明度。

    如果设置此参数,则无法使用 switch 参数。

    callback

    可选。toggle 函数执行完之后,要执行的函数。

    如需学习更多有关 callback 的内容,请访问我们的 jQuery Callback 这一章。

    除非设置了 speed 参数,否则不能设置该参数。

    switch

    可选。布尔值。规定 toggle 是否隐藏或显示所有被选元素。

    • True - 显示所有元素
    • False - 隐藏所有元素

    如果设置此参数,则无法使用 speed 和 callback 参数。

    提示和注释

    注释:该效果适用于通过 jQuery 隐藏的元素,或在 CSS 中声明 display:none 的元素(但不适用于 visibility:hidden 的元素)。

     

     

  • 相关阅读:
    first root
    C r and n(组合数)
    学习笔记
    zabbix历史数据全部清楚
    Jetson TX2安装固态硬盘(原创)
    Jetson TX2安装tensorflow(原创)
    机器视觉编程作业02(01)(原创)
    机器视觉编程作业02(00)EM算法
    机器视觉 编程作业题 第一题(01)(原创)
    动态代理
  • 原文地址:https://www.cnblogs.com/jihua/p/jquerycaidan.html
Copyright © 2011-2022 走看看