溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用vue怎么實現(xiàn)移動端模態(tài)框

發(fā)布時間:2021-06-02 17:42:36 來源:億速云 閱讀:161 作者:Leah 欄目:web開發(fā)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)使用vue怎么實現(xiàn)移動端模態(tài)框,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1-封裝模態(tài)框組件Mydialog (樣式可以自己寫)

<template>
 <transition name="modal" tag="div">
  <div class="modal" v-show="visible" transition="fade">
   <div class="modal-dialog">
    <div class="modal-content">
     <!--頭部-->
     <div class="modal-header">
      <slot name="header">
       <!-- <p class="title">{{modal.title}}</p> -->
      </slot>
      <a @click="close(0)" class="xd xd-close" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></a>
     </div>
     <!--內(nèi)容區(qū)域-->
     <div class="modal-body">
      <slot name="body">
      </slot>
     </div>
     <!--尾部,操作按鈕-->
     <div class="modal-footer">
      <slot name="footer">
       <slot name="button" class="footer">>
        <a v-if="modal.showCancelButton" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="button" :class="modal.cancelButtonClass" @click="close">{{modal.cancelButtonText}}</a>
        <a v-if="modal.showConfirmButton" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="button" :class="modal.confirmButtonClass" @click="close()">{{modal.confirmButtonText}}</a>
       </slot>
      </slot>
     </div>
    </div>
   </div>
  </div>
  <!-- <div v-show="show" class="modal-backup"></div> -->
 </transition>
</template>

<script>
export default {
 props: {
  visible: { type: Boolean, default: false },
  options: {
   type: Object,
   default: {}
  }
 },
 // 采用v-bind將visible傳入
 methods: {
  ConfirmHandler () {
   this.$emit('update:visible', false);  // 更新visible的值(可選,如果想點擊確認(rèn)之后,進(jìn)行一些處理再決定關(guān)不關(guān)閉,可取消在這里更新visible)
   this.$emit('Confirm');  // 傳遞確認(rèn)事件
  },
  CancelHandler () {
   this.$emit('update:visible', false);  // 更新visible的值
   this.$emit('Cancel');  // 傳遞取消事件
  },
  close () {
   this.visible = false;
  }
 },
 /**
  * modal 模態(tài)接口參數(shù)
  * @param {string} modal.title 模態(tài)框標(biāo)題
  * @param {boolean} modal.showCancelButton 是否顯示取消按鈕
  * @param {string} modal.cancelButtonClass 取消按鈕樣式
  * @param {string} modal.cancelButtonText 取消按鈕文字
  * @param {string} modal.showConfirmButton 是否顯示確定按鈕
  * @param {string} modal.confirmButtonClass 確定按鈕樣式
  * @param {string} modal.confirmButtonText 確定按鈕標(biāo)文字
  */
 computed: {
  /**
   * 格式化props進(jìn)來的參數(shù),對參數(shù)賦予默認(rèn)值
   */
  modal () {
   let modal = this.options;
   if (modal) {
    modal = {
     title: modal.title || '提示',
     showCancelButton: typeof modal.showCancelButton == 'undefined' ? true : modal.showCancelButton,
     cancelButtonClass: modal.cancelButtonClass ? modal.showCancelButton : 'btn-default',
     cancelButtonText: modal.cancelButtonText ? modal.cancelButtonText : '取消',
     showConfirmButton: typeof modal.showConfirmButton == 'undefined' ? true : modal.cancelButtonClass,
     confirmButtonClass: modal.confirmButtonClass ? modal.confirmButtonClass : 'btn-active',
     confirmButtonText: modal.confirmButtonText ? modal.confirmButtonText : '確定',
    };
   } else { // 使用時沒有傳遞參數(shù)
    modal = {
     title: '提示',
     showCancelButton: true,
     cancelButtonClass: 'btn-default',
     cancelButtonText: '取消',
     showConfirmButton: true,
     confirmButtonClass: 'btn-active',
     confirmButtonText: '確定',
    };
   }
   return modal;
  },
 },

}
</script>

<style lang="scss" scoped>
.modal-enter-active {
 animation: modal-in 0.35s linear;
}

.modal-leave-active {
 animation: modal-in 0.35s reverse linear;
}

@keyframes modal-in {
 0% {
  transform: translateY(-20px) rotateX(-35deg);
  opacity: 0;
 }
 50% {
  opacity: 0.5;
 }
 100% {
  transform: translateY(0) rotateX(0);
  opacity: 1;
 }
}

.modal {
 width: 100%;
 height: 100%;
 position: fixed;
 left: 0;
 top: 0;
 right: 0;
 bottom: 0;
 z-index: 1001;
 outline: 0;
 overflow: hidden;
 background-color: rgba(0, 0, 0, 0.8);
}

.modal-dialog {
 position: absolute;
 left: 50%;
 top: 50%;
 transform: translate(-50%, -50%);
 width: auto;
 width: 608px;
 background: #fff;
 border-radius: 20px;
 box-shadow: 0 8px 24px 7px rgba(0, 0, 0, 0.11);
 z-index: 1002;
 overflow: hidden;

 .modal-content {
  > div {
   // padding: 0.15rem 0.4rem;
  }
  .modal-header {
   background: url("../assets/images/tournaments/1.png") no-repeat;
   background-size: cover;
   height: 70px;
   img {
    width: 100%;
   }
  }
  .modal-body {
   // padding: 90px 0 72px 0;
   color: #3c3c43;
   font-size: 25px;
   border-bottom: 1px solid #bdbdbd;
   font-weight: 500;
  }
  .footer {
   a {
    font-size: 30px;
    color: #2c2c2c;
   }
  }
  .modal-footer {
   padding: 30px 0 40px 0;
   color: #3c3c43;
   font-size: 30px;
   font-weight: 500;
  }
 }
}

.modal-backup {
 width: 100%;
 height: 100%;
 position: fixed;
 top: 0;
 right: 0;
 bottom: 0;
 left: 0;
 z-index: 1000;
 background: rgba(0, 0, 0, 0.5);
}
</style>

2-使用方法1

頁面中引入在進(jìn)行調(diào)用

(1) import Mydialog from '../carrer/mydialog.vue';

(2)引入組件

 components: {
  Mydialog
 }

(3  在html中插入組件

<Mydialog v-show="isShowDialog" :dialog-option="dialogOption" ref="mydialog"></Mydialog>

data中的參數(shù)

 showDialog: false,
   dialogOption: {
    text: '歡迎',
    cancelButtonText: '否',
    confirmButtonText: '是',
    isShowCancelButton: ''
  },

methods中 在需要出現(xiàn)彈框時候讓其顯示就好啦

 showDialog()
   this.dialogOption.text = ` <p>確認(rèn)拒絕?</p> `;
   this.dialogOption.isShowCancelButton = true
   this.showDialog = true;
   this.$refs.mydialog.confirm().then(() => {
    this.showDialog = false;
    this.refuse(id)
   }).catch(() => {
    this.showDialog = false;
   })
  },

3.使用方法2

因為在項目中經(jīng)常要使用到,而且每次使用的時候都要帶上相同的參數(shù),所以就封裝了一個js,(模態(tài)框的工具類),使用的時候調(diào)用就好了

1)- 新建一個js文件dialogUtil,復(fù)制下面的代碼就好了

class normalDialog {
 constructor(args) {
  // console.log("args",args);
  this.parent = args.parent;
  this.isShowDialog = args.isShowDialog;
  this.dialogOption = this.parent[args.dialogOption];
  this.mydialog = this.parent.$refs[args.mydialog];
  // console.log("dialogOption=",this.dialogOption);
 }

 showDialog(text, showCancelButton, success, error) {
  console.log("showDialog=="+text);
  this.dialogOption.text = text || "請輸入彈框標(biāo)題";
  let suc = typeof showCancelButton == "function" ? showCancelButton : success;
  let err = typeof showCancelButton == "function" ? success : error;
  if (typeof showCancelButton != "function") {
   this.dialogOption.isShowCancelButton = !!showCancelButton;
  } else {
   this.dialogOption.isShowCancelButton = true;
  }
  this.parent[this.isShowDialog] = true;
  this.confirm(suc, err);
 }

 //彈框回調(diào)
 confirm(success, error) {
  let self = this;
  this.mydialog.confirm().then(() => {
   typeof success == "function" && success();
   self.parent[this.isShowDialog] = false;
  }).catch(() => {
   typeof error == "function" && error();
   self.parent[this.isShowDialog] = false;
  })
 }

 toString() {
  // console.log(this.name + " : " + this.age);
 }

}

export default {
 //args:參數(shù), 類型
 creatByType(args, type) {
  type = !!type ? type : 1;

  switch (type) {
   case 1:
    return new normalDialog(args)
    break;
   case 2:
    break
   default:
    break;
  }
 }
}

2) 因為很多頁面都用到,所以讓他成為全局的(在main中引入就好了),那么別的頁面使用就不需要引入了

import dbDiaLogUtil from './assets/js/dialogUtil'
Vue.prototype.$dbDiaLogUtil = dbDiaLogUtil;

3)在使用的時候

頁面中引入組件在進(jìn)行調(diào)用

(1) import Mydialog from '../carrer/mydialog.vue';

(2)引入組件

 components: {
  Mydialog
 }

 (3) 在html中插入組件

 <Mydialog v-show="isShowDialog" :dialog-option="dialogOption" ref="mydialog"></Mydialog>

在data()中用對象的形式

  isShowDialog : false,
   dialogOption:{text: '',cancelButtonText: '否',confirmButtonText: '確認(rèn)',isShowCancelButton: false},
   dialogNormal:null,

在mounted中進(jìn)行初始化

this.dialogNormal = this.$dbDiaLogUtil.creatByType({parent:this,isShowDialog:'isShowDialog',dialogOption:'dialogOption',mydialog:'mydialog'});

最后在需要彈框的地方調(diào)用,一句代碼搞定啦

this.dialogNormal.showDialog('dialog要顯示的內(nèi)容',function(){console.log('成功執(zhí)行的')} , function(){console.log('失敗執(zhí)行的')})

上述就是小編為大家分享的使用vue怎么實現(xiàn)移動端模態(tài)框了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI