feat: lay.onClickOutside 新增 capture 选项

sight 6 months ago
parent 40943aa6fc
commit 1c1c9e6912

@ -628,6 +628,7 @@
* @param {string} [options.event="pointerdown"] - 监听的事件类型
* @param {HTMLElement | Window} [options.scope=document] - 监听范围
* @param {Array<HTMLElement | string>} [options.ignore] - 忽略监听的元素或选择器字符串
* @param {boolean} [options.capture=true] - 对内部事件侦听器使用捕获阶段
* @returns {() => void} - 返回一个停止事件监听的函数
*/
lay.onClickOutside = function(target, handler, options){
@ -635,6 +636,7 @@
var eventType = options.event || ('onpointerdown' in window ? 'pointerdown' : 'mousedown');
var scopeTarget = options.scope || document;
var ignore = options.ignore || [];
var useCapture = 'capture' in options ? options.capture : true;
var listener = function(event){
var el = target;
@ -692,14 +694,14 @@
}
if(document.addEventListener){
scopeTarget.addEventListener(eventType, listener, false);
scopeTarget.addEventListener(eventType, listener, useCapture);
}else{
scopeTarget.attachEvent('on' + eventType, listener);
}
var stop = function(){
if(document.removeEventListener){
scopeTarget.removeEventListener(eventType, listener, false);
scopeTarget.removeEventListener(eventType, listener, useCapture);
}else{
scopeTarget.detachEvent('on' + eventType, listener);
}

Loading…
Cancel
Save