JS實(shí)現(xiàn)左側(cè)菜單工具欄
本文實(shí)例為大家分享了JS實(shí)現(xiàn)左側(cè)菜單工具欄的具體代碼,供大家參考,具體內(nèi)容如下
摘要
該js腳本可幫助你快速實(shí)現(xiàn)左側(cè)菜單工具欄。通過js封裝成一個(gè)方法類,直接new該對(duì)象即可快速生成左側(cè)菜單工具欄。
一、效果展示
二、menu.js文件
(1)WenMenuNode節(jié)點(diǎn)
let WenMenuNode = function ({ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? wenMenu, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes = {}, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? subs = [], ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? parentElement = null, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? iconHTML = '', ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? level = 1, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? parentNode = null, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? isActive = false, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? onLaunch = null, ? ? ? ? ? ? ? ? ? ? ? ? ? ? }) { ? ? this._level = level; ? ? this._text = text; ? ? this._attributes = attributes; ? ? this._wenMenu = wenMenu; ? ? this._subs = subs; ? ? this._onLaunch = onLaunch; ? ? this._childHeight = 0; ? ? this._height = 0; ? ? this.style = { ? ? ? ? childHeight: 0, ? ? } ? ? ? this._parentElement = parentElement; ? ? this._parentNode = parentNode; ? ? this._element = this._wenMenu.createElement('li', { ? ? ? ? class: "wen-menu-li", ? ? }); ? ? this._textElement = this._wenMenu.createElement('a', this._attributes); ? ? this._iconHTML = iconHTML; ? ? this._childNodes = []; ? ? this._childElement = null; ? ? this._activeChild = null; ? ? if (this._parentElement) this._parentElement.append(this._element); ? ? this._isActive = isActive; ? ? if (this._isActive) { ? ? ? ? if (this._level == 1) { ? ? ? ? ? ? this._wenMenu._activeMenu = this; ? ? ? ? } else if (this._parentNode) { ? ? ? ? ? ? this._parentNode._activeChild = this; ? ? ? ? } ? ? } ? ? this.create().onLaunch(); } ? ? WenMenuNode.prototype.create = function () { ? ? let a = this._textElement; ? ? let icon = this._wenMenu.createElement('i', { ? ? ? ? class: "wen-menu-icon", ? ? }) ? ? if (this._level > 1) { ? ? ? ? a.innerHTML = '<span class="wen-menu-tree">--</span>'; ? ? } ? ? icon.innerHTML = this._iconHTML; ? ? a.append(icon); ? ? a.innerHTML += `<span class="wen-menu-text">${this._text}</span>`; ? ? if (this._level == 1) { ? ? ? ? a.classList.add('wen-menu-first'); ? ? } ? ? this._element.append(a); ? ? if (this._subs.length) { ? ? ? ? let ul = this._wenMenu.createElement('ul', { ? ? ? ? ? ? class: "wen-menu-ul" + (this._level == 1 ? " wen-menu-ul-second" : ""), ? ? ? ? }); ? ? ? ? this._element.append(ul); ? ? ? ? ? this._childElement = ul; ? ? ? ? this._subs.forEach((item, i) => { ? ? ? ? ? ? let node = new WenMenuNode({ ? ? ? ? ? ? ? ? text: item.text, ? ? ? ? ? ? ? ? wenMenu: this._wenMenu, ? ? ? ? ? ? ? ? attributes: item.attributes, ? ? ? ? ? ? ? ? subs: item.subs, ? ? ? ? ? ? ? ? parentElement: ul, ? ? ? ? ? ? ? ? iconHTML: item.iconHTML, ? ? ? ? ? ? ? ? level: this._level + 1, ? ? ? ? ? ? ? ? parentNode: this, ? ? ? ? ? ? ? ? isActive: this._isActive && i == 0, ? ? ? ? ? ? ? ? onLaunch: (childNode) => { ? ? ? ? ? ? ? ? ? ? this._childNodes.push(childNode); ? ? ? ? ? ? ? ? ? ? if (i == this._subs.length - 1) { ? ? ? ? ? ? ? ? ? ? ? ? this.setEventListener(true); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }); ? ? ? ? }); ? ? } else { ? ? ? ? this.setEventListener(false); ? ? } ? ? return this; } ? ? WenMenuNode.prototype.onLaunch = function () { ? ? if (this._onLaunch) { ? ? ? ? this._onLaunch.call(this._parentNode, this); ? ? } ? ? return this; } ? WenMenuNode.prototype.setEventListener = function (hasSub = false) { ? ? if (hasSub) { ? ? ? ? this._height = this._subs.length * this._wenMenu._menuHeight; ? ? ? ? this._childHeight = this._childElement.clientHeight; ? ? ? ? if (this._isActive) { ? ? ? ? ? ? this._textElement.setAttribute('wen-active', ''); ? ? ? ? ? ? this._textElement.setAttribute('wen-expand', ''); ? ? ? ? ? ? this.style.childHeight = this._childHeight + this._wenMenu._menuSpacing; ? ? ? ? } else { ? ? ? ? ? ? this._textElement.setAttribute('wen-icon', '') ? ? ? ? ? ? this._textElement.setAttribute('wen-collapse', ''); ? ? ? ? ? ? this.style.childHeight = 0; ? ? ? ? } ? ? ? ? this._childElement.style.height = this.style.childHeight + "px"; ? ? ? ? this._textElement.addEventListener('click', (e) => { ? ? ? ? ? ? if (this._wenMenu._autoCollapse) { ? ? ? ? ? ? ? ? this.resetHeight(); ? ? ? ? ? ? ? ? this.setHeight({ ? ? ? ? ? ? ? ? ? ? menuNode: this, ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? let height = 0, target = e.target; ? ? ? ? ? ? ? ? if (target.classList.value.indexOf('wen-menu-text') >= 0) { ? ? ? ? ? ? ? ? ? ? target = target.parentElement; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (target.getAttribute('wen-expand') === null) { ? ? ? ? ? ? ? ? ? ? // todo:: 展開 ? ? ? ? ? ? ? ? ? ? height = this.style.childHeight = this._height + this._wenMenu._menuSpacing; ? ? ? ? ? ? ? ? ? ? target.setAttribute('wen-expand', ''); ? ? ? ? ? ? ? ? ? ? target.removeAttribute('wen-collapse'); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? // todo:: 收起 ? ? ? ? ? ? ? ? ? ? height = -this.style.childHeight; ? ? ? ? ? ? ? ? ? ? this.style.childHeight = 0; ? ? ? ? ? ? ? ? ? ? target.setAttribute('wen-collapse', ''); ? ? ? ? ? ? ? ? ? ? target.removeAttribute('wen-expand'); ? ? ? ? ? ? ? ? ? ? this.resetHeight(this._childNodes) ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? this._childElement.style.height = this.style.childHeight + 'px'; ? ? ? ? ? ? ? ? if (this._parentNode) { ? ? ? ? ? ? ? ? ? ? this.setHeight({ ? ? ? ? ? ? ? ? ? ? ? ? menuNode: this._parentNode, ? ? ? ? ? ? ? ? ? ? ? ? direction: 'up', ? ? ? ? ? ? ? ? ? ? ? ? childHeight: height, ? ? ? ? ? ? ? ? ? ? ? ? childNode: this, ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? } else { ? ? ? ? if (this._isActive) { ? ? ? ? ? ? this._textElement.classList.add('wen-active'); ? ? ? ? } ? ? ? ? this._textElement.addEventListener('click', (e) => { ? ? ? ? ? ? if (this._wenMenu._autoCollapse) { ? ? ? ? ? ? ? ? this.resetHeight(); ? ? ? ? ? ? ? ? this.setHeight({ ? ? ? ? ? ? ? ? ? ? menuNode: this._parentNode, ? ? ? ? ? ? ? ? ? ? direction: 'up', ? ? ? ? ? ? ? ? ? ? childNode: this, ? ? ? ? ? ? ? ? ? ? childHeight: this._height, ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? } ? ? ? ? ? ? this.removeActive(this._wenMenu._activeMenu) ? ? ? ? ? ? this._isActive = true; ? ? ? ? ? ? this._textElement.classList.add('wen-active'); ? ? ? ? ? ? let target = e.target; ? ? ? ? ? ? if (target.classList.value.indexOf('wen-menu-text') >= 0) { ? ? ? ? ? ? ? ? target = target.parentElement; ? ? ? ? ? ? } ? ? ? ? ? ? if (target.classList.value.indexOf('wen-menu-first') >= 0) { ? ? ? ? ? ? ? ? this._wenMenu._activeMenu = this; ? ? ? ? ? ? } else if (this._parentNode) { ? ? ? ? ? ? ? ? this.addActive(this._parentNode, this) ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? this._wenMenu._activeMenu = this; ? ? ? ? ? ? } ? ? ? ? ? ? if (this._wenMenu._event) { ? ? ? ? ? ? ? ? this._wenMenu._event.call(this, e) ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? return this; } ? ? WenMenuNode.prototype.setHeight = function ({ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? menuNode = null, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? direction = 'down', ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? childHeight = 0, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? childNode = null, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }) { ? ? if (!menuNode) { ? ? ? ? return 0; ? ? } ? ? menuNode._textElement.setAttribute('wen-expand', ''); ? ? menuNode._textElement.removeAttribute('wen-collapse'); ? ? if (this._wenMenu._autoCollapse) { ? ? ? ? menuNode.style.childHeight = menuNode._height; ? ? } ? ? if (direction == 'down') { ? ? ? ? if (menuNode._subs.length) { ? ? ? ? ? ? menuNode.style.childHeight += (this._wenMenu._menuSpacing * (childNode ? childNode._level : 1)); ? ? ? ? ? ? if (menuNode._isActive) { ? ? ? ? ? ? ? ? menuNode.style.childHeight += this.setHeight({ ? ? ? ? ? ? ? ? ? ? menuNode: menuNode._activeChild, ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? } ? ? ? ? ? ? if (menuNode._childElement) { ? ? ? ? ? ? ? ? menuNode._childElement.style.height = menuNode.style.childHeight + "px"; ? ? ? ? ? ? } ? ? ? ? ? ? if (menuNode._parentNode) { ? ? ? ? ? ? ? ? this.setHeight({ ? ? ? ? ? ? ? ? ? ? menuNode: menuNode._parentNode, ? ? ? ? ? ? ? ? ? ? direction: 'up', ? ? ? ? ? ? ? ? ? ? childNode: menuNode, ? ? ? ? ? ? ? ? ? ? childHeight: menuNode.style.childHeight, ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? } ? ? ? ? } ? ? } else { ? ? ? ? menuNode.style.childHeight += (childHeight + this._wenMenu._menuSpacing); ? ? ? ? menuNode._childElement.style.height = menuNode.style.childHeight + "px"; ? ? ? ? if (menuNode._parentNode) { ? ? ? ? ? ? this.setHeight({ ? ? ? ? ? ? ? ? menuNode: menuNode._parentNode, ? ? ? ? ? ? ? ? direction: 'up', ? ? ? ? ? ? ? ? childHeight: menuNode.style.childHeight, ? ? ? ? ? ? ? ? childNode: menuNode, ? ? ? ? ? ? }); ? ? ? ? } ? ? } ? ? return menuNode.style.childHeight; } ? WenMenuNode.prototype.resetHeight = function (menuNodes) { ? ? if (!menuNodes) { ? ? ? ? menuNodes = this._wenMenu._menuNodes; ? ? } ? ? menuNodes.forEach((node) => { ? ? ? ? if (node._childElement) { ? ? ? ? ? ? node.style.childHeight = 0; ? ? ? ? ? ? node._childElement.style.height = '0px'; ? ? ? ? } ? ? ? ? if (node._childNodes.length) { ? ? ? ? ? ? node._textElement.setAttribute('wen-collapse', ''); ? ? ? ? ? ? node._textElement.removeAttribute('wen-expand'); ? ? ? ? ? ? this.resetHeight(node._childNodes); ? ? ? ? } ? ? }); ? ? return this; } ? WenMenuNode.prototype.addActive = function (menuNode, activeChildNode) { ? ? menuNode._isActive = true ? ? menuNode._textElement.setAttribute('wen-active', ''); ? ? menuNode._textElement.removeAttribute('wen-icon'); ? ? if (this._wenMenu._autoCollapse) { ? ? ? ? menuNode._textElement.setAttribute('wen-expand', ''); ? ? ? ? menuNode._textElement.removeAttribute('wen-collapse'); ? ? } ? ? menuNode._activeChild = activeChildNode; ? ? if (menuNode._parentNode) { ? ? ? ? this.addActive(menuNode._parentNode, menuNode); ? ? } else { ? ? ? ? this._wenMenu._activeMenu = menuNode; ? ? } ? ? return this; } ? /** ?* 去除active屬性 ?* @param ? ?WenMenuNode menuNode ?* @return ? ?WenMenuNode ?*/ WenMenuNode.prototype.removeActive = function (menuNode) { ? ? menuNode._isActive = false; ? ? if (menuNode._subs.length) { ? ? ? ? menuNode._textElement.removeAttribute('wen-active'); ? ? ? ? menuNode._textElement.setAttribute('wen-icon', ''); ? ? ? ? if (this._wenMenu._autoCollapse) { ? ? ? ? ? ? menuNode._textElement.setAttribute('wen-collapse', ''); ? ? ? ? ? ? menuNode._textElement.removeAttribute('wen-expand'); ? ? ? ? } ? ? ? ? if (menuNode._activeChild) { ? ? ? ? ? ? this.removeActive(menuNode._activeChild); ? ? ? ? } ? ? } else { ? ? ? ? menuNode._textElement.classList.remove('wen-active'); ? ? } ? ? return this; }
(2) WenMenu對(duì)象
let WenMenu = function ({ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ele, ? ? ? ? ? ? ? ? ? ? ? ? ? ? menus, ? ? ? ? ? ? ? ? ? ? ? ? ? ? event = null, ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes = {}, ? ? ? ? ? ? ? ? ? ? ? ? ? ? menuHeight = 35, ? ? ? ? ? ? ? ? ? ? ? ? ? ? menuSpacing = 0, ? ? ? ? ? ? ? ? ? ? ? ? ? ? autoCollapse = true, ? ? ? ? ? ? ? ? ? ? ? ? ? ? duration = 300, ? ? ? ? ? ? ? ? ? ? ? ? }) { ? ? this._ele = ele; ? ? this._duration = duration; ? ? this._menus = menus; ? ? this._event = event; ? ? this._menuNodes = []; ? ? this._autoCollapse = autoCollapse; ? ? this.style = { ? ? ? ? width: '100%', ? ? ? ? height: '100%', ? ? } ? ? this._menuElement = this.createElement('ul', attributes); ? ? this._menuElement.classList.value += 'wen-menu-ul wen-menu-ul-first'; ? ? this._ele.append(this._menuElement); ? ? this._activeMenu = null; ? ? this._menuHeight = menuHeight; ? ? this._menuSpacing = menuSpacing; ? ? this.init().createStyle().createMenu(); }; ? WenMenu.prototype.init = function () { ? ? if (this._ele.clientHeight) { ? ? ? ? this._ele.style.overflow = 'hidden'; ? ? ? ? this._menuElement.style['overflow-y'] = 'scroll'; ? ? ? ? let scrollWidth = this._menuElement.offsetWidth - this._menuElement.clientWidth; ? ? ? ? this.style.width = 'calc(100% + ' + scrollWidth + 'px)'; ? ? ? ? this.style.height = this._ele.clientHeight + 'px'; ? ? } ? ? return this; } ? /** ?* 創(chuàng)建菜單 ?*/ WenMenu.prototype.createMenu = function () { ? ? this._menus.forEach((item, i) => { ? ? ? ? let node = new WenMenuNode({ ? ? ? ? ? ? text: item.text, ? ? ? ? ? ? attributes: item.attributes, ? ? ? ? ? ? subs: item.subs, ? ? ? ? ? ? parentElement: this._menuElement, ? ? ? ? ? ? wenMenu: this, ? ? ? ? ? ? isActive: i == 0, ? ? ? ? }); ? ? ? ? this._menuNodes.push(node); ? ? }); ? ? return this; }; ? /** ?* 創(chuàng)建元素 ?* @param ? ?tagName ?* @param ? ?attributes ?* @returns ? ?{HTMLElement} ?*/ WenMenu.prototype.createElement = function (tagName, attributes = {}) { ? ? let ele = document.createElement(tagName); ? ? ? function checkValue(value) { ? ? ? ? if (Object.prototype.toString.call(value) === "[object Array]") { ? ? ? ? ? ? value = value.join(','); ? ? ? ? } else if (Object.prototype.toString.call(value) === '[object Object]') { ? ? ? ? ? ? var valueStr = ''; ? ? ? ? ? ? Object.keys(value).forEach(function (name) { ? ? ? ? ? ? ? ? valueStr += name + ":" + checkValue(value[name]) + ";"; ? ? ? ? ? ? }); ? ? ? ? ? ? value = valueStr; ? ? ? ? } ? ? ? ? return value; ? ? } ? ? ? if (attributes) { ? ? ? ? Object.keys(attributes).forEach((name) => { ? ? ? ? ? ? let value = checkValue(attributes[name]); ? ? ? ? ? ? ele.setAttribute(name, value); ? ? ? ? }) ? ? } ? ? return ele; }; ? ? WenMenu.prototype.createStyle = function () { ? ? let style = this.createElement('style'), ? ? ? ? head = document.querySelector('head'); ? ? style.innerHTML = ` ? ? ? ? .wen-menu-ul-first, .wen-menu-ul-first *{ ? ? ? ? ? ? padding: 0px; ? ? ? ? ? ? margin: 0px; ? ? ? ? ? ? border-spacing: 0px; ? ? ? ? ? ? list-style: none; ? ? ? ? } ? ? ? ? .wen-menu-ul-first{ ? ? ? ? ? ? width: ${this.style.width}; ? ? ? ? ? ? height: ${this.style.height}; ? ? ? ? } ? ? ? ? .wen-menu-ul { ? ? ? ? ? ? overflow: hidden; ? ? ? ? } ? ? ? ?? ? ? ? ? .wen-menu-ul-first, .wen-menu-ul-second { ? ? ? ? ? ? background: rgba(0, 0, 0, 0.1); ? ? ? ? } ? ? ? ? .wen-menu-li { ? ? ? ? ? ? padding-left: 22px; ? ? ? ? ? ? -webkit-box-sizing: border-box; ? ? ? ? ? ? -moz-box-sizing: border-box; ? ? ? ? ? ? box-sizing: border-box; ? ? ? ? } ? ? ? ? .wen-menu-ul-first > .wen-menu-li { ? ? ? ? ? ? padding: 0px; ? ? ? ? } ? ? ? ? .wen-menu-ul-second > .wen-menu-li { ? ? ? ? ? ? padding: 0px 0px 0px 18px; ? ? ? ? } ? ? ? ? .wen-menu-tree { ? ? ? ? ? ? border-left: 1px dashed rgba(0, 0, 0, 1); ? ? ? ? ? ? width: 16px; ? ? ? ? } ? ? ? ? .wen-menu-text{ ? ? ? ? ? ? width: calc(100% - 16px); ? ? ? ? ? ? display: flex; ? ? ? ? ? ? overflow: hidden; ? ? ? ? ? ? white-space: nowrap; ? ? ? ? ? ? text-overflow: ellipsis; ? ? ? ? ? ? -o-text-overflow: ellipsis; ? ? ? ? } ? ? ? ? .wen-menu-li a { ? ? ? ? ? ? display: inline-block; ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? height: ${this._menuHeight}px; ? ? ? ? ? ? -webkit-box-sizing: border-box; ? ? ? ? ? ? -moz-box-sizing: border-box; ? ? ? ? ? ? box-sizing: border-box; ? ? ? ? ? ? position: relative; ? ? ? ? ? ? cursor: pointer; ? ? ? ? ? ? display: flex; ? ? ? ? ? ? align-items: center; ? ? ? ? } ? ? ? ? .wen-menu-ul, .wen-menu-li a[wen-icon]:after, .wen-menu-li a[wen-active]:after { ? ? ? ? ? ? -webkit-transition: all ${this._duration}ms linear; ? ? ? ? ? ? -moz-transition: all ${this._duration}ms linear; ? ? ? ? ? ? -ms-transition: all ${this._duration}ms linear; ? ? ? ? ? ? -o-transition: all ${this._duration}ms linear; ? ? ? ? ? ? transition: all ${this._duration}ms linear; ? ? ? ? } ? ? ? ? .wen-menu-li a[wen-expand]:after { ? ? ? ? ? ? -webkit-transform: scale(1.3) rotate(90deg); ? ? ? ? ? ? -moz-transform: scale(1.3) rotate(90deg); ? ? ? ? ? ? -ms-transform: scale(1.3) rotate(90deg); ? ? ? ? ? ? -o-transform: scale(1.3) rotate(90deg); ? ? ? ? ? ? transform: scale(1.3) rotate(90deg); ? ? ? ? } ? ? ? ? .wen-menu-li a[wen-collapse]:after { ? ? ? ? ? ? -webkit-transform: scale(1.3) rotate(180deg); ? ? ? ? ? ? -moz-transform: scale(1.3) rotate(180deg); ? ? ? ? ? ? -ms-transform: scale(1.3) rotate(180deg); ? ? ? ? ? ? -o-transform: scale(1.3) rotate(180deg); ? ? ? ? ? ? transform: scale(1.3) rotate(180deg); ? ? ? ? } ? ? ? ? .wen-menu-li a[wen-icon]:after { ? ? ? ? ? ? content: '?'; ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? right: 5px; ? ? ? ? ? ? font-weight: bold; ? ? ? ? } ? ? ? ? .wen-menu-li a[wen-active]:after { ? ? ? ? ? ? content: '?'; ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? right: 5px; ? ? ? ? ? ? font-weight: bold; ? ? ? ? } ? ? ? ? .wen-menu-first { ? ? ? ? ? ? padding-left: 15px !important; ? ? ? ? } ? ? ? ? .wen-menu-li a[wen-active], .wen-active { ? ? ? ? ? ? color: white; ? ? ? ? } ? ? ? ? .wen-menu-li a[wen-active].wen-menu-first, .wen-active.wen-menu-first { ? ? ? ? ? ? border-left: 3px solid white; ? ? ? ? } ? ? `; ? ? if (!head) { ? ? ? ? head = document.body; ? ? } ? ? head.append(style); ? ? return this; }
三、Example-Code
(1)html文件
<!doctype html> <html lang="en"> <head> ? ? <meta charset="UTF-8"> ? ? <meta name="viewport" ? ? ? ? ? content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> ? ? <meta http-equiv="X-UA-Compatible" content="ie=edge"> ? ? <title>說明文檔</title> ? ? <script src="/js/canvas/menu.js"></script> ? ? <style> ? ? ? ? * { ? ? ? ? ? ? padding: 0px; ? ? ? ? ? ? margin: 0px; ? ? ? ? ? ? border-spacing: 0px; ? ? ? ? ? ? list-style: none; ? ? ? ? } ? ? ? ? body { ? ? ? ? ? ? min-height: 100vh; ? ? ? ? ? ? padding: 0px; ? ? ? ? ? ? margin: 0px; ? ? ? ? } ? ? ? ? .readme-title { ? ? ? ? ? ? background: rgba(0, 0, 50, 0.3); ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? display: flex; ? ? ? ? ? ? justify-content: center; ? ? ? ? ? ? align-items: center; ? ? ? ? ? ? padding: 20px; ? ? ? ? ? ? -webkit-box-sizing: border-box; ? ? ? ? ? ? -moz-box-sizing: border-box; ? ? ? ? ? ? box-sizing: border-box; ? ? ? ? } ? ? ? ? .readme-body { ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? height: calc(100% - 75px); ? ? ? ? ? ? display: flex; ? ? ? ? ? ? justify-content: start; ? ? ? ? } ? ? ? ? .readme-menu-box { ? ? ? ? ? ? width: 250px; ? ? ? ? ? ? height: 100%; ? ? ? ? ? ? position: fixed; ? ? ? ? ? ? left: 0px; ? ? ? ? ? ? top: 0px; ? ? ? ? ? ? background: rgba(100, 10, 10, 0.2); ? ? ? ? ? ? overflow: auto; ? ? ? ? } ? ? </style> </head> <body> <div class="readme-body"> ? ? <div class="readme-menu-box"> ? ? ? ? <div class="readme-title"> ? ? ? ? ? ? <h2>目錄</h2> ? ? ? ? </div> ? ? </div> </div> </body> </html>
(2)JS代碼
a、菜單列表
let menuOptions = [{ ? ? ? ? ? ? ? ? ? ? text: "導(dǎo)入數(shù)據(jù)列表", ? ? ? ? ? ? ? ? ? ? subs: [ ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "全部數(shù)據(jù)", ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? ? ? ? ? subs: [ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "消費(fèi)金額", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "放款金額", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "返傭金額", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "導(dǎo)入數(shù)據(jù)", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "查看", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "編輯", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ] ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "消費(fèi)金額", ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "放款金額", ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "返傭金額", ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "導(dǎo)入數(shù)據(jù)", ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "查看", ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? ? ? ? ? text: "編輯", ? ? ? ? ? ? ? ? ? ? ? ? ? ? attributes: { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "data-url": "", ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ] ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? text: "異常數(shù)據(jù)列表", ? ? ? ? ? ? ? ? ? ? subs: [] ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? text: "數(shù)據(jù)修正", ? ? ? ? ? ? ? ? ? ? subs: [] ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? text: "修正審核-客服經(jīng)理", ? ? ? ? ? ? ? ? ? ? subs: [] ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? text: "修正審核-財(cái)務(wù)", ? ? ? ? ? ? ? ? ? ? subs: [] ? ? ? ? ? ? ? ? }, { ? ? ? ? ? ? ? ? ? ? text: "導(dǎo)入日志", ? ? ? ? ? ? ? ? ? ? subs: [] ? ? ? ? ? ? ? ? }]
b、菜單實(shí)例化
window.onload = function () { ? ? ? ? ? ? new WenMenu({ ? ? ? ? ? ? ? ? ele: document.querySelector('.readme-menu-box'), // 菜單插入的位置 ? ? ? ? ? ? ? ? menus: menuOptions, ? ? ? ? ? ? ? ? event: function (e) { }, // 菜單最底端點(diǎn)擊事件觸發(fā) ? ? ? ? ? ? ? ? attributes: {}, // 最外層ul屬性設(shè)置 ? ? ? ? ? ? ? ? menuHeight: 35, // 每個(gè)菜單項(xiàng)的高度 ? ? ? ? ? ? ? ? autoCollapse: true, // 是否自動(dòng)收起無(wú)活動(dòng)菜單 ? ? ? ? ? ? }) ? ? ? ? };
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)頂部可折疊的菜單工具欄效果實(shí)例
- Extjs學(xué)習(xí)筆記之四 工具欄和菜單
- JS實(shí)現(xiàn)自動(dòng)固定頂部的懸浮菜單欄效果
- 基于vue.js實(shí)現(xiàn)側(cè)邊菜單欄
- 使用ReactJS實(shí)現(xiàn)tab頁(yè)切換、菜單欄切換、手風(fēng)琴切換和進(jìn)度條效果
- 簡(jiǎn)單實(shí)現(xiàn)js菜單欄切換效果
- 原生JS仿蘋果任務(wù)欄菜單,放大效果的菜單
- JS實(shí)現(xiàn)仿蘋果底部任務(wù)欄菜單效果代碼
- JavaScript NodeTree導(dǎo)航欄(菜單項(xiàng)JSON類型/自制)
- 非常酷的js圖形漸隱導(dǎo)航菜單欄
相關(guān)文章
學(xué)習(xí)使用bootstrap的modal和carousel
這篇文章主要教大家學(xué)會(huì)用bootstrap的modal和carousel,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12js實(shí)現(xiàn)簡(jiǎn)易購(gòu)物車功能
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡(jiǎn)易購(gòu)物車功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10JavaScript實(shí)現(xiàn)帶標(biāo)題的圖片輪播特效
這里給大家分享的是4屏帶標(biāo)題和文字描述的js圖片輪播代碼,完美兼容IE6。圖片滾動(dòng)切換,鼠標(biāo)放到數(shù)字選項(xiàng)卡即可切換圖片。點(diǎn)擊圖片跳轉(zhuǎn)到指定頁(yè)面,有需要的小伙伴可以參考下。2015-05-05three.js 制作動(dòng)態(tài)二維碼的示例代碼
這篇文章主要介紹了three.js 制作動(dòng)態(tài)二維碼的示例代碼,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07無(wú)閃爍更新網(wǎng)頁(yè)內(nèi)容JS實(shí)現(xiàn)
這篇文章主要介紹了無(wú)閃爍更新網(wǎng)頁(yè)內(nèi)容JS實(shí)現(xiàn),有需要的朋友可以參考一下2013-12-12詳釋JavaScript執(zhí)行環(huán)境與執(zhí)行棧
一句話就可以概括:代碼 ( 包括函數(shù) ) 執(zhí)行時(shí)所需要的所有信息就是執(zhí)行環(huán)境。由于 ES 歷經(jīng)多個(gè)版本,所以執(zhí)行環(huán)境的標(biāo)準(zhǔn)也一直在變。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04List Information About the Binary Files Used by an Applicati
List Information About the Binary Files Used by an Application...2007-06-06javaScript canvas實(shí)現(xiàn)(畫筆大小 顏色 橡皮的實(shí)例)
下面小編就為大家分享一篇javaScript canvas實(shí)現(xiàn)(畫筆大小 顏色 橡皮的實(shí)例),具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11