JS封装遮罩

效果

在这里插入图片描述

/* 参数 */
/* 
args:
 {
	 title:'温馨提示',
	 content:'内容',
	 confirmFn:function(){
		 var blueDiv = document.createElement('div');
		 blueDiv.style.backgroundColor = "blue";
		 blueDiv.style.width = "300px";
		 blueDiv.style.height = "300px";
		 body.appendChild(blueDiv);
	 }, s
	 cancelFn:function(){
		 
	 }
	 
 }
 
 */

function llAlert(args){
	var zhezhao = document.createElement('div'); /* 创建div*/
	zhezhao.className = "zhezhao"; /* 创建class属性*/
	/* zhezhao 后追加内容 */
	zhezhao.innerHTML = `
	<div class="alert">
		<div class="header"><span class="title">`+args.title+`</span><span class="close">X</span></div>
		<div class="main">
			`+args.content+`
		</div>
		<div class="btnList">
			<div class="btn yes">确定</div>
			<div class="btn no">取消</div>
		</div>
	</div>
	`
	/* 找到父元素 */
	var body = document.querySelector('body');	
	body.appendChild(zhezhao);/* 在父元素上追加内容 */
	
	/* 获取close元素(关闭) */
	var closeDiv = document.querySelector(".close");
	/* 设置关闭 点击事件 */
	closeDiv.onclick = function(){
		body.removeChild(zhezhao); /* 移除*/
	}
	
	/* 点击确定添加蓝色div */
	/* 1/找到确定按钮 */
	var yesDiv = document.querySelector(".btn.yes");
	yesDiv.onclick = function(){
		if(args.confirmFn() == 'function'){
			args.confirmFn(); /* 添加函数*/
		}else{
			console.error("函数没有设置参数");
		}
		
		
		body.removeChild(zhezhao); /* 添加后移除*/		
	}
			
	/* 取消 */
	var noDiv = document.querySelector(".btn.no");
	noDiv.onclick = function(){
		if(typeof args.cancelFn() == 'function'){
			args.cancelFn(); /* 取消函数*/
		}else{
			console.error("函数没有设置参数");
		}
		
		body.removeChild(zhezhao);
	}
			
}
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/style.css"/>
		<script src="js/alert.js" type="text/javascript" charset="UTF-8"></script>
	</head>
	<body>
		
		<button type="button">是否跳转到百度页面</button>
		
		<script type="text/javascript">
			var button = document.querySelector("button");/* 获取button元素 */
			button.onclick = function(){
				llAlert({
					title:"警告!",
					content:"是否跳转百度页面",
					confirmFn:function(){
						location.href = "https://www.bilibili.com/";/* 跳转页面 */
					},
					cancelFn:function(){
						var h1 = document.createElement("h1");
						h1.innerHTML = "用户已取消跳转页面";
						document.body.appendChild(h1);
					}
				})
			}
		</script>
	</body>
</html>

*{
	margin : 0;
	padding: 0;
	box-sizing: border-box;
}

/* 最外层 */
.zhezhao{
	position: fixed; /* 固定*/
	left: 0;
	top: 0;
	height: 100vh;
	width: 100vw;
	background-color: rgba(0,0,0,0.5);
	display: flex;
	align-items: center; /* 垂直方向居中*/
	justify-content: center; /* 纵轴广告居中*/
}

/* 弹框 */
.alert{
	width: 600px;
	height: 400px;
	background-color: #fff;
	display: flex; 
	flex-direction: column; /* 内容垂直分布*/
}

/* 表头部 */
.alert>.header{
	height: 80px;
	line-height: 80px;
	border-bottom: 1px solid #ccc;
	display: flex;
	justify-content: space-between; /* 每行内容两边排齐*/
	padding:0px 30px;
}

.alert>.header>.title{
	font-size: 30px;
	font-weight: 900;
	color: #ccc;
}

/* 关闭 */
.alert>.header>.close:hover{
	color: red;
}

/* 主要内容:放文字 */
.alert>.main{
	flex: 1;  /* 占一份*/
	padding:30px;
	border-bottom: 1px solid #ccc;
}

/* 设置按钮总位置大小 */
.alert>.btnList{
	height:80px;
	display: flex;
	justify-content: center;
	align-items: center;
	padding:0 30px;
}
/* 按钮 */
.alert>.btnList>.btn{
	height: 60px;
	width:120px;
	text-align: center;
	line-height: 60px; 
	margin: 50px; /* 外边距*/
}

/* 设置按钮颜色 */
.alert>.btnList>.btn:nth-child(1){
	background-color: coral;
}

.alert>.btnList>.btn:nth-child(2){
	background-color: #999;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值