javascript - 创建带有空白背景屏幕的警报弹出窗口
问题描述
我需要显示一个警告框,同时覆盖我的 HTML 页面上的内容。基本上,当我的警报弹出窗口出现时,我希望有一个空白或白色背景。我尝试了类似下面的方法,但不起作用。
if (something happens) {
changeBackgroundColor();
if (alert("My alert box")){
} else {
//Return to previous page
window.history.go(-1);
}
}
..
....
.....
function changeBackgroundColor() {
document.body.style.backgroundColor = "white";
}
解决方案
更改背景颜色不会隐藏页面上的任何元素。您很可能需要一个叠加层,它可以是一个简单的 div 样式,如下所示:
.overlay {
position:fixed;
top:0;
left:0;
right:0;
bottom:0;
z-index:999; /* adjust the z-index as you need it */
background:#fff;
display:none;
}
然后,您可以在警报之前显示叠加层并在之后隐藏它
function toggleOverlay( show ){
document.querySelector('.overlay').style.display = (show === true) ? 'block' : 'none';
}
// ...
toggleOverlay(true);
// Kudos to Kobe for pointing out that the alert is triggered before the repaint
// simple way to solve the problem is a timeout, which will
// make the browser paint the changes before the alert is triggered
setTimeout(function(){
alert('Something');
toggleOverlay(false);
}, 0);
这是一个有效的 jsFiddle:https ://jsfiddle.net/UsernamesSuck/cpdrtgb8/2/
推荐阅读
- ruby - 是什么意思?
- javascript - 如何在javascript中处理输入参数
- java - 某些网站图像未显示在 android 的 webview 中
- regex - 去掉最外面的括号
- java - JPA:'CascadeType.REMOVE' 或 'orphanRemoval = true',在 an:n 关系中使用,生成带有 EmbeddeId 类的新表/类?
- javascript - Froala VUE v3 不显示工具栏按钮
- returnn - RETURNN Librispeech 任务:为 LM 和编码器-解码器模型重用预训练模型的参数
- java - 如何使用大值格式化程序设置自定义后缀
- django - EC2 和 RDS 上的 Django 应用程序(服务器是否在主机“localhost”(127.0.0.1)上运行并接受端口 5432 上的 TCP/IP 连接?)
- angular - 如果强度不是 100,则 Angular 密码强度扩展显示错误