背景

之前项目的模态框改造完成,业务也想把页面中的提示框和确认框也改造一下;这里记录一下改造中的细节。

之前项目中的提示框和确认框用的是浏览器自带的 alertconfirm。改造之前无法支持业务一些复杂一点的需求,遂将之改造;

实现

弹框层级设计参考了 elementantd,默认样式也是复用了他们的样式。层级从父到子如下所示

messageBoxWrap 是弹出框包裹层和遮罩层。messageBox 是弹出框本身,里面包含了标题,内容,按钮。

而且因为默认 confirm 框会有返回值,所以这里改造后的也就返回了一个 Promise,并且只会 resolve(true) 或者 resolve(false)

// type 区分是 alert 还是 confirm
// paramSting 表示提示框的内容,项目中没有自定义标题的需求,所以这里提示框都用统一的标题
function showMessagebox(type, paramString) {
//获取项目顶层的document,弹框显示需要覆盖顶层页面
var topDoc = window.top.document;
var mainFrameSet = topDoc.querySelector("#setMain");
var mainFrameSetParent = mainFrameSet.parentNode;
var topDocBody = window.top.document.body;
//创建弹框盒子,添加遮罩层
return new Promise((resolve, reject) => {
// 具体实现
});
}

注意事项

有几个注意的点是

  • 创建弹框时需要禁用键盘事件,弹框消失后启用键盘事件,防止弹框还未消失,使用 tab+enter去了其他页面的情况
  • 需要避免有一个弹框存在的情况下,重复点击创建弹框

然后是具体实现。

添加默认样式

/*弹框的默认样式;*/
var messageBoxWrap_class = "modernMessagebox";
var messageBoxWrap_style =
"width:100%;height:100%;position:fixed;top:0;left:0;text-align:center;";
var messageBox_style =
"user-select:none;display: inline-block;width: 420px;padding: 10px 0px 20px;vertical-align: middle;background-color: #fff;border-radius: 4px;border: 1px solid #ebeef5;font-size:18px;box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);text-align: left;";
var header_style =
"padding: 10px 16px 10px;font-size:14px;display:flex;justify-content: space-between;";
var content_style = "padding: 10px 15px;color: #606266;font-size: 14px;";
var footer_style = "padding: 5px 15px 0;text-align: right;";
var button_style =
"display: inline-block;line-height: 1;wite-space: nowrap;cursor: pointer;background: #fff;border: 1px solid #dcdfe6;color: #606266;-webkit-appearance: none;text-align:center;box-sizing:border-box;outline:0;margin:0;transition:.1s;font-weight:500;padding:10px 16px;font-size:12px;border-radius:3px;";
var button_primary =
"color: #fff;background: rgb(11, 87, 208);border: 1px solid rgb(11, 87, 208);border-radius: 20px 20px;";

创建 messageBoxWrap,添加遮罩层

var messageBoxWrap = document.createElement("div");
messageBoxWrap.setAttribute("tabindex", "-1"); // 设置使其不能通过tab键聚焦
messageBoxWrap.className = messageBoxWrap_class + Date.now(); // 每次弹框的类名都是唯一的 // 使 messageBoxWrap 覆盖在顶层 window 上
messageBoxWrap.style = messageBoxWrap_style + ";width:" + topDocBody.clientWidth + "px;";

创建 messageBox,添加 header,content,footer

1、创建messageBox

var messageBox = document.createElement("div");
messageBox.className = "messageBox";
messageBox.style = messageBox_style;

2、创建 header

//创建 header
var header = document.createElement("div");
header.className = "messageBox_header";
header.style = header_style;
var headerText = document.createElement("div");
headerText.textContent = "提示";
var headerBtn = document.createElement("div");
headerBtn.style = "cursor:pointer;text-align:right;";
headerBtn.textContent = "x";
headerBtn.onclick = function () {
mainFrameSetParent.removeChild(messageBoxWrap);
enableKeyboard();
};

当点击头部的关闭按钮时,在顶层文档对象(mainFrameSetParent)中移除弹框。这里 enableKeyboard

表示移除弹框后恢复键盘事件。因为有时候业务操作飞快,有时候因为电脑卡,弹框还没完全关闭,使用键盘操作去到了其他页面,导致弹框显示在了其他页面。

3、创建 content 包裹层

//创建 content 包裹层
var contentBox = document.createElement("div");
contentBox.className = "messageBox_content";
contentBox.style = content_style;
contentBox.textContent = paramString;

4、创建 footer

这里如果使 confirm 框,则多一个取消按钮

//创建 footer
var footer = document.createElement("div");
footer.className = "messageBox_footer";
footer.style = footer_style;
var yesBtn = document.createElement("button");
yesBtn.style = button_style + button_primary;
yesBtn.textContent = type === "confirm" ? "确认" : "确定";
yesBtn.onclick = function () {
if (type === "alert") {
if (currentAlertBoxCount > 0) {
currentAlertBoxCount--;
}
}
resolve(true);
mainFrameSetParent.removeChild(messageBoxWrap);
enableKeyboard(); // 关闭弹框后恢复键盘事件
};
if (type === "confirm") {
var noBtn = document.createElement("button");
noBtn.style =
button_style + "margin-left: 10px;border-radius: 20px 20px;";
noBtn.textContent = "取消";
noBtn.onclick = function () {
resolve(false);
mainFrameSetParent.removeChild(messageBoxWrap);
enableKeyboard(); // 关闭弹框后恢复键盘事件
};
}
footer.appendChild(yesBtn);
if (type === "confirm") {
footer.appendChild(noBtn);
}

5、将创建的弹框插入顶层文档

创建弹框时,使焦点聚焦到弹框,并且禁用键盘事件。

messageBox.appendChild(header);
messageBox.appendChild(contentBox);
messageBox.appendChild(footer);
messageBoxWrap.appendChild(messageBox);
//将创建好的弹框插入顶层 document
mainFrameSetParent.appendChild(messageBoxWrap);
messageBoxWrap.focus();
disableKeyboard();

6、页面调用弹出框

设置 currentAlertBoxCount 代表 alert 框的数量,避免同时出现多个弹框

var currentAlertBoxCount = 0;
async function modernConfirm(str) {
return await showMessagebox("confirm", str);
}
async function modernAlert(str) {
if(currentAlertBoxCount>0){
return
}
currentAlertBoxCount++;
return await showMessagebox("alert", str);
}

完整代码

/*弹框的默认样式;*/
var messageBoxWrap_class = "modernMessagebox";
var messageBoxWrap_style =
"width:100%;height:100%;position:fixed;top:0;left:0;text-align:center;";
var messageBox_style =
"user-select:none;display: inline-block;width: 420px;padding: 10px 0px 20px;vertical-align: middle;background-color: #fff;border-radius: 4px;border: 1px solid #ebeef5;font-size:18px;box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);text-align: left;";
var header_style =
"padding: 10px 16px 10px;font-size:14px;display:flex;justify-content: space-between;";
var content_style = "padding: 10px 15px;color: #606266;font-size: 14px;";
var footer_style = "padding: 5px 15px 0;text-align: right;";
var button_style =
"display: inline-block;line-height: 1;wite-space: nowrap;cursor: pointer;background: #fff;border: 1px solid #dcdfe6;color: #606266;-webkit-appearance: none;text-align:center;box-sizing:border-box;outline:0;margin:0;transition:.1s;font-weight:500;padding:10px 16px;font-size:12px;border-radius:3px;";
var button_primary =
"color: #fff;background: rgb(11, 87, 208);border: 1px solid rgb(11, 87, 208);border-radius: 20px 20px;";
var currentAlertBoxCount = 0;
//弹框时禁用键盘事件,弹框消失后启用键盘事件,防止弹框还未消失,使用 tab+enter去了其他页面的情况
let keyboardDisabled = false;
function disableKeyboard() {
keyboardDisabled = true;
document.addEventListener("keydown", handleKeyDown);
}
function enableKeyboard() {
keyboardDisabled = false;
document.removeEventListener("keydown", handleKeyDown);
}
function handleKeyDown(event) {
if (keyboardDisabled) {
event.preventDefau1t();
}
}
// type 区分是 alert 还是 confirm
// paramSting 表示提示框的内容,项目中没有自定义标题的需求,所以这里提示框都用统一的标题
function showMessagebox(type, paramString) {
//获取项目顶层的document,弹框显示需要覆盖顶层页面
var topDoc = window.top.document;
var mainFrameSet = topDoc.querySelector("#setMain");
var mainFrameSetParent = mainFrameSet.parentNode;
var topDocBody = window.top.document.body;
//创建弹框盒子,添加遮罩层
return new Promise((resolve, reject) => {
var messageBoxWrap = document.createElement("div");
messageBoxWrap.setAttribute("tabindex", "-1");
messageBoxWrap.className = messageBoxWrap_class + Date.now();
messageBoxWrap.style = messageBoxWrap_style + ";width:" + topDocBody.clientWidth + "px;";
var messageBox = document.createElement("div");
messageBox.className = "messageBox";
messageBox.style = messageBox_style;
//创建 header
var header = document.createElement("div");
header.className = "messageBox_header";
header.style = header_style;
var headerText = document.createElement("div");
headerText.textContent = "提示";
var headerBtn = document.createElement("div");
headerBtn.style = "cursor:pointer;text-align:right;";
headerBtn.textContent = "x";
headerBtn.onclick = function () {
mainFrameSetParent.removeChild(messageBoxWrap);
enableKeyboard();
};
header.appendChild(headerText);
header.appendChild(headerBtn);
//创建 content 包裹层
var contentBox = document.createElement("div");
contentBox.className = "messageBox_content";
contentBox.style = content_style;
contentBox.textContent = paramString;
//创建 footer
var footer = document.createElement("div");
footer.className = "messageBox_footer";
footer.style = footer_style;
var yesBtn = document.createElement("button");
yesBtn.style = button_style + button_primary;
yesBtn.textContent = type === "confirm" ? "确认" : "确定";
yesBtn.onclick = function () {
if (type === "alert") {
if (currentAlertBoxCount > 0) {
currentAlertBoxCount--;
}
}
resolve(true);
mainFrameSetParent.removeChild(messageBoxWrap);
enableKeyboard();
};
if (type === "confirm") {
var noBtn = document.createElement("button");
noBtn.style =
button_style + "margin-left: 10px;border-radius: 20px 20px;";
noBtn.textContent = "取消";
noBtn.onclick = function () {
resolve(false);
mainFrameSetParent.removeChild(messageBoxWrap);
enableKeyboard();
};
}
footer.appendChild(yesBtn);
if (type === "confirm") {
footer.appendChild(noBtn);
} messageBox.appendChild(header);
messageBox.appendChild(contentBox);
messageBox.appendChild(footer);
messageBoxWrap.appendChild(messageBox);
//将创建好的弹框插入顶层 document
mainFrameSetParent.appendChild(messageBoxWrap);
messageBoxWrap.focus();
disableKeyboard();
});
}
async function modernConfirm(str) {
return await showMessagebox("confirm", str);
}
async function modernAlert(str) {
if(currentAlertBoxCount>0){
return
}
currentAlertBoxCount++;
return await showMessagebox("alert", str);
}

改造jsp项目的alert框和confirm框的更多相关文章

  1. jquery仿alert提示框、confirm确认对话框、prompt带输入的提示框插件[附实例演示]

    jquery仿alert提示框.confirm确认对话框.prompt带输入的提示框插件实例演示 第一步:引入所需要的jquery插件文件: http://www.angelweb.cn/Inc/eg ...

  2. 如何在我自己的web 项目的jsp页面中添加链接,直接让别人通过内网在我的电脑上下载文件

    今天接到一个任务,将昨天年会的视频,音频,图片等放在公司自己的服务器上,使连接同一个路由器的(即同一个内网)的同事可以通过内网下载视频(通过内网下载,可以提高下载速度). 备注:本次用的是tomcat ...

  3. 一个电商项目的Web服务化改造

    一个电商项目的Web服务化改造 项目,早期是随便瞎做的,没啥架构,连基本的设计也没. 有需求,实现需求,再反复修改. 大致就是这么做的. 最近,项目要重新架构,和某boss协商的结果是,采用阿里开源的 ...

  4. 为什么项目的jar包会和tomcat的jar包冲突?

    为什么项目的jar包会和tomcat的jar包冲突? 碰到这个问题,猜测tomcat启动时会将自己的lib和项目的lib在逻辑上归并为一个大的lib,但是并没有做版本区分以及去重,这样相同的包可能就有 ...

  5. Eclipse中修改Web项目的URL访问路径

    背景 访问路径,也就是指在浏览器中访问该web系统时的根路径,比如http://localhost:8080/xxxx/index.jsp  这里的xxxx,也就是request.getContext ...

  6. Unity中加入Android项目的Build步骤

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简介: 有的项目需要在Android中加入Unity功能,例如ANDROID应用中嵌入Un ...

  7. Eclipse 启动Tomcat后web项目的classes的子文件夹中没有calss文件

    Eclipse 启动Tomcat后web项目的classes的子文件夹中没有calss文件. 经网上查得以下方法可解决 把properties属性里的java compiler-->buildi ...

  8. 干货|人人都是翻译项目的Master

    在平时的工作中,我们都会经常查阅一些英文文档来解决平时遇到的问题和拓宽视野.看到好的文章或者书籍有没有想要和小伙伴分享的冲动,那么我们一起来翻译吧- 翻译主张 "信 达 雅" .& ...

  9. Tomcat下使用C3P0配置JNDI数据源(在项目的META-INF目录下创建context.xml的文件)

    一.C3P0下载 C3P0下载地址:http://sourceforge.net/projects/c3p0/files/?source=navbar 下载完成之后得到一个压缩包

  10. Android项目的targetSDK>=23,在低于Android6.0的部分测试机(类似华为)上运行时出现的系统权限问题

    相信大家对Android6.0以上的动态权限已经有所了解,很多童鞋也已经跃跃欲试地将自己项目的targetSDK升级到了23及其以上,很不幸的是我也成为了其中一员,然而我还是图样图森破了,升级之后的问 ...

随机推荐

  1. Ubuntu安装GPU驱动+CUDA+cuDNN的安装方法

    一台有GPU的虚拟机如果没有安装CUDA的驱动,是需要我们手动去进行安装的,介绍Ubuntu操作系统的安装教程. 1. 下载安装文件 NVIDIA CUDA Toolkit Archive 点击上面链 ...

  2. Tampermonkey 油猴脚本中文手册(出处:https://www.itblogcn.com/article/2233.html)

    文章目录 @name @namespace @copyright @version @description @icon, @iconURL, @defaulticon @icon64, @icon6 ...

  3. 2. RabbitMQ 的详细安装步骤(两种方式,第一种:yum 安装;第二种:docker 容器安装)

    2. RabbitMQ 的详细安装步骤(两种方式,第一种:yum 安装:第二种:docker 容器安装) @ 目录 2. RabbitMQ 的详细安装步骤(两种方式,第一种:yum 安装:第二种:do ...

  4. springboot和jdk的版本对应关系

    spring-boot和jdk的版本对应关系 SpringBoot Versions JDK Versions 0.0 -1.1 6+(6 or higher) 1.2 - 1.5 6 - 7 2.0 ...

  5. FREERTOS_LWIP TcpServer 加快接收速度

    刚开始调试时,关注点都在接收缓存等参数上,接受的间隔上限时钟在250ms左右. 后来发现是其中一个参数的设定决定了接收的速度,调整参数后,可以达到每80ms接收1024个字节. 发文留存,备忘.

  6. .net WorkFlow 流程转办

    WikeFlow官网:www.wikesoft.com WikeFlow学习版演示地址:workflow.wikesoft.com WikeFlow学习版源代码下载:https://gitee.com ...

  7. Ruby+Selenium+testunit web自动化demo

    1.安装对应库 使用RubyMine新建项目打开终端安装对应库 gem install selenium-webdriver gem install test-unit 如果安装不成功,请切换到国内源 ...

  8. K8s新手系列之Pod的重启策略

    概述 K8s中Pod的重启策略具有确保服务连续性.保证任务完整性.提升资源利用效率.便于故障排查的作用 Pod的重启策略可以根据restartPolicy字段定义. 重启策略适用于pod对象中的所有容 ...

  9. PLSQL中&符号处理

    在SQL语句中的字符串中出现了&符号,当执行的时候会被认为是参数需要传递,如update product set brand = 'D&G'; 解决办法是把语句改为:update pr ...

  10. 第一章 Kafka 配置部署及SASL_PLAINTEXT安全认证

    系列文章目录 第一章 Kafka 配置部署及SASL_PLAINTEXT安全认证 第二章  Spring Boot 整合 Kafka消息队列 生产者 第三章  Spring Boot 整合 Kafka ...