javascript:自定义弹窗的写法

项目中经常出现广告弹窗,记录下简单弹框写法。

基本html结构:

<div class="popBox">
    <div class="popBox-mask"></div>
    <div class="popBox-content">
	<!-- 书写弹框内容 -->
    </div>
</div>
css:

.popBox-mask{
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    opacity: .6;
    z-index: 999;
    background-color: #000;
}
.popBox-content{
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    z-index: 999;
}
所有的弹框可以复用以上代码,各个弹框要加上唯一标识。具体demo如下:
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        /*通用*/
        .popBox-mask{
            position: fixed;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            opacity: .6;
            z-index: 999;
            background-color: #000;
        }
        .popBox-content{
            position: fixed;
            left: 50%;
            top: 50%;
            transform: translate(-50%,-50%);
            z-index: 999;
        }
        /*各个弹框下的样式可以自己更改*/
        #popBox-demo .popBox-content{
            width: 600px;
            height: 400px;
            background-color: #fff;
            text-align: center;
        }
        #popBox-demo p{
            line-height: 400px;
            margin: 0;
        }
        #popBox-demo .btn-close{
            position: absolute;
            top: 20px;
            right: 20px;
        }
    </style>
</head>
<body>
    <button οnclick="showPopBox()">出现弹框</button>
    <div class="popBox" id="popBox-demo" style="display:none">
        <div class="popBox-mask"></div>
        <div class="popBox-content">
            <p>很丑的例子</p>
            <a href="javascript:void(0)" class="btn-close" οnclick="hidePopBox()">关闭</a>
        </div>
    </div>
    <script>
        //显示弹框
        function showPopBox(){
            document.getElementById('popBox-demo').style.display = 'block';
        }
        //关闭弹框
        function hidePopBox(){
            document.getElementById('popBox-demo').style.display = 'none';
        }
    </script>
</body>
</html>
效果图:





  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值