优化 layer 所在的 window resize 事件机制,避免事件不断叠加

pull/1112/head
贤心 2022-08-13 00:43:10 +08:00
parent 089f2d2b40
commit 2b3c5ebf2b
1 changed files with 26 additions and 7 deletions

View File

@ -24,7 +24,7 @@ var isLayui = window.layui && layui.define, $, win, ready = {
return GLOBAL.layer_dir || jsPath.substring(0, jsPath.lastIndexOf('/') + 1); return GLOBAL.layer_dir || jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
}(), }(),
config: {}, end: {}, minIndex: 0, minLeft: [], config: {}, end: {}, events: {resize: {}}, minIndex: 0, minLeft: [],
btn: ['确定', '取消'], btn: ['确定', '取消'],
//五种原始层模式 //五种原始层模式
@ -445,13 +445,15 @@ Class.pt.creat = function(){
}(); }();
}(); }();
//如果是固定定位 // 若是固定定位,则跟随 resize 事件来自适应坐标
if(config.fixed){ if(config.fixed){
win.on('resize', function(){ if(!ready.events.resize[that.index]){
that.offset(); ready.events.resize[that.index] = function(){
(/^\d+%$/.test(config.area[0]) || /^\d+%$/.test(config.area[1])) && that.auto(times); that.resize();
config.type == 4 && that.tips(); };
}); // 此处 resize 事件不会一直叠加,当关闭弹层时会移除该事件
win.on('resize', ready.events.resize[that.index]);
}
} }
config.time <= 0 || setTimeout(function(){ config.time <= 0 || setTimeout(function(){
@ -473,6 +475,16 @@ Class.pt.creat = function(){
} }
}; };
// 当前实例的 resize 事件
Class.pt.resize = function(){
var that = this;
var config = that.config;
that.offset();
(/^\d+%$/.test(config.area[0]) || /^\d+%$/.test(config.area[1])) && that.auto(times);
config.type == 4 && that.tips();
};
//自适应 //自适应
Class.pt.auto = function(index){ Class.pt.auto = function(index){
var that = this, config = that.config, layero = $('#'+ doms[0] + index); var that = this, config = that.config, layero = $('#'+ doms[0] + index);
@ -1099,9 +1111,16 @@ layer.close = function(index, callback){
layero[0].innerHTML = ''; layero[0].innerHTML = '';
layero.remove(); layero.remove();
} }
typeof ready.end[index] === 'function' && ready.end[index](); typeof ready.end[index] === 'function' && ready.end[index]();
delete ready.end[index]; delete ready.end[index];
typeof callback === 'function' && callback(); typeof callback === 'function' && callback();
// 移除 reisze 事件
if(ready.events.resize[index]){
win.off('resize', ready.events.resize[index]);
delete ready.events.resize[index];
}
}; };
if(layero.data('isOutAnim')){ if(layero.data('isOutAnim')){