ITPub博客

首页 > 应用开发 > IT综合 > 垂直折叠展开,点击展开、收缩的jQ多级菜单

垂直折叠展开,点击展开、收缩的jQ多级菜单

IT综合 作者:chunzhisu 时间:2014-07-05 15:58:59 0 删除 编辑

点击(此处)折叠或打开

  1. <!doctype html>
  2. <html lang=\"en\">
  3. <head>
  4. <meta charset=\"UTF-8\">
  5. <title>菜单垂直展开折叠</title>
  6.  <style type=\"text/css\">
  7. .wrap-menu {width:778px; margin:0 auto; overflow:auto; width:300px; background:#F6F6F6; font:12px/1.5 Tahoma,Arial,sans-serif}
  8. .wrap-menu ul{ list-style:none; margin:0; padding:0;}
  9. .wrap-menu ul li{ text-indent:3em; white-space:nowrap; }
  10. .wrap-menu ul li h2{ cursor:pointer; height:100%; width:100%; margin:0 0 1px 0; font:12px/31px \'宋体\'; color:#fff; background:red;}
  11. .wrap-menu ul li a{ display:block; outline:none; height:25px; line-height:25px; margin:1px 0; color:#1A385C; text-decoration:none;}
  12. .wrap-menu ul li img{ margin-right:10px; margin-left:-17px; margin-top:9px; width:7px; height:7px; background:url(/jscss/demoimg/201402/arrow.gif) no-repeat; border:none;}
  13. .wrap-menu ul li img.unfold{ background-position:0 -9px;}
  14. .wrap-menu ul li a:hover{ background-color:#ccc; background-image:none;}
  15. </style>
  16. <script src=\"/ajaxjs/jquery-1.6.2.min.js\"></script>
  17. <script>
  18. function AccordionMenu(options) {
  19.     this.config = {
  20.         containerCls : \'.wrap-menu\',// 外层容器
  21.         menuArrs: \'\',// JSON传进来的数据
  22.         type: \'click\', // 默认为click 也可以mouseover
  23.         renderCallBack: null, // 渲染html结构后回调
  24.         clickItemCallBack: null // 每点击某一项时候回调
  25.     };
  26.     this.cache = {
  27.     };
  28.     this.init(options);
  29.  }
  30.  AccordionMenu.prototype = {
  31.     constructor: AccordionMenu,
  32.     init: function(options){
  33.         this.config = $.extend(this.config,options || {});
  34.         var self = this,
  35.             _config = self.config,
  36.             _cache = self.cache;
  37.         // 渲染html结构
  38.         $(_config.containerCls).each(function(index,item){
  39.             self._renderHTML(item);
  40.             // 处理点击事件
  41.             self._bindEnv(item);
  42.         });
  43.     },
  44.     _renderHTML: function(container){
  45.         var self = this,
  46.             _config = self.config,
  47.             _cache = self.cache;
  48.         var ulhtml = $(\'
      \'
      );
    •         $(_config.menuArrs).each(function(index,item){
    •             var lihtml = $(\'
    • \'+item.name+\'

    • \');
    •             if(item.submenu && item.submenu.length > 0) {
    •                 self._createSubMenu(item.submenu,lihtml);
    •             }
    •             $(ulhtml).append(lihtml);
    •         });
    •         $(container).append(ulhtml);
    •         _config.renderCallBack && $.isFunction(_config.renderCallBack) && _config.renderCallBack();
    •         // 处理层级缩进
    •         self._levelIndent(ulhtml);
    •     },
    •     /**
    •      * 创建子菜单
    •      * @param {array} 子菜单
    •      * @param {lihtml} li项
    •      */
    •     _createSubMenu: function(submenu,lihtml){
    •         var self = this,
    •             _config = self.config,
    •             _cache = self.cache;
    •         var subUl = $(\'
        \'
        ),
      •             callee = arguments.callee,
      •             subLi;
      •         $(submenu).each(function(index,item){
      •             var url = item.url || \'javascript:void(0)\';
      •             subLi = $(\'
      • +url+\'\">\'+item.name+\'
      • \');
      •             if(item.submenu && item.submenu.length > 0) {
      •                 $(subLi).children(\'a\').prepend(\'\"\"/\');
      •                 callee(item.submenu, subLi);
      •             }
      •             $(subUl).append(subLi);
      •         });
      •         $(lihtml).append(subUl);
      •     },
      •     /**
      •      * 处理层级缩进
      •      */
      •     _levelIndent: function(ulList){
      •         var self = this,
      •             _config = self.config,
      •             _cache = self.cache,
      •             callee = arguments.callee;
      •         var initTextIndent = 2,
      •             lev = 1,
      •             $oUl = $(ulList);
      •         while($oUl.find(\'ul\').length > 0){
      •             initTextIndent = parseInt(initTextIndent,10) + 2 + \'em\';
      •             $oUl.children().children(\'ul\').addClass(\'lev-\' + lev)
      •                         .children(\'li\').css(\'text-indent\', initTextIndent);
      •             $oUl = $oUl.children().children(\'ul\');
      •             lev++;
      •         }
      •         $(ulList).find(\'ul\').hide();
      •         $(ulList).find(\'ul:first\').show();
      •     },
      •     /**
      •      * 绑定事件
      •      */
      •     _bindEnv: function(container) {
      •         var self = this,
      •             _config = self.config;
      •         $(\'h2,a\',container).unbind(_config.type);
      •         $(\'h2,a\',container).bind(_config.type,function(e){
      •             if($(this).siblings(\'ul\').length > 0) {
      •                 $(this).siblings(\'ul\').slideToggle(\'slow\').end().children(\'img\').toggleClass(\'unfold\');
      •             }
      •             $(this).parent(\'li\').siblings().find(\'ul\').hide()
      •                  .end().find(\'img.unfold\').removeClass(\'unfold\');
      •             _config.clickItemCallBack && $.isFunction(_config.clickItemCallBack) && _config.clickItemCallBack($(this));
      •         });
      •     }
      •  };
      • </script>
      • </head>
      • <body>
      • <div class=\"wrap-menu\">
      • </div>
      • <script>
      •     var testMenu=[
      •    {
      •        \"name\": \"一级菜单\",
      •        \"submenu\": [
      •            {
      •                \"name\": \"二级菜单\",
      •                \"url\": \"\"
      •            },
      •            {
      •                \"name\": \"二级菜单\",
      •                \"url\": \"\"
      •            }
      •        ]
      •    },
      •    {
      •        \"name\": \"一级菜单\",
      •        \"submenu\": [
      •            {
      •                \"name\": \"二级菜单\",
      •                \"url\": \"\"
      •            },
      •            {
      •                \"name\": \"二级菜单\",
      •                \"submenu\": [
      •                    {
      •                        \"name\": \"三级菜单\",
      •                        \"submenu\": [
      •                            {
      •                                \"name\": \"四级菜单\",
      •                                \"url\": \"\"
      •                            }
      •                        ]
      •                    },
      •                    {
      •                        \"name\": \"三级菜单\",
      •                        \"url\": \"\"
      •                    }
      •                ]
      •            },
      •            {
      •                \"name\": \"二级菜单\",
      •                \"url\": \"\"
      •            },
      •            {
      •                \"name\": \"二级菜单\",
      •                \"submenu\": [
      •                    {
      •                        \"name\": \"三级菜单\",
      •                        \"submenu\": [
      •                            {
      •                                \"name\": \"四级菜单\",
      •                                \"url\": \"\"
      •                            },
      •                            {
      •                                \"name\": \"四级菜单\",
      •                                \"submenu\": [
      •                                    {
      •                                        \"name\": \"五级菜单\",
      •                                        \"url\": \"\"
      •                                    },
      •                                    {
      •                                        \"name\": \"五级菜单\",
      •                                        \"url\": \"\"
      •                                    }
      •                                ]
      •                            }
      •                        ]
      •                    },
      •                    {
      •                        \"name\": \"三级菜单\",
      •                        \"url\": \"\"
      •                    }
      •                ]
      •            },
      •            {
      •                \"name\": \"二级菜单\",
      •                \"url\": \"\"
      •            }
      •        ]
      •    },
      •    {
      •        \"name\": \"一级菜单\",
      •        \"submenu\": [
      •            {
      •                \"name\": \"二级菜单\",
      •                \"url\": \"\"
      •            },
      •            {
      •                \"name\": \"二级菜单\",
      •                \"url\": \"\"
      •            },
      •            {
      •                \"name\": \"二级菜单\",
      •                \"url\": \"\"
      •            }
      •        ]
      •    }
      • ];
      •     $(function(){
      •         new AccordionMenu({menuArrs:testMenu});
      •     });
      •     </script>
      • <div style=\"text-align:center;clear:both\">
      • </div>
      • </body>
      • </html>

      来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29737856/viewspace-1210357/ ,如需转载,请注明出处,否则将追究法律责任。

      请登录后发表评论 登录
      全部评论

      注册时间:2014-07-04

      • 博文量
        95
      • 访问量
        122213