window.alert弹窗自定义

该代码实现了一个自定义的JavaScript弹窗功能,通过创建元素、设置样式和事件监听来展示和关闭提示框。弹窗数组`alert_list`用于管理多个弹窗,确保始终有一个弹窗显示。此外,使用计时器监控弹窗数组状态,当数组为空时清除计时器。
//定义弹窗数组
var alert_list = [];

/** 
 * 函数说明
 * @function function(text,title) 重置提示框
 * @param text{String} 要提示的文本内容
 * @param title{String} 提示框的标题
 * @version 2.0
 * @example alert("程序结束!!!","网页提示")
 */
window.alert = function(text, title = "网页的提示!!!") {
	var alert_box = document.createElement('div');
	var alert_title = document.createElement('h4');
	var alert_text = document.createElement('div');
	var alert_btn = document.createElement('button');

	//alert弹窗整体样式
	alert_box.style.width = "450px";
	alert_box.style.height = "fit-content";
	alert_box.style.position = "fixed";
	alert_box.style.zIndex = "199";
	alert_box.style.left = "50%";
	alert_box.style.top = "50%";
	alert_box.style.transform = "translate(-50% ,-50%)";
	alert_box.style.backgroundColor = "#fff";
	alert_box.style.padding = "10px";
	alert_box.style.border = "2px solid #C4C4C4";
	alert_box.style.borderRadius = "5px";
	alert_box.style.fontFamily = "'Courier New', Courier, monospace";
	alert_box.style.display = "none";

	//alert 标题样式
	alert_title.style.padding = "0";
	alert_title.style.margin = "0";
	alert_title.style.fontWeight = "600";
	alert_title.innerText = title;
	// alert_title.innerText = "挽月阁";

	//内容文本样式
	alert_text.style.margin = "10px 0";
	alert_text.style.lineHeight = "1.35";
	alert_text.innerText = text;
	// alert_text.innerText = "落霞与孤鹜齐飞,秋水共长天一色";

	//ok按钮
	alert_btn.style.display = "block";
	alert_btn.style.width = "60px";
	alert_btn.style.height = "30px";
	alert_btn.style.lineHeight = "27px";
	alert_btn.style.padding = "2px";
	alert_btn.style.float = "right";
	alert_btn.style.backgroundColor = "#17BBF2";
	alert_btn.style.border = "1px solid #17BBF2";
	alert_btn.style.borderRadius = "2px";
	alert_btn.style.backgroundClip = "content-box";
	// alert_btn.style.transform = "scale(0.8)";
	alert_btn.style.fontWeight = "600";
	alert_btn.style.fontFamily = "'Courier New', Courier, monospace";
	alert_btn.style.fontSize = "15px";
	alert_btn.innerText = "确定";

	//添加ok事件
	alert_btn.addEventListener("click", function() {
		//将弹框移除数组并从body中删除弹窗
		alert_list.shift();
		alert_box.remove();
	});

	//添加弹窗到弹窗数组
	alert_list.push(alert_box);

	//向页面中添加元素
	document.body.appendChild(alert_box);
	alert_box.appendChild(alert_title);
	alert_box.appendChild(alert_text);
	alert_box.appendChild(alert_btn);
};

/*
定义计时器,判断弹窗数组是否为空
确保弹窗数组中的第一个弹窗一直是显示状态
当数组为空的时候清除计时器
*/
var yue = setInterval(function() {
	if (alert_list.length != 0) {
		alert_list[0].style.display = "block";
	} else {
		clearInterval(yue);
	}
}, 100);

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lx挽月.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值