"use strict"; function _instanceof(left, right) { if (right != null && typeof symbol !== "undefined" && right[symbol.hasinstance]) { return !!right[symbol.hasinstance](left); } else { return left instanceof right; } } function _classcallcheck(instance, constructor) { if (!_instanceof(instance, constructor)) { throw new typeerror("cannot call a class as a function"); } } function _defineproperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; object.defineproperty(target, descriptor.key, descriptor); } } function _createclass(constructor, protoprops, staticprops) { if (protoprops) _defineproperties(constructor.prototype, protoprops); if (staticprops) _defineproperties(constructor, staticprops); return constructor; } var mbnav = /*#__pure__*/ function () { function mbnav() { _classcallcheck(this, mbnav); this.id = '#mbnav'; this.listid = '#mbnavlist'; this.slistid = '#mbnavslist'; this.data = this.getdata(); } _createclass(mbnav, [{ key: "initial", value: function initial() { var that = this; // 添加元素 that.render().renderchildren(); // 打开二级菜单 $(that.listid).on('click', '.item', function () { if ($(this).hasclass('sub')) { $(that.slistid + ' .sitem').removeclass('active').eq($(this) .index()).addclass('active'); } }); // 收起二级菜单 $(that.slistid).on('click', '.tit', function () { $(that.slistid + ' .sitem').removeclass('active'); }); // 关闭菜单 $(that.id + '>.bg').on('click', function () { that.off(); }); } }, { key: "getdata", value: function getdata() { var arr = []; var $data = $(this.id + ' .data ul'); for (var i = 0; i < $data.length; i++) { var item = { title: $data.eq(i).attr('data-title'), uri: $data.eq(i).attr('data-uri') }; var sub = []; for (var j = 0; j < $data.eq(i).find('li').length; j++) { var sitem = { title: $data.eq(i).find('li').eq(j).attr('data-title'), uri: $data.eq(i).find('li').eq(j).attr('data-uri') }; sub.push(sitem); } item.sub = sub; arr.push(item); } $data.parent().remove(); return arr; } }, { key: "render", value: function render() { var str = ""; this.data.foreach(function (item) { if (array.isarray(item.sub) && item.sub.length > 0) { str += "".concat( item.title, ""); } else { str += "") .concat(item.title, ""); } }); $(this.listid).html(str); return this; } }, { key: "renderchildren", value: function renderchildren() { var str = ""; this.data.foreach(function (item) { if (array.isarray(item.sub) && item.sub.length > 0) { var s_str = ""; item.sub.foreach(function (sitem) { s_str += "").concat(sitem.title, ""); }); str += "
".concat(item .title, "
").concat(s_str, "
"); } else { str += "
"; } }); $(this.slistid).html(str); return this; } }, { key: "open", value: function open() { // 打开菜单 $('body').removeclass('mbnavoff').addclass('mbnavopen'); } }, { key: "off", value: function off() { // 关闭菜单 $('body').removeclass('mbnavopen').addclass('mbnavoff'); } }]); return mbnav; }(); var mbnav = new mbnav(); mbnav.initial(); $('.j-mbnav-open').click(function () { mbnav.open(); });