用CSS和js实现模态框

40 篇文章 0 订阅
28 篇文章 1 订阅

用CSS和JS改变z-index的属性值就可以实现模态框

<html>
<head>
	<title>modal box</title>
<style type="text/css">
*{
	margin:0px;
	padding:0px;
}
</style>
</head>
<body>
<!--先在CSS里面把zindex的值设为负值让其在背景图片后面-->
<div style="background:url(http://pic.90sjimg.com/back_pic/00/04/27/49/5b1eee8bdba7b9aefc62fccafe72737c.jpg);width:100%;height:800px;z-index:1;">
	<button  id="modal-box">弹框</button>
	<div id="modal-show" style="position:fixed;z-index:-9999;width:100%;height:100%;background-color:rgba(236, 214, 214, 0.2);">	
	</div>
	<div id="modal-show2" style="position:fixed;z-index:-9999;width:30%;height:30%;margin:200px auto;border:1px solid red;">	
			欢迎你登录
	</div>

</div> 

<script type="text/javascript">   
document.getElementById("modal-box").οnclick=function()//点击按钮改变zIndex的值为1让模态框在背景图的前面
{
	document.getElementById("modal-show").style.zIndex = "1";
	document.getElementById("modal-show2").style.zIndex = "1";
};

document.getElementById("modal-show").οnclick=function()//点击模态框的透明背景时,模态框的zIndex值变为-9999,会隐藏在
{								背景图片的后面,点击模态框本身是不会消失的

	this.style.zIndex = "-9999";
	document.getElementById("modal-show2").style.zIndex = "-9999";
};


</script>

</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值