原生模拟element的alert的组件

247 阅读2分钟

项目完事稍微记录点东西,老项目有部分页面要复用,alert太难看,于是乎模拟下element的alert组件,瞬间好看多了,童鞋们可以参考下,不用使劲扒element了,哪些不满意可以再修整下

  • CSS部分 上代码:

  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .bigbox {
        display: none;
    }
    .mask {
    
      z-index: 2009;
      display: flex;
      justify-content: center;
      align-items: center;
      position: fixed;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
    }

    .model {
      display: none;
      position: fixed;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      z-index: 2001;
      background: #000;
      opacity: 0.5;
    }

    .box_header .before {
      position: absolute;
      right: 18px;
      top: 14px;
      display: block;
      content: '';
      width: 12px;
      height: 12px;
      background: url("./close.png") no-repeat center;
      background-size: 20px 20px;
      cursor: pointer;
    }

    .box {

      /* position: absolute;
        left: 50%;
        top: 50%;
        transform: translateX(-50%) translateY(-50%); */
      display: inline-block;
      width: 420px;
      padding-bottom: 10px;
      vertical-align: middle;
      background-color: #fff;
      border-radius: 4px;
      border: 1px solid #ebeef5;
      font-size: 18px;
      -webkit-box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
      text-align: left;
      overflow: hidden;
      -webkit-backface-visibility: hidden;
      backface-visibility: hidden;

    }

    .box_header {
      position: relative;
      padding: 15px 15px 10px;
    }

    .box_content {
      position: relative;
      padding: 10px 15px;
      color: #606266;
      font-size: 14px;
    }

    .box__title {
      padding-left: 0;
      margin-bottom: 0;
      font-size: 18px;
      line-height: 1;
      color: #303133;
    }

    .box__headerbtn {
      position: absolute;
      top: 15px;
      right: 15px;
      padding: 0;
      border: none;
      outline: 0;
      width: 16px;
      height: 16px;
      /* background: 0 0; */
      /* font-size: 16px; */
      /* cursor: pointer; */
    }

    .box_btn {
      text-align: right;
      margin-right: 13px;
      margin-top: 10px;
    }

    .btn {
      padding: 9px 15px;
      display: inline-block;
      line-height: 1;
      white-space: nowrap;
      cursor: pointer;
      background: #fff;
      border: 1px solid #dcdfe6;
      color: #606266;
      -webkit-appearance: none;
      text-align: center;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      outline: 0;
      margin: 0;
      -webkit-transition: .1s;
      transition: .1s;

      font-size: 14px;
      border-radius: 5px;
      color: #fff;
      background-color: #409eff;
      border-color: #409eff;
      font-size: 12px;
    }

    .btn:hover {
      background: #66b1ff;
      border-color: #66b1ff;
      color: #fff;
    }
  </style>
  • js部分
<body>
  <button class="btn2">点击按钮</button>
  <div class="bigbox">
    <div class="mask">
      <div class="box">
        <div class="box_header">
          <div class="box__title">
            <span>提示</span>
          </div>
          <i class="before" id="close"></i>
        </div>
        <div class="box_content">
          <div class="title"></div>
        </div>
        <div class="box_btn">
          <button class="btn">确定</button>
        </div>
      </div>
    </div>
  </div>
  <div class="model"></div>
  <script>
    let bigbox = document.querySelector('.bigbox');
    let close = document.querySelector('#close');
    let btn = document.querySelector('.btn')
    let title = document.querySelector('.title')
    let model = document.querySelector('.model')
    let btn2 = document.querySelector('.btn2')

    title.innerHTML = '在此输入内容'

    btn2.onclick = function () {
      bigbox.style.display = 'block';
      model.style.display = 'block';
    };
    close.onclick = function () {
      bigbox.style.display = 'none';
      model.style.display = 'none';
    };
    btn.onclick = function () {
      bigbox.style.display = 'none';
      model.style.display = 'none';

    };


  </script>
</body>

  • 关于水平垂直居中部分可以再优化下哈,本人懒的整了,又给套了个大盒子,代码多了,层次也多了,碰到懒人就将就用吧