最近趁着國慶假期一直在搗鼓Nuxt.js項目開發,因爲項目中須要多個地方使用到彈窗功能,鑑於網上的一些組件可能不能很好知足項目需求,因而就本身動手開發了個自定義對話框組件Vpopup。html
VPopup 輕量級移動端Vue彈窗組件。集中融合了有贊Vant、京東NutUI等Vue組件庫中的 Msg信息框、Popup彈層、Dialog對話框、Toast提示框、ActionSheet動做面板框、Notify通知框 等功能。vue
在main.js中引入組件android
import Popup from './components/popup' Vue.use(Popup)
支持標籤式和函數式兩種方式調用組件。ios
<!-- 標籤式調用 --> <template> <view id="root"> ... <!-- VPopup模板 --> <v-popup v-model="showDialog" anim="scaleIn" title="標題內容" content="彈窗內容,告知當前狀態、信息和解決方法,描述文字儘可能控制在三行內!" shadeClose="false" xclose :btns="[ {...}, {...}, ]" /> </view> </template>
<!-- 函數式調用 --> <script> export default { ... methods: { handleShowDialog() { let $el = this.$vpopup({ title: '標題內容', content: '彈窗內容,告知當前狀態、信息和解決方法,描述文字儘可能控制在三行內!', anim: 'scaleIn', shadeClose: false, xclose: true, onClose: () => { console.log('vpopup is closed!') }, btns: [ {text: '關閉'}, { text: '肯定', style: 'color:#00e0a1', click: () => { $el.close() } } ] }); } } } </script>
這裏能夠根據項目需求,自行選擇調用方式便可。segmentfault
<!-- msg提示框 --> <v-popup v-model="showMsg" anim="fadeIn" content="msg提示框測試(3s後窗口關閉)" shadeClose="false" time="3" /> <v-popup v-model="showMsgBg" anim="footer" content="自定義背景顏色" shade="false" time="2" popup-style="background:rgba(0,0,0,.6);color:#fff;" /> <!-- 詢問框 --> <v-popup v-model="showConfirm" shadeClose="false" title="警告信息" xclose z-index="2001" content="<div style='color:#00e0a1;padding:20px 40px;'>確認框(這裏是確認框提示信息)</div>" :btns="[ {text: '取消', click: () => showConfirm=false}, {text: '肯定', style: 'color:#e63d23;', click: handleInfo}, ]" />
<!-- Toast輕提示彈窗 --> <v-popup v-model="showToast" type="toast" icon="loading" time="2" content="加載中..." /> <v-popup v-model="showToast" type="toast" icon="success" shade="false" time="2" content="成功提示" /> <v-popup v-model="showToast" type="toast" icon="fail" shade="false" time="2" content="失敗提示" />
<!-- Android風格彈窗 --> <v-popup v-model="showAndroid1" type="android" shadeClose="false" xclose title="標題內容" z-index="2001" content="彈窗內容,告知當前狀態、信息和解決方法,描述文字儘可能控制在三行內" :btns="[ {text: '知道了', click: () => showAndroid1=false}, {text: '肯定', style: 'color:#00e0a1;', click: handleInfo}, ]" > </v-popup>
<!-- ActionSheet底部菜單 --> <v-popup v-model="showActionSheet" anim="footer" type="actionsheet" :z-index="1011" content="彈窗內容,告知當前狀態、信息和解決方法,描述文字儘可能控制在三行內" :btns="[ {text: '拍照', style: 'color:#09f;', disabled: true, click: handleInfo}, {text: '從手機相冊選擇', style: 'color:#00e0a1;', click: handleInfo}, {text: '保存圖片', style: 'color:#e63d23;', click: () => null}, {text: '取消', click: () => showActionSheet=false}, ]" />
soga,這裏就不一一貼上示例代碼了。若是以爲還不錯,那就往下看實現過程吧。微信
參數配置
彈窗支持以下參數自定義配置,你們根據須要自行搭配使用。app
@@Props ------------------------------------------ v-model 當前組件是否顯示 title 標題 content 內容(支持自定義插槽內容) type 彈窗類型(toast | footer | actionsheet | actionsheetPicker | android/ios) popupStyle 自定義彈窗樣式 icon toast圖標(loading | success | fail) shade 是否顯示遮罩層 shadeClose 是否點擊遮罩時關閉彈窗 opacity 遮罩層透明度 round 是否顯示圓角 xclose 是否顯示關閉圖標 xposition 關閉圖標位置(left | right | top | bottom) xcolor 關閉圖標顏色 anim 彈窗動畫(scaleIn | fadeIn | footer | fadeInUp | fadeInDown) position 彈出位置(top | right | bottom | left) follow 長按/右鍵彈窗(座標點) time 彈窗自動關閉秒數(一、二、3) zIndex 彈窗層疊(默認8080) btns 彈窗按鈕(參數:text|style|disabled|click) @@$emit ------------------------------------------ open 打開彈出層時觸發(@open="xxx") close 關閉彈出層時觸發(@close="xxx") @@Event ------------------------------------------ onOpen 打開彈窗回調 onClose 關閉彈窗回調
彈窗模板popup.vue函數
<template> <div v-show="opened" class="nuxt__popup" :class="{'nuxt__popup-closed': closeCls}" :id="id"> <div v-if="JSON.parse(shade)" class="nuxt__overlay" @click="shadeClicked" :style="{opacity}"></div> <div class="nuxt__wrap"> <div class="nuxt__wrap-section"> <div class="nuxt__wrap-child" :class="['anim-'+anim, type&&'popui__'+type, round&&'round', position]" :style="popupStyle"> <div v-if="title" class="nuxt__wrap-tit" v-html="title"></div> <div v-if="type=='toast'&&icon" class="nuxt__toast-icon" :class="['nuxt__toast-'+icon]" v-html="toastIcon[icon]"></div> <template v-if="$slots.content"><div class="nuxt__wrap-cnt"><slot name="content" /></div></template> <template v-else><div v-if="content" class="nuxt__wrap-cnt" v-html="content"></div></template> <slot /> <div v-if="btns" class="nuxt__wrap-btns"> <span v-for="(btn,index) in btns" :key="index" class="btn" :style="btn.style" v-html="btn.text"></span> </div> <span v-if="xclose" class="nuxt__xclose" :class="xposition" :style="{'color': xcolor}" @click="close"></span> </div> </div> </div> </div> </template>
/** * @Desc VueJs自定義彈窗組件VPopup * @Time andy by 2020-10-06 * @About Q:282310962 wx:xy190310 */ <script> let $index = 0, $lockCount = 0, $timer = {}; export default { props: { ... }, data() { return { opened: false, closeCls: '', toastIcon: { ... } } }, watch: { value(val) { const type = val ? 'open' : 'close'; this[type](); }, }, methods: { // 打開彈窗 open() { if(this.opened) return; this.opened = true; this.$emit('open'); typeof this.onOpen === 'function' && this.onOpen(); if(JSON.parse(this.shade)) { if(!$lockCount) { document.body.classList.add('nt-overflow-hidden'); } $lockCount++; } // 倒計時關閉 if(this.time) { $index++; if($timer[$index] !== null) clearTimeout($timer[$index]) $timer[$index] = setTimeout(() => { this.close(); }, parseInt(this.time) * 1000); } if(this.follow) { this.$nextTick(() => { let obj = this.$el.querySelector('.nuxt__wrap-child'); let oW, oH, winW, winH, pos; oW = obj.clientWidth; oH = obj.clientHeight; winW = window.innerWidth; winH = window.innerHeight; pos = this.getPos(this.follow[0], this.follow[1], oW, oH, winW, winH); obj.style.left = pos[0] + 'px'; obj.style.top = pos[1] + 'px'; }); } }, // 關閉彈窗 close() { if(!this.opened) return; this.closeCls = true; setTimeout(() => { this.opened = false; this.closeCls = false; if(JSON.parse(this.shade)) { $lockCount--; if(!$lockCount) { document.body.classList.remove('nt-overflow-hidden'); } } if(this.time) { $index--; } this.$emit('input', false); this.$emit('close'); typeof this.onClose === 'function' && this.onClose(); }, 200); }, shadeClicked() { if(JSON.parse(this.shadeClose)) { this.close(); } }, btnClicked(e, index) { let btn = this.btns[index]; if(!btn.disabled) { typeof btn.click === 'function' && btn.click(e) } }, getZIndex() { for(var $idx = parseInt(this.zIndex), $el = document.getElementsByTagName('*'), i = 0, len = $el.length; i < len; i++) $idx = Math.max($idx, $el[i].style.zIndex) return $idx; }, // 獲取彈窗座標點 getPos(x, y, ow, oh, winW, winH) { let l = (x + ow) > winW ? x - ow : x; let t = (y + oh) > winH ? y - oh : y; return [l, t]; } }, } </script>
經過組件傳過來的v-model來調用open()和close()方法。測試
watch: { value(val) { const type = val ? 'open' : 'close'; this[type](); }, },
組件還支持右鍵彈窗/長按彈窗及自定義插槽內容。動畫
<v-popup v-model="showComponent" xclose xposition="bottom" :shadeClose="false" content="這裏是內容信息" :btns="[ {text: '確認', style: 'color:#f60;', click: () => showComponent=false}, ]" @open="handleOpen" @close="handleClose" > <template #content><b style="color:#00e0a1;">當 content 和 自定義插槽 內容同時存在,只顯示插槽內容!!!</b></template> <!-- <div slot="content">顯示自定義插槽內容!</div> --> <div style="padding:30px 15px;"> <img src="https://img.yzcdn.cn/vant/apple-3.jpg" style="width:100%;" @click="handleContextPopup" /> </div> </v-popup>
經過Vue.extend擴展構造器來實現函數式調用this.$vpopup({...})
import Vue from 'vue'; import VuePopup from './popup.vue'; let PopupConstructor = Vue.extend(VuePopup); let $instance; let VPopup = function(options = {}) { // 同一個頁面中,id相同的Popup的DOM只會存在一個 options.id = options.id || 'nuxt-popup-id'; $instance = new PopupConstructor({ propsData: options }); $instance.vm = $instance.$mount(); let popupDom = document.querySelector('#' + options.id); if(options.id && popupDom) { popupDom.parentNode.replaceChild($instance.$el, popupDom); } else { document.body.appendChild($instance.$el); } Vue.nextTick(() => { $instance.value = true; }) return $instance; } VPopup.install = () => { Vue.prototype['$vpopup'] = VPopup; Vue.component('v-popup', VuePopup); } export default VPopup;
如上就實現了在Vue的 prototype 上掛載 $vpopup 方法及註冊 v-popup 組件。
好了,基於Vue+Nuxt自定義彈窗組件就介紹到這裏。目前該組件正在Nuxt項目中使用,到時也會分享出來。但願對你們有所幫助!!💪💪
最後附上基於Uniapp+Vue跨端仿抖音|聊天實例項目
http://www.javashuo.com/article/p-djfhoqju-cy.html