美文网首页
vue 封装自定义全局弹窗组件 confirm

vue 封装自定义全局弹窗组件 confirm

作者: 可乐_加冰_ | 来源:发表于2022-07-15 16:22 被阅读0次
    • 第一步,创建 /components/confirmationBox.vue
    <template>
        <div class="confirmationBox" v-if="isShow">
          <div class="mask-div"></div>
          <div class="out-line-box animate__animated animate__zoomIn">
            <div class="confirmation-content-box">
              <!-- 左上角盒子  -->
              <section class="left-top-corner">
                <div class="div1"></div>
                <div class="div2"></div>
              </section>
              <!-- 右下角 -->
              <section class="right-bottom-corner">
                <div class="div1"></div>
                <div class="div2"></div>
              </section>
    
              <!-- 标题和描述 -->
              <div class="confirmation-title">{{ text.title }}</div>
              <div class="confirmation-msg">{{ text.msg }}</div>
    
              <!-- 底部两个按钮 -->
              <div class="btn-back-and-ok">
                <!-- 返回按钮 -->
                <div class="go-back-btn-out-line" @click="cancel">
                  <div class="go-back-btn">
                    <section class="back-btn-left-top-corner">
                      <div class="div1"></div>
                      <div class="div2"></div>
                    </section>
    
                    <section class="back-btn-right-bottom-corner">
                      <div class="div1"></div>
                      <div class="div2"></div>
                    </section>
    
                    <span style="position: relative; top: -2px;">{{  text.btn.cancelVal }}</span>
                  </div>
                </div>
    
    
                <!-- 确认按钮 -->
                <div class="ok-btn-out-line" @click="confirm">
                <div class="ok-btn">
    
                  <section class="ok-btn-left-top-corner">
                    <div class="div1"></div>
                    <div class="div2"></div>
                  </section>
    
                  <section class="ok-btn-right-bottom-corner">
                    <div class="div1"></div>
                    <div class="div2"></div>
                  </section>
    
                  <span style="position: relative; top: -2px;">{{  text.btn.confirmVal }}</span>
                </div>
                </div>
              </div>
            </div>
          </div>
        </div>
    </template>
    
    <script>
    export default {
      name: "confirmationBox",
      data(){
        return{
          isShow: true,
          text: {
            title: "提示",
            msg: "描述信息!",
            cancelBtn: true,
            confirmBtn: true,
            btn: {
              confirmVal: "确定",
              cancelVal: "取消"
            }
          }
        }
      },
      mounted() {
        // this.isShow = false;
      },
      methods:{
        //取消
        cancel() {
          this.isShow = false;
        },
        //确认
        confirm() {
          this.isShow = false;
        },
    
      }
    }
    </script>
    
    <style scoped>
    .confirmationBox{
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
    }
    
    .mask-div{
      position: absolute;
      width: 100%;
      height: 100%;
      /*opacity: .5;*/
      /*background: #161a1c;*/
      background: #161a1cde;
      z-index: 9999999;
    }
    
    /*外边框*/
    .out-line-box{
      box-shadow:  0 0 10px 2px #63c6ffa1;
      height: 300px;
      width: 600px;
      margin: auto;
      border-top-left-radius: 7px;
      border-bottom-right-radius: 7px;
      z-index: 9999999;
      position: relative;
      top: calc((100% - 300px) / 2);
    
    }
    /*内容*/
    .confirmation-content-box{
      height: 100%;
      width: 100%;
      border: 3px solid #63c6ff;
      border-top-left-radius: 7px;
      border-bottom-right-radius: 7px;
      box-shadow: 0 0 15px 6px #63c6ffa1 inset;
    }
    
    .confirmation-title{
      color: #63c6ff;
      font-size: 24px;
      position: relative;
      top: 30px;
      text-align: center;
      letter-spacing: 2px;
    }
    
    .confirmation-msg{
      color: #fff;
      font-size: 26px;
      position: relative;
      top: 60px;
      text-align: center;
      letter-spacing: 1px;
    }
    
    /*返回、确认按钮--*/
    .btn-back-and-ok{
      position: absolute;
      bottom: 60px;
      width: 100%;
      font-size: 18px;
      text-align: center;
      color: #fff;
    }
    
    .go-back-btn-out-line:hover{
      background: #c767605c;
      transition: 0.5s all;
      cursor: pointer;
    }
    .go-back-btn-out-line{
      box-shadow:  0 0 1px 1px #b7554d;
      height: 40px;
      width: 180px;
      margin: auto;
      border-top-left-radius: 7px;
      border-bottom-right-radius: 7px;
      z-index: 9999999;
      position: relative;
      left: -15px;
      display: inline-block;
      line-height: 40px;
    }
    .go-back-btn{
      height: 100%;
      width: 100%;
      border: 2px solid #e06157;
      border-top-left-radius: 7px;
      border-bottom-right-radius: 7px;
      box-shadow: 0 0 8px 1px #b7554d inset;
      letter-spacing: 3px;
      color: #c76760;
    }
    .back-btn-left-top-corner{
      display: inline-block;
      height: auto;
      position: absolute;
      top: 0;
      left: 2px;
    }
    .back-btn-left-top-corner .div1{
      height: 0;
      display: inline-block;
      width: 10px;
      border-bottom: 3px solid #e06157;
      border-left: 2px solid transparent;
      border-right: 2px solid transparent;
      transform: rotateZ(90deg);
      position: absolute;
      top: 10px;
    }
    .back-btn-left-top-corner .div2{
      height: 0;
      display: inline-block;
      width: 10px;
      border-bottom: 3px solid #e06157;
      border-left: 2px solid transparent;
      border-right: 2px solid transparent;
      transform: rotateZ(180deg);
      position: absolute;
      top: 5px;
      left: 6px;
    }
    .back-btn-right-bottom-corner{
      display: inline-block;
      height: auto;
      position: absolute;
      bottom: 0;
      right: 2px;
      transform: rotateZ(180deg);
    }
    .back-btn-right-bottom-corner .div1{
      height: 0;
      display: inline-block;
      width: 10px;
      border-bottom: 3px solid #e06157;
      border-left: 2px solid transparent;
      border-right: 2px solid transparent;
      transform: rotateZ(90deg);
      position: absolute;
      top: 10px;
    }
    .back-btn-right-bottom-corner .div2{
      height: 0;
      display: inline-block;
      width: 10px;
      border-bottom: 3px solid #e06157;
      border-left: 2px solid transparent;
      border-right: 2px solid transparent;
      transform: rotateZ(180deg);
      position: absolute;
      top: 5px;
      left: 6px;
    }
    
    
    .ok-btn-out-line:hover{
      background: #56a9db54;
      transition: 0.5s all;
      cursor: pointer;
    }
    .ok-btn-out-line{
      box-shadow:  0 0 1px 1px #63c6ffa1;
      height: 40px;
      width: 180px;
      margin: auto;
      border-top-left-radius: 7px;
      border-bottom-right-radius: 7px;
      z-index: 9999999;
      position: relative;
      left: 15px;
      display: inline-block;
      line-height: 40px;
    }
    .ok-btn{
      height: 100%;
      width: 100%;
      border: 2px solid #63c6ff;
      border-top-left-radius: 7px;
      border-bottom-right-radius: 7px;
      box-shadow: 0 0 8px 1px #63c6ffa1 inset;
      letter-spacing: 3px;
      color: #56a9db;
    }
    .ok-btn-left-top-corner{
      display: inline-block;
      height: auto;
      position: absolute;
      top: 0;
      left: 2px;
    }
    .ok-btn-left-top-corner .div1{
      height: 0;
      display: inline-block;
      width: 10px;
      border-bottom: 3px solid #63c6ff;
      border-left: 2px solid transparent;
      border-right: 2px solid transparent;
      transform: rotateZ(90deg);
      position: absolute;
      top: 10px;
    }
    .ok-btn-left-top-corner .div2{
      height: 0;
      display: inline-block;
      width: 10px;
      border-bottom: 3px solid #63c6ff;
      border-left: 2px solid transparent;
      border-right: 2px solid transparent;
      transform: rotateZ(180deg);
      position: absolute;
      top: 5px;
      left: 6px;
    }
    .ok-btn-right-bottom-corner{
      display: inline-block;
      height: auto;
      position: absolute;
      bottom: 0;
      right: 2px;
      transform: rotateZ(180deg);
    }
    .ok-btn-right-bottom-corner .div1{
      height: 0;
      display: inline-block;
      width: 10px;
      border-bottom: 3px solid #63c6ff;
      border-left: 2px solid transparent;
      border-right: 2px solid transparent;
      transform: rotateZ(90deg);
      position: absolute;
      top: 10px;
    }
    .ok-btn-right-bottom-corner .div2{
      height: 0;
      display: inline-block;
      width: 10px;
      border-bottom: 3px solid #63c6ff;
      border-left: 2px solid transparent;
      border-right: 2px solid transparent;
      transform: rotateZ(180deg);
      position: absolute;
      top: 5px;
      left: 6px;
    }
    
    /*返回、确认按钮end*/
    
    
    
    /*大盒子-左上角*/
    .left-top-corner{
      display: inline-block;
      height: auto;
      position: relative;
      top: 15px;
      left: 7px;
    }
    .left-top-corner .div1{
      height: 0;
      display: inline-block;
      width: 20px;
      border-bottom: 5px solid #63c6ff;
      border-left: 3px solid transparent;
      border-right: 3px solid transparent;
      transform: rotateZ(90deg);
      position: relative;
      top: 5px;
    }
    .left-top-corner .div2{
      height: 0;
      display: inline-block;
      width: 20px;
      border-bottom: 5px solid #63c6ff;
      border-left: 3px solid transparent;
      border-right: 3px solid transparent;
      transform: rotateZ(180deg);
      position: relative;
      top: -8px;
      left: -12px;
    }
    /*左上角end*/
    
    /*大盒子-右下角*/
    .right-bottom-corner{
      display: inline-block;
      height: auto;
      position: absolute;
      right: 7px;
      bottom: 15px;
      transform: rotateZ(180deg);
    }
    .right-bottom-corner .div1{
      height: 0;
      display: inline-block;
      width: 20px;
      border-bottom: 5px solid #63c6ff;
      border-left: 3px solid transparent;
      border-right: 3px solid transparent;
      transform: rotateZ(90deg);
      position: relative;
      top: 5px;
    }
    .right-bottom-corner .div2{
      height: 0;
      display: inline-block;
      width: 20px;
      border-bottom: 5px solid #63c6ff;
      border-left: 3px solid transparent;
      border-right: 3px solid transparent;
      transform: rotateZ(180deg);
      position: relative;
      top: -8px;
      left: -12px;
    }
    /*右下角end*/
    </style>
    
    
    • 第二步,创建 /assets/js/confirmMy.js
    //https://blog.csdn.net/qq_33235582/article/details/121144874
    import Vue from 'vue';
    import confirm from '../../components/confirmationBox';
    
    let confirmConstructor = Vue.extend(confirm);
    
    let theConfirm = function (text) {
      return new Promise((res, rej) => { //promise封装,ok执行resolve,no执行rejectlet
        let confirmDom = new confirmConstructor({
          el: document.createElement('div')
        })
        document.body.appendChild(confirmDom.$el);  //new一个对象,然后插入body里面
        confirmDom.text = Object.assign({},confirmDom.text, text);   //为了使confirm的扩展性更强,这个采用对象的方式传入,所有的字段都可以根据需求自定义
        confirmDom.confirm = function () {
          confirmDom.isShow = false;
          res("确认")
        }
        confirmDom.cancel = function () {
          confirmDom.isShow = false;
          rej("取消")
        }
    
      })
    }
    
    export default theConfirm;
    
    
    • 第三步 main.js 全局注册
    import confirmMy from './assets/js/confirmMy.js'
    Vue.prototype.$confirmMy = confirmMy;
    
    • 第四步 页面调用
    <template>
    <!-- 测试版-->
      <div class="page-div">
         <el-button @click="openConfirmationBox"> 点我弹窗 </el-button>
      </div>
    </template>
    
    <script>
    export default {
      name: "test",
      data(){
        return{
         
        }
      },
      methods:{
    
        openConfirmationBox(){
          this.$confirmMy({
            title: "退出",
            msg: "确定要退出吗?",
            btn: {
              confirmVal: "退出",
              cancelVal: "返回"
            }
          }).then((res) => {
            console.log("yes:", res);
          }).catch((err) => {
            console.log("no:", err);
          });
    
        },
      }
    }
    </script>
    

    相关文章

      网友评论

          本文标题:vue 封装自定义全局弹窗组件 confirm

          本文链接:https://www.haomeiwen.com/subject/iuccirtx.html