From dab67b0a6c0000aeea16702fcf6605926b09b59e Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Wed, 18 Oct 2023 01:54:58 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20dropdown=20=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E7=BB=84=E6=8A=98=E5=8F=A0=E5=8A=A8=E7=94=BB=E6=95=88?= =?UTF-8?q?=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/css/layui.css | 6 ++++-- src/modules/dropdown.js | 48 +++++++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/css/layui.css b/src/css/layui.css index 2989c8ad..9d37e181 100644 --- a/src/css/layui.css +++ b/src/css/layui.css @@ -1201,7 +1201,7 @@ body .layui-table-tips .layui-layer-content{background: none; padding: 0; box-sh .layui-menu li, .layui-menu-body-title, .layui-menu-body-title a{padding: 5px 15px; color: initial} -.layui-menu li{position: relative; margin: 1px 0; line-height: 26px; color: rgba(0,0,0,.8); font-size: 14px; white-space: nowrap; cursor: pointer; transition: all .3s;} +.layui-menu li{position: relative; margin: 0 0 1px; line-height: 26px; color: rgba(0,0,0,.8); font-size: 14px; white-space: nowrap; cursor: pointer; transition: all .3s;} .layui-menu li:hover{background-color: #f8f8f8; } .layui-menu li.layui-disabled, .layui-menu li.layui-disabled *{background: none !important; color: #d2d2d2 !important; cursor: not-allowed !important;} @@ -1224,6 +1224,8 @@ body .layui-table-tips .layui-layer-content{background: none; padding: 0; box-sh .layui-menu .layui-menu-item-down:hover{cursor: pointer;} .layui-menu .layui-menu-item-up>.layui-menu-body-title{ color: rgba(0,0,0,.8);} .layui-menu .layui-menu-item-up>ul{visibility: hidden; height: 0; overflow: hidden;} +.layui-menu .layui-menu-item-down>.layui-menu-body-title>.layui-icon-down{transform: rotate(180deg);} +.layui-menu .layui-menu-item-up>.layui-menu-body-title>.layui-icon-up{transform: rotate(-180deg);} .layui-menu .layui-menu-item-up>.layui-menu-body-title:hover>.layui-icon, .layui-menu .layui-menu-item-down:hover>.layui-menu-body-title>.layui-icon{color: rgba(0,0,0,1);} .layui-menu .layui-menu-item-down>ul{visibility: visible; height: auto;} @@ -1237,7 +1239,7 @@ body .layui-table-tips .layui-layer-content{background: none; padding: 0; box-sh .layui-menu-body-title{position: relative; margin: -5px -15px; overflow: hidden; text-overflow: ellipsis;} .layui-menu-body-title a{display: block; margin: -5px -15px; color: rgba(0,0,0,.8);} .layui-menu-body-title a:hover{transition: all .3s;} -.layui-menu-body-title>.layui-icon{position: absolute; right: 15px; top: 50%; margin-top: -6px; line-height: normal; font-size: 14px;} +.layui-menu-body-title>.layui-icon{position: absolute; right: 15px; top: 50%; margin-top: -6px; line-height: normal; font-size: 14px; transition: all .2s; -webkit-transition: all .2s;} .layui-menu-body-title>.layui-icon:hover{transition: all .3s;} .layui-menu-body-title>.layui-icon-right{right: 14px;} .layui-menu-body-panel{display: none; position: absolute; top: -7px; left: 100%; z-index: 1000; margin-left: 13px; padding: 5px 0;} diff --git a/src/modules/dropdown.js b/src/modules/dropdown.js index 2bab9663..7cf3ad9e 100644 --- a/src/modules/dropdown.js +++ b/src/modules/dropdown.js @@ -102,7 +102,8 @@ layui.define(['jquery', 'laytpl', 'lay', 'util'], function(exports){ isSpreadItem: true, // 是否初始展开子菜单 data: [], // 菜单数据结构 delay: 300, // 延迟关闭的毫秒数,若 trigger 为 hover 时才生效 - shade: 0 // 遮罩 + shade: 0, // 遮罩 + accordion: false // 手风琴效果,仅菜单组生效。基础菜单需要在容器上追加 'lay-accordion' 属性。 }; // 重载实例 @@ -343,12 +344,12 @@ layui.define(['jquery', 'laytpl', 'lay', 'util'], function(exports){ // 触发菜单组展开收缩 that.elemView.find(STR_GROUP_TITLE).on('click', function(e){ - var othis = $(this) - ,elemGroup = othis.parent() - ,data = elemGroup.data('item') || {} + var othis = $(this); + var elemGroup = othis.parent(); + var data = elemGroup.data('item') || {}; if(data.type === 'group' && options.isAllowSpread){ - thisModule.spread(elemGroup); + thisModule.spread(elemGroup, options); } }); @@ -442,17 +443,36 @@ layui.define(['jquery', 'laytpl', 'lay', 'util'], function(exports){ }; // 设置菜单组展开和收缩状态 - thisModule.spread = function(othis){ - // 菜单组展开和收缩 + thisModule.spread = function(othis, options){ var needSpread = othis.hasClass(STR_ITEM_UP); - var elemIcon = othis.children('.'+ STR_MENU_TITLE).find('.layui-icon-' + (needSpread ? 'down' : 'up')); - if(needSpread){ - othis.removeClass(STR_ITEM_UP).addClass(STR_ITEM_DOWN); - elemIcon.removeClass('layui-icon-down').addClass('layui-icon-up'); - } else { - othis.removeClass(STR_ITEM_DOWN).addClass(STR_ITEM_UP); - elemIcon.removeClass('layui-icon-up').addClass('layui-icon-down'); + var accordion = options + ? options.accordion + : typeof othis.parents('.layui-menu').eq(0).attr('lay-accordion') === 'string'; + + var toggle = function (groupElem, isOpen) { + var contentElem = groupElem.children('ul'); + var contentHeight = contentElem[0].scrollHeight; + + groupElem.removeClass(isOpen ? STR_ITEM_UP : STR_ITEM_DOWN).addClass(isOpen ? STR_ITEM_DOWN : STR_ITEM_UP); + contentElem.height(isOpen ? 0 : contentHeight) + .stop() + .animate({ + opacity: isOpen ? 1 : 0, + height: isOpen ? contentHeight : 0 + }, 200, function(){ + contentElem.css({ height: '', opacity: '' }) + }) } + + toggle(othis, needSpread); + + if (!needSpread || !accordion) return; + othis.siblings('.' + STR_ITEM_GROUP).each(function (index, item) { + var itemElem = $(item) + if(!itemElem.hasClass(STR_ITEM_UP)){ + toggle(itemElem, false); + } + }) }; // 全局事件 From 73c1129f2ca1c594308aa10e43de203e886ebbad Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Wed, 18 Oct 2023 02:23:52 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20dropdown=20=E5=92=8C?= =?UTF-8?q?=20menu=20=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/dropdown/detail/options.md | 14 ++++++++++++++ docs/menu/index.md | 1 + 2 files changed, 15 insertions(+) diff --git a/docs/dropdown/detail/options.md b/docs/dropdown/detail/options.md index 1f058f56..6aa5b415 100644 --- a/docs/dropdown/detail/options.md +++ b/docs/dropdown/detail/options.md @@ -120,6 +120,20 @@ `true` + + +