自制弹窗页面

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Modal</title>
  <style>
      #btn {
          margin: 10px;
        }
     .overlay {
          display: none;
          position: fixed;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          height: 100%;
          width:  100%;
          background: rgba(0, 0, 0, 0.5);
          z-index: 99;
        }

        .modal {
          width:  500px;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          border: 1px solid #ccc;
          border-radius: 6px;
          background-color: #fff;
        }

        .modal>h2 {
          border-bottom: 1px solid #ccc;
          margin: 0;
          line-height: 2;
          padding-left: 20px;
        }

        .modal>.detail {
          padding:  0 20px;
          height: 100px;
          border-bottom: 1px solid #ccc;
        }

        .modal>.btn {
          line-height: 2.2;
          cursor: pointer;
          float: right;
          margin: 0 10px;
        }
  </style>
</head>
<body>
    <input id="btn" type="button" value="点我">
    <div class="overlay">
      <div class="modal">
        <h2>我是标题</h2>
        <div class="detail">
          <p>我是内容</p>
          <p>我是内容</p>
        </div>
        <span class="btn close">确定</span>
        <span class="btn cancel">取消</span>
      </div>
    </div>
    <script>
        var btn = document.getElementById('btn'),
                overlay = document.getElementsByClassName('overlay')[0],
                modal = document.getElementsByClassName('modal')[0];
        btn.addEventListener('click',function(e){
            e.stopPropagation();
            overlay.setAttribute('style','display:block');
        });
        document.body.addEventListener('click',function(e){
            overlay.setAttribute('style','display:none');
        });
        modal.addEventListener('click',function(e){
            e.stopPropagation();
            if(e.target.classList.contains('close') || e.target.classList.contains('cancel')){
                overlay.setAttribute('style','display:none');
            }
        });
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值