您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關使用vue怎么實現移動端模態框,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1-封裝模態框組件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> <!--內容區域--> <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的值(可選,如果想點擊確認之后,進行一些處理再決定關不關閉,可取消在這里更新visible) this.$emit('Confirm'); // 傳遞確認事件 }, CancelHandler () { this.$emit('update:visible', false); // 更新visible的值 this.$emit('Cancel'); // 傳遞取消事件 }, close () { this.visible = false; } }, /** * modal 模態接口參數 * @param {string} modal.title 模態框標題 * @param {boolean} modal.showCancelButton 是否顯示取消按鈕 * @param {string} modal.cancelButtonClass 取消按鈕樣式 * @param {string} modal.cancelButtonText 取消按鈕文字 * @param {string} modal.showConfirmButton 是否顯示確定按鈕 * @param {string} modal.confirmButtonClass 確定按鈕樣式 * @param {string} modal.confirmButtonText 確定按鈕標文字 */ computed: { /** * 格式化props進來的參數,對參數賦予默認值 */ 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 { // 使用時沒有傳遞參數 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
頁面中引入在進行調用
(1) import Mydialog from '../carrer/mydialog.vue';
(2)引入組件
components: { Mydialog }
(3 在html中插入組件
<Mydialog v-show="isShowDialog" :dialog-option="dialogOption" ref="mydialog"></Mydialog>
data中的參數
showDialog: false, dialogOption: { text: '歡迎', cancelButtonText: '否', confirmButtonText: '是', isShowCancelButton: '' },
methods中 在需要出現彈框時候讓其顯示就好啦
showDialog() this.dialogOption.text = ` <p>確認拒絕?</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
因為在項目中經常要使用到,而且每次使用的時候都要帶上相同的參數,所以就封裝了一個js,(模態框的工具類),使用的時候調用就好了
1)- 新建一個js文件dialogUtil,復制下面的代碼就好了
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 || "請輸入彈框標題"; 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); } //彈框回調 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:參數, 類型 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)在使用的時候
頁面中引入組件在進行調用
(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: '確認',isShowCancelButton: false}, dialogNormal:null,
在mounted中進行初始化
this.dialogNormal = this.$dbDiaLogUtil.creatByType({parent:this,isShowDialog:'isShowDialog',dialogOption:'dialogOption',mydialog:'mydialog'});
最后在需要彈框的地方調用,一句代碼搞定啦
this.dialogNormal.showDialog('dialog要顯示的內容',function(){console.log('成功執行的')} , function(){console.log('失敗執行的')})
上述就是小編為大家分享的使用vue怎么實現移動端模態框了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。