—————————————————————————————————————————————————————————

使用方法

实现效果

引入文件

<link rel="stylesheet" href="vendor/bootstrap/css/bootstrap.css"> // bootstrap样式

<link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"> // 图标使用font-awesome

<link rel="stylesheet" href="vendor/metisMenu/metisMenu.css"> // 插件样式

<script src="vendor/jquery/jquery.js"></script> // 基于jquery实现

<script src="vendor/bootstrap/js/bootstrap.min.js"></script> // 使用bootstrap的collapse插件

<script src="vendor/metisMenu/metisMenu.min.js"></script> // 插件代码

调用方法

<script type="text/javascript">
$(function() {$('#side-menu').metisMenu(); // ul.nav#side-menu
})
</script>

使用样例

<<Demo.html>>

<nav class="navbar navbar-default navbar-static-top"><div class="navbar-default sidebar" role="navigation"><div class="sidebar-nav navbar-collapse"><ul class="nav" id="side-menu"><li class="sidebar-search"><div class="input-group custom-search-form"><input type="text" class="form-control" placeholder="Search..."><span class="input-group-btn"><button class="btn btn-default" type="button"><i class="fa fa-search"></i></button></span></div></li><li><a href=""><i class="fa fa-dashboard fa-fw"></i> Dashboard</a></li><li><a href="#"><i class="fa fa-bar-chart-o fa-fw"></i> Charts<spanclass="fa arrow"></span></a><ul class="nav nav-second-level"><li><a href="">Flot Charts</a></li><li><a href="">Morris.js Charts</a></li></ul></li><li><a href=""><i class="fa fa-table fa-fw"></i> Tables</a></li><li><a href=""><i class="fa fa-edit fa-fw"></i> Forms</a></li><li><a href="#"><i class="fa fa-wrench fa-fw"></i> UI Elements<spanclass="fa arrow"></span></a><ul class="nav nav-second-level"><li><a href="">Panels and Wells</a></li><li><a href="">Buttons</a></li><li><a href="">Notifications</a></li><li><a href="">Typography</a></li><li><a href=""> Icons</a></li><li><a href="">Grid</a></li></ul></li><li><a href="#"><i class="fa fa-sitemap fa-fw"></i> Multi-Level Dropdown<spanclass="fa arrow"></span></a><ul class="nav nav-second-level"><li><a href="#">Second Level Item</a></li><li><a href="#">Second Level Item</a></li><li><a href="#">Third Level <span class="fa arrow"></span></a><ul class="nav nav-third-level"><li><a href="#">Third Level Item</a></li><li><a href="#">Third Level Item</a></li><li><a href="#">Third Level Item</a></li><li><a href="#">Third Level Item</a></li></ul></li></ul></li><li class="active"><a href="#"><i class="fa fa-files-o fa-fw"></i> Sample Pages<spanclass="fa arrow"></span></a><ul class="nav nav-second-level"><li><a class="active" href="#">Blank Page</a></li><li><a href="#">Login Page</a></li></ul></li></ul></div></div>
</nav>

<<blank.css>>

@media (min-width: 768px) {.sidebar {position: absolute;width: 250px;margin-top: 51px;z-index: 2;}
}
.sidebar .sidebar-search {padding: 15px;}
.sidebar ul li a.active {background-color: #eee;}
.sidebar ul li {background: #f8f8f8;border-bottom: #e7e7e7 1px solid;}
.nav-second-level li {border-bottom: 0 !important;}
.nav-second-level li a {padding-left: 38px;}

树形图

p.s.自定义样式用下划线表示

—————————————————————————————————————————————————————————

源码解读

调用

使用$.fn.extend(object)的方式为jquery类添加成员方法,功能封装在原型中,能够全局调用;

通过$("#side-menu")生成的jquery类实例来调用该方法;

初始配置

全局定义插件名称pluginName,定义对象defaults存放toggle(展开/闭合)与doubleTapToGo(双击事件)的开关;

样式

使用font-awesome来扩展侧边栏图标和展开标识(三角),引入font-awesome.css后只需要通过i.fa.fa-***来调用就可以了

基础布局通过bootstrap类实现,详细分支情况见属性图

初始化展开

借助bootstrap内置的collapse插件来实现;

通过检测<li.active>来标识当前页对应的列表,在metisMenu.js中检查是否需要展开的列表;

判断激活的标签栏是否有子列表,如果有则添加.collapse.in类(bootstrap.css),实际为添加样式{display:block};

未被激活的标签栏如果有子列表则添加.collapse类(boostrap.css),{display:none};

监听点击事件

监听方法借助bootstrap的collapse插件;

绑定事件时需要遵循collapse插件规则.on("click"+"."+pluginName,function(e){});

为点击的标签栏添加激活样式.active,如果有子列表则展开/关闭,使用collapse插件方法.collapse("toggle");

闭合其他所有有子列表的标签栏,使用.collapse("hide");

—————————————————————————————————————————————————————————

简单版 v1.0

功能

仅实现基础效果

复写代码

<<metisMenu-demo1.js>>

/*** Created by hughd on 2017/8/9.* 简单版 - v1.0* 仅实现基础折叠*/
;(function ($, window, document, undefined) {var pluginName = "metisMenu";var toggle = true;function Plugin(element, options) {this.element = $(element);this.init();}Plugin.prototype = {init: function () {var $this = this.element;// var obj = this;$this.find("li.active").has("ul").children("ul").addClass('collapse in');// 初始激活的元素的后代全部展开$this.find('li').not(".active").has("ul").children("ul").addClass('collapse');// 未激活的元素显示隐藏// 监听点击事件$this.find("li").children("a").on("click." + pluginName, function () {//被点击的元素激活样式并展开$(this).parent('li').toggleClass('active').children("ul").collapse("toggle");if (toggle) {//将其他被展开的元素闭合$(this).parent("li").siblings().removeClass("active").children("ul").collapse("hide");}});}}$.fn[pluginName] = function (options) {// 实例化Plugin对象,并存放在元素<ul#side-menu>._proto_.metisMenu中this.data(pluginName, new Plugin(this, options));return this;}
})(jQuery, window, document);

实现思路

  • 初始化

    获取当前激活的标签,将该标签的内容显示(如果没有子标签则不显示)

    未激活的标签,所有子列表隐藏

  • 点击事件(通过boostrap的collapse插件)

    当前点击的标签获取激活样式,子列表展开

    除点击标签之外的其他标签移除激活样式,并闭合

—————————————————————————————————————————————————————————

完整插件

功能

IE浏览器兼容情况;

移除已有metisMenu数据情况

双击标题事件

源码

<<metisMenu.js>>

/** metismenu - v1.1.3* Easy menu jQuery plugin for Twitter Bootstrap 3* https://github.com/onokumus/metisMenu** Made by Osman Nuri Okumus* Under MIT License*/
;(function($, window, document, undefined) {var pluginName = "metisMenu",defaults = {toggle: true,doubleTapToGo: false};function Plugin(element, options) {this.element = $(element);this.settings = $.extend({}, defaults, options);this._defaults = defaults;this._name = pluginName;this.init();}Plugin.prototype = {init: function() {var $this = this.element,$toggle = this.settings.toggle,obj = this;if (this.isIE() <= 9) {$this.find("li.active").has("ul").children("ul").collapse("show");$this.find("li").not(".active").has("ul").children("ul").collapse("hide");} else {$this.find("li.active").has("ul").children("ul").addClass("collapse in");$this.find("li").not(".active").has("ul").children("ul").addClass("collapse");}//add the "doubleTapToGo" class to active items if neededif (obj.settings.doubleTapToGo) {$this.find("li.active").has("ul").children("a").addClass("doubleTapToGo");}$this.find("li").has("ul").children("a").on("click" + "." + pluginName, function(e) {e.preventDefault();//Do we need to enable the double tapif (obj.settings.doubleTapToGo) {//if we hit a second time on the link and the href is valid, navigate to that urlif (obj.doubleTapToGo($(this)) && $(this).attr("href") !== "#" && $(this).attr("href") !== "") {e.stopPropagation();document.location = $(this).attr("href");return;}}$(this).parent("li").toggleClass("active").children("ul").collapse("toggle");if ($toggle) {$(this).parent("li").siblings().removeClass("active").children("ul.in").collapse("hide");}});},isIE: function() { //https://gist.github.com/padolsey/527683var undef,v = 3,div = document.createElement("div"),all = div.getElementsByTagName("i");while (div.innerHTML = "<!--[if gt IE " + (++v) + "]><i></i><![endif]-->",all[0]) {return v > 4 ? v : undef;}},//Enable the link on the second click.doubleTapToGo: function(elem) {var $this = this.element;//if the class "doubleTapToGo" exists, remove it and returnif (elem.hasClass("doubleTapToGo")) {elem.removeClass("doubleTapToGo");return true;}//does not exists, add a new class and return falseif (elem.parent().children("ul").length) {//first remove all other class$this.find(".doubleTapToGo").removeClass("doubleTapToGo");//add the class on the current elementelem.addClass("doubleTapToGo");return false;}},remove: function() {this.element.off("." + pluginName);this.element.removeData(pluginName);}};$.fn[pluginName] = function(options) {this.each(function () {var el = $(this);if (el.data(pluginName)) {el.data(pluginName).remove();}el.data(pluginName, new Plugin(this, options));});return this;};})(jQuery, window, document);

<<metisMenu.css>>

/** metismenu - v1.1.3* Easy menu jQuery plugin for Twitter Bootstrap 3* https://github.com/onokumus/metisMenu** Made by Osman Nuri Okumus* Under MIT License*/
.arrow {float: right;line-height: 1.42857;
}.glyphicon.arrow:before {content: "\e079";
}.active > a > .glyphicon.arrow:before {content: "\e114";
}/** Require Font-Awesome* http://fortawesome.github.io/Font-Awesome/
*/.fa.arrow:before {content: "\f104";
}.active > a > .fa.arrow:before {content: "\f107";
}.plus-times {float: right;
}.fa.plus-times:before {content: "\f067";
}.active > a > .fa.plus-times {filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);-ms-transform: rotate(45deg);-o-transform: rotate(45deg);transform: rotate(45deg);
}.plus-minus {float: right;
}.fa.plus-minus:before {content: "\f067";
}.active > a > .fa.plus-minus:before {content: "\f068";
}

转载于:https://www.cnblogs.com/hughdong/p/7337863.html

读源码 | metisMenu侧边栏插件相关推荐

  1. 微信读书vscode插件_跟我一起读源码 – 如何阅读开源代码

    阅读是最好的老师 在学习和提升编程技术的时候,通过阅读高质量的源码,来学习专家写的高质量的代码,是一种非常有效的提升自我的方式.程序员群体是一群乐于分享的群体,因此在互联网上有大量的高质量开源项目,阅 ...

  2. 带着问题读源码-soul(2021-01-16)

    ### 带着问题读源码系列之Dubbo插件 像往常一样启动 [soul-admin] 和 [soul-bootstrap] . 因为dubbo需要依赖zookeeper, 需要需要启动一个监听在 lo ...

  3. 带着问题读源码-soul(2021-01-15)

    带着问题读源码系列-soul的本地服务筛选 在上一期中,了解到soul的http请求是通过dividePlugin插件完成对本地服务的筛选. 总体来说,可以分为两步: 1. 选出符合调用要求的服务列表 ...

  4. 我是怎么读源码的,授之以渔

    点击上方"视学算法",选择"设为星标" 做积极的人,而不是积极废人 作者 :youzhibing 链接 :https://www.cnblogs.com/you ...

  5. 这样读源码,不牛X也难

    程序员在工作过程中,会遇到很多需要阅读源码的场景,比如技术预研.选择技术框架.接手以前的项目.review他人的代码.维护老产品等等.可以说,阅读源代码是程序员的基本功,这项基本功是否扎实,会在很大程 ...

  6. myisam怎么读_耗时半年,我成功“逆袭”,拿下美团offer(刷面试题+读源码+项目准备)...

    欢迎关注专栏[以架构赢天下]--每天持续分享Java相关知识点 以架构赢天下​zhuanlan.zhihu.com 以架构赢天下--持续分享Java相关知识点 每篇文章首发此专栏 欢迎各路Java程序 ...

  7. 读源码,对程序员重要吗?

    来源: CSDN(ID:CSDNnews) 嘿,朋友们!本文我将分享一些关于主动阅读和研究源码的一些想法.在我看来,阅读源码能够帮你成为一名更专业的开发人员.毫无疑问的是,阅读源码提高了我的软件开发水 ...

  8. 夜读源码,带你探究 Go 语言的iota

    Go 语言的 iota 怎么说呢,感觉像枚举,又有点不像枚举,它的底层是什么样的,用哪个姿势使用才算正规,今天转载一篇「Go夜读」社区上分享的文章,咱们一起学习下.Go 夜读,带你每页读源码~!  这 ...

  9. 【一起读源码】1. Java 中元组 Tuple

    1.1 问题描述 使用 Java 做数据分析.机器学习的时候,常常需要对批量的数据进行处理,如果需要处理的数据的维度不超过10时,可以考虑使用 org.javatuples 提供的 Tuple 类工具 ...

最新文章

  1. 机器学习获量子加速!物理学家与计算科学家「自然联姻」
  2. Android Input系统之触摸屏
  3. 《Lua程序设计》第6章 深入函数 学习笔记
  4. HTML常见小问题2
  5. abstractset java_AbstractSet源码分析-java8
  6. Git操作之克隆某一个特定的远程分支
  7. PCM复用设备主要传输什么业务?
  8. SpringBoot整合themeleaf+bootstrap (五)
  9. intelRealsense D435 python开发环境搭建
  10. 制作可以 SSH 登录的 Docker 镜像
  11. HubbleDotNet 基本语法
  12. oracle 归档模式 ASM,oracle rac启用归档模式
  13. 去掉CAD打印时出现由AUTODESK教育版产品制作戳记的方法
  14. 一例智能网卡(mellanox)的网卡故障分析
  15. 最新版 银图/网银/MOMO模拟按键/Photoshop图像处理
  16. 扩容卡检测(win下,能作为参考)
  17. 安装win7纯净版系统时,提示缺少所需的CD/DVD驱动器设备驱动程序的解决方案,亲测有效
  18. 知识蒸馏(作者 HINTON ,JEFF DEAN)
  19. 图难于其易,为大于其细。天下难事,必作于易;天下大事,必作于细--《道德经》...
  20. 1016: 银行利率 C语言

热门文章

  1. Java三大结构 顺序结构、选择结构、循环结构
  2. 修改手机IP地址方法
  3. asp.net MVC 5 及 Web API修改日期格式
  4. springboot集成redis集群实现集群拓扑动态刷新
  5. python学生信息管理系统(GUI界面+mysql数据库)
  6. Python 控制舵机
  7. MySQL的运行原理
  8. 【转】程序员的职业生涯该如何过——前锤子科技研发总监池建强
  9. 在Ubuntu18.04.2LTS上安装视频播放器smplayer/vlc
  10. HNUST 1695: 跳格子(简单模拟)