重写原生alert,弹出层过一会就消失

本文介绍了一种使用JavaScript自定义实现网页弹窗的方法,包括创建遮罩层和弹窗对话框,设置样式以增强用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

window.alert = function(str) {
if(document.querySelectorAll("div.shieldClass").length!=0){
return false;
}
// 遮罩层
var shield = document.createElement("DIV");
shield.className = "shieldClass";
shield.id = "shield";
shield.style.position = "absolute";
shield.style.left = "0px";
shield.style.top = "0px";
shield.style.width = "100%";
shield.style.height = "100%";
//弹出对话框时的背景颜色
shield.style.background = "#111";
shield.style.textAlign = "center";
shield.style.zIndex = "25000";
shield.style.opacity = "0.4";
//背景透明 IE有效
var alertFram = document.createElement("DIV");
alertFram.className = "alertFramClass";
alertFram.id = "alertFram";
alertFram.style.position = "absolute";
alertFram.style.left = "50%";
alertFram.style.top = "40%";
alertFram.style.background = "rgba(0,0,0,0.7)";
alertFram.style.textAlign = "center";
alertFram.style.zIndex = "25001";
alertFram.style.borderRadius="6px";
strHtml = "<p style='text-align:center;padding:15px 15px;font-size:12px;font-weight: normal;color:#fff'>" + str +"</p>"
alertFram.innerHTML = strHtml;
document.body.appendChild(alertFram);
document.body.appendChild(shield);
var o = document.getElementById("alertFram");
var body=document.getElementsByTagName("body")[0]
var w = o.offsetWidth; //宽度
o.style.marginLeft="-"+w/2+"px";
setTimeout(function(){
var shieldDom=document.querySelectorAll("div.shieldClass");
var alertFramDom=document.querySelectorAll("div.alertFramClass");
for(var i=0;i<shieldDom.length;i++){
body.removeChild(shieldDom[i]);
};
for(var j=0;j<alertFramDom.length;j++){
body.removeChild(alertFramDom[j]);
};
},1000);

}

转载于:https://www.cnblogs.com/binmengxue/p/9639171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值