feat: lay.onClickOutside 新增 detectIframe 属性

sight 6 months ago
parent 1c1c9e6912
commit 1fe122bbb4

@ -457,7 +457,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){
hideDown(); hideDown();
initValue && input.val(initValue); initValue && input.val(initValue);
}, },
{ignore: title} {ignore: title, detectIframe: true}
); );
}; };

@ -629,6 +629,7 @@
* @param {HTMLElement | Window} [options.scope=document] - 监听范围 * @param {HTMLElement | Window} [options.scope=document] - 监听范围
* @param {Array<HTMLElement | string>} [options.ignore] - 忽略监听的元素或选择器字符串 * @param {Array<HTMLElement | string>} [options.ignore] - 忽略监听的元素或选择器字符串
* @param {boolean} [options.capture=true] - 对内部事件侦听器使用捕获阶段 * @param {boolean} [options.capture=true] - 对内部事件侦听器使用捕获阶段
* @param {boolean} [options.detectIframe] - 是否检测 iframe
* @returns {() => void} - 返回一个停止事件监听的函数 * @returns {() => void} - 返回一个停止事件监听的函数
*/ */
lay.onClickOutside = function(target, handler, options){ lay.onClickOutside = function(target, handler, options){
@ -637,6 +638,7 @@
var scopeTarget = options.scope || document; var scopeTarget = options.scope || document;
var ignore = options.ignore || []; var ignore = options.ignore || [];
var useCapture = 'capture' in options ? options.capture : true; var useCapture = 'capture' in options ? options.capture : true;
var detectIframe = options.detectIframe;
var listener = function(event){ var listener = function(event){
var el = target; var el = target;
@ -693,21 +695,36 @@
return [eventTarget].concat(getParents(eventTarget)); return [eventTarget].concat(getParents(eventTarget));
} }
if(document.addEventListener){ function bindEventListener(elem, eventName, handler, opts){
scopeTarget.addEventListener(eventType, listener, useCapture); elem.addEventListener
}else{ ? elem.addEventListener(eventName, handler, opts)
scopeTarget.attachEvent('on' + eventType, listener); : elem.attachEvent('on' + eventName, handler);
}
var stop = function(){ return function(){
if(document.removeEventListener){ elem.removeEventListener
scopeTarget.removeEventListener(eventType, listener, useCapture); ? elem.removeEventListener(eventName, handler, opts)
}else{ : elem.detachEvent('on' + eventName, handler);
scopeTarget.detachEvent('on' + eventType, listener);
} }
} }
return stop; var cleanup = [
bindEventListener(scopeTarget, eventType, listener, lay.passiveSupported ? { passive: true, capture: useCapture } : useCapture),
detectIframe && bindEventListener(window, 'blur', function(event){
setTimeout(function(){
if(document.activeElement && document.activeElement.tagName === 'IFRAME'
&& target.contains && !target.contains(document.activeElement)
){
handler(event);
}
}, 0);
})
]
return function(){
for(var i=0; i < cleanup.length; i++){
cleanup[i] && cleanup[i]();
}
}
}; };

Loading…
Cancel
Save