/** * layer助手 * 最后修改:2020.08.26 */ var currentwinlayer = layer var layer = top.layer || parent.layer || layer // 重写加载中,更语义化 layer.loading = layer.load $(function () { 'use strict' // 点击链接或按钮打开iframe窗口,窗口尺寸用data-winsize="[650,300]"定义,标题用title='xxx'定义,目标url在href或者data-url里面 $('a.win,input[type="button"].win,button.win').on('click', function (e) { e.preventDefault() var $this = $(this) var winsize = $this.data('winsize') || [750, 400] var title = ($this.attr('title') || $this.data('title') || $this.text() || $this.val()).replace(/ /g, '') || '对话框' var url = $this.attr('href') || $this.data('url') if (url === '') { layer.msg('对不起,URL参数为空。', { icon: 5 }) } else { // 相对路径转换为绝对路径,参考:http://www.cnblogs.com/ranran/p/3599025.html var a = document.createElement('a') a.href = url url = a.href // 特定页面 if (url.match(/qrcode\.htm/i)) { title = '网址二维码' winsize = [600, 350] } else if (url.match(/studentinfo\.asp/i)) { title = '查看/编辑' winsize = [650, 350] } else if (url.match(/recordview\.asp/i)) { winsize = [750, 400] } if (url.match(/\.(png|jpg|jpeg|gif)$/i)) { // 图片 currentwinlayer.photos({ photos: { data: [{ src: url }] }, success: function () { $('div.layui-layer-imgsee').remove() } }) } else { // url layer.open({ type: 2, title: title, shadeClose: true, area: [winsize[0] + 'px', winsize[1] + 'px'], content: url }) } } }) // 预览图片 $('input[type="button"][value="预 览"],button:contains("预 览")').on('click', function () { var $input = $(this).prev('input[type="text"]') if ($input.length > 0) { var url = $input.val() if (url.match(/\.(png|jpg|jpeg|gif|webp)$/i)) { currentwinlayer.photos({ photos: { data: [{ src: url }] }, success: function () { $('div.layui-layer-imgsee').remove() } }) } else { layer.msg('图片地址为空或不正确(必须以图片格式的扩展名结尾)。') $input.trigger('focus') } } else { layer.msg('按钮前无图片网址框,请检查。') } }) }) // 手动执行打开layer窗口,参数:目标网址,标题文字,窗口尺寸(逗号隔开两个数字),关闭后的回调 function win_open(url, t, winsize, end) { 'use strict' if (typeof (winsize) === 'undefined') { winsize = [750, 400] } else { winsize = winsize.split(',') } var title = t || '对话框' var a = document.createElement('a') a.href = url url = a.href if (url.match(/\.(png|jpg|jpeg|gif)$/i)) { currentwinlayer.photos({ photos: { data: [{ src: url }] }, success: function () { $('div.layui-layer-imgsee').remove() } }) } else if (typeof (end) === 'function') { layer.open({ type: 2, title: title, shadeClose: true, area: [winsize[0] + 'px', winsize[1] + 'px'], content: url, end: end }) } else { layer.open({ type: 2, title: title, shadeClose: true, area: [winsize[0] + 'px', winsize[1] + 'px'], content: url }) } } /* 其他常用操作: layer.loading() layer.load() layer.msg('提示信息,会自动关闭') layer.closeAll() layer.alert('有确定按钮的提示信息') layer.open({title:'标题',content:'内容'}) */