From f955a75d652375d28a22f2862969fdf82e40e38c Mon Sep 17 00:00:00 2001 From: sunxiaobin89 <285584806@qq.com> Date: Sat, 16 Jul 2022 02:10:18 +0800 Subject: [PATCH] =?UTF-8?q?layer=20=E5=BC=B9=E5=87=BA=E4=B9=8B=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E9=A1=B5=E9=9D=A2=E7=84=A6=E7=82=B9=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/css/modules/layer/default/layer.css | 1 + src/modules/layer.js | 46 +++++++++++++++++++++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/css/modules/layer/default/layer.css b/src/css/modules/layer/default/layer.css index 54a6aeee..c61fb830 100644 --- a/src/css/modules/layer/default/layer.css +++ b/src/css/modules/layer/default/layer.css @@ -20,6 +20,7 @@ html #layuicss-layer{display: none; position: absolute; width: 1989px;} .layui-layer-dialog .layui-layer-ico, .layui-layer-setwin a, .layui-layer-btn a{display:inline-block; *display:inline; *zoom:1; vertical-align:top;} +.layui-layer-btn a:focus {border-style: dashed;border-color: rgba(46, 45, 60, 0.84);} .layui-layer-move{display: none; position: fixed; *position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; cursor: move; opacity: 0; filter:alpha(opacity=0); background-color: #fff; z-index: 2147483647;} .layui-layer-resize{position: absolute; width: 15px; height: 15px; right: 0; bottom: 0; cursor: se-resize;} diff --git a/src/modules/layer.js b/src/modules/layer.js index edcee5b0..46af8528 100644 --- a/src/modules/layer.js +++ b/src/modules/layer.js @@ -133,7 +133,8 @@ var layer = { if(type) yes = options; return layer.open($.extend({ content: content, - yes: yes + yes: yes, + autofocus: true }, type ? {} : options)); }, @@ -146,6 +147,7 @@ var layer = { return layer.open($.extend({ content: content, btn: ready.btn, + autofocus: true, yes: yes, btn2: cancel }, type ? {} : options)); @@ -164,6 +166,7 @@ var layer = { title: false, closeBtn: false, btn: false, + autofocus: false, resize: false, end: end }, (type && !ready.config.skin) ? { @@ -179,11 +182,16 @@ var layer = { }, load: function(icon, options){ + var activeElem = document.activeElement; // 记录弹出时的焦点 return layer.open($.extend({ type: 3, icon: icon || 0, resize: false, - shade: 0.01 + shade: 0.01, + autofocus: true, // 加载的时候去掉原来的焦点 + end: function () { + activeElem.focus(); // 恢复加载前的焦点 + } }, options)); }, @@ -192,6 +200,7 @@ var layer = { type: 4, content: [content, follow], closeBtn: false, + autofocus: false, time: 3000, shade: false, resize: false, @@ -667,6 +676,29 @@ Class.pt.move = function(){ Class.pt.callback = function(){ var that = this, layero = that.layero, config = that.config; that.openLayer(); + + var btnElem = layero.find('.'+ doms[6]).children('a'); + + // 根据autofocus设置默认聚焦的按钮 + var autofocus = config.autofocus; + if (autofocus) { + document.activeElement.blur(); + btnElem.attr('href', 'javascript:;'); + var btnFocus; + if (layui.type(autofocus) === 'string') { + var btnNum = parseInt(autofocus.substr(3)) || 1; + btnFocus = btnElem.filter(function (index, item) { + return $(item).hasClass(doms[0] + '-' + autofocus.substr(0, 3) + (btnNum - 1)); + }).first(); + } else { + btnFocus = btnElem.first(); + } + btnFocus && btnFocus.length && btnFocus.focus(); + layero.one('click', function (event) { + btnElem.attr('href', null); + }) + } + if(config.success){ if(config.type == 2){ layero.find('iframe').on('load', function(){ @@ -679,7 +711,7 @@ Class.pt.callback = function(){ layer.ie == 6 && that.IE6(layero); //按钮 - layero.find('.'+ doms[6]).children('a').on('click', function(){ + btnElem.on('click', function(){ var index = $(this).index(); if(index === 0){ if(config.yes){ @@ -1058,6 +1090,7 @@ layer.prompt = function(options, yes){ return layer.open($.extend({ type: 1 ,btn: ['确定','取消'] + ,autofocus: false ,content: content ,skin: 'layui-layer-prompt' + skin('prompt') ,maxWidth: win.width() @@ -1069,9 +1102,10 @@ layer.prompt = function(options, yes){ ,resize: false ,yes: function(index){ var value = prompt.val(); - if(value === ''){ - prompt.focus(); - } else if(value.length > (options.maxlength||500)) { + // if(value === ''){ + // prompt.focus(); + // } else // 允许为空 + if(value.length > (options.maxlength||500)) { layer.tips('最多输入'+ (options.maxlength || 500) +'个字数', prompt, {tips: 1}); } else { yes && yes(value, index, prompt);