From 3842a9836089e6aaab1104440cc501b3ebabfea2 Mon Sep 17 00:00:00 2001 From: morning-star <26325820+Sight-wcg@users.noreply.github.com> Date: Thu, 17 Apr 2025 23:08:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(dropdown):=20onClickoutside=20=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=20iframe=20=E5=8C=BA=E5=9F=9F=20(#2629)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/dropdown.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/modules/dropdown.js b/src/modules/dropdown.js index df3eff9d..f0053cd9 100644 --- a/src/modules/dropdown.js +++ b/src/modules/dropdown.js @@ -567,7 +567,29 @@ layui.define(['jquery', 'laytpl', 'lay', 'util'], function(exports) { } that.remove(); - }, {passive: false}); + }, lay.passiveSupported ? { passive: false} : false); + + // onClickOutside 检测 iframe + _WIN.on('blur', function(e){ + if(!dropdown.thisId) return; + var that = thisModule.getThis(dropdown.thisId) + if(!that) return; + if(!that.config.elem.data(MOD_INDEX_OPENED)) return; + + setTimeout(function(){ + if(document.activeElement && document.activeElement.tagName === 'IFRAME' + && that.mainElem && that.mainElem[0] + && that.mainElem[0].contains && !that.mainElem[0].contains(document.activeElement) + ){ + // 点击 dropdown 外部时的回调 + if(typeof that.config.onClickOutside === 'function'){ + var shouldClose = that.config.onClickOutside(e.originalEvent); + if(shouldClose === false) return; + } + that.remove(); + } + }, 0); + }) // 基础菜单的静态元素事件 var ELEM_LI = '.layui-menu:not(.layui-dropdown-menu) li';