開發時通常要優先權衡以下三點:
1、進入與彈出的動畫效果。
2、z-index 的控管。
3、遮罩層(overlay)。
關於動畫
Vue 對動畫的處理相對簡單,只要為組件加上 CSS transition 即可。
<template> <div class="modal" transition="modal-scale"> <!--此處省略其他內容--> </div> </template> <script> // ... </script> <style> .modal-scale-transition{ transition: transform,opacity .3s ease; } .modal-scale-enter, .modal-scale-leave { opacity: 0; } .modal-scale-enter { transform: scale(1.1); } .modal-scale-leave { transform: scale(0.8); } </style>
外部也能由使用者自行控制,透過 v-if 或 v-show 來切換顯示。
z-index 的控管
關於 z-index,需要達成兩個目標:
1、確保彈出框的 z-index 足夠高,能處於最外層。
2、後彈出的彈出框,其 z-index 要比先前的更高。
要同時滿足這兩點,可以透過以下程式碼來實現:
const zIndex = 20141223 // 先預設一個較高的值 const getZIndex = function () { return zIndex++ // 每次取得後 z-index 自動遞增 }
接著把 z-index 綁定到組件上:
<template> <div class="modal" :style="{'z-index': zIndex}" transition="modal-scale"> <!--此處省略其他內容--> </div> </template> <script> export default { data () { return { zIndex: getZIndex() } } } </script>
遮罩層(overlay)的控管
遮罩層往往是彈窗組件裡最棘手的環節。一個理想的遮罩層需要滿足以下幾點:
1、遮罩層與彈出層的動畫需要並行。
2、遮罩層的 z-index 要小於彈出層。
3、遮罩層彈出時需要鎖定頁面捲動。
4、點擊遮罩層時要能給予彈出層回饋。
5、確保整個頁面最多只有一個遮罩層(多層堆疊會使顏色加深)。
為了妥善處理這些問題,也避免每個彈出框組件都要各自解決一次,我們決定運用 Vue 的 mixins 機制,把這些公共邏輯封裝成一個 mixin,各彈出框組件直接引用即可。
vue-popup-mixin
釐清上述問題後,開始開發 mixin。首先需要一個 overlay(遮罩層組件):
<template> <div class="overlay" @click="handlerClick" @touchmove="prevent" :style="style" transition="overlay-fade"></div> </template> <script> export default { props: { onClick: { type: Function }, opacity: { type: Number, default: 0.4 }, color: { type: String, default: '#000' } }, computed: { style () { return { 'opacity': this.opacity, 'background-color': this.color } } }, methods: { prevent (event) { event.preventDefault() event.stopPropagation() }, handlerClick () { if (this.onClick) { this.onClick() } } } } </script> <style lang="less"> .overlay { position: fixed; left: 0; right: 0; top: 0; bottom: 0; background-color: #000; opacity: .4; z-index: 1000; } .overlay-fade-transition { transition: all .3s linear; &.overlay-fade-enter, &.overlay-fade-leave { opacity: 0 !important; } } </style>
接著需要一段 JS 來管理 overlay 的顯示與隱藏。
import Vue from 'vue' import overlayOpt from '../overlay' // 引入 overlay 組件 const Overlay = Vue.extend(overlayOpt) const getDOM = function (dom) { if (dom.nodeType === 3) { dom = dom.nextElementSibling || dom.nextSibling getDOM(dom) } return dom } // z-index 控管 const zIndex = 20141223 const getZIndex = function () { return zIndex++ } // 管理 const PopupManager = { instances: [], // 用來儲存所有彈出層實例 overlay: false, // 彈窗框打開時呼叫此方法 open (instance) { if (!instance || this.instances.indexOf(instance) !== -1) return // 當沒有遮罩層時,顯示遮罩層 if (this.instances.length === 0) { this.showOverlay(instance.overlayColor, instance.overlayOpacity) } this.instances.push(instance) // 儲存打開的彈出框組件 this.changeOverlayStyle() // 控制不同彈出層的透明度與顏色 // 為彈出層加上 z-index const dom = getDOM(instance.$el) dom.style.zIndex = getZIndex() }, // 彈出框關閉方法 close (instance) { let index = this.instances.indexOf(instance) if (index === -1) return Vue.nextTick(() => { this.instances.splice(index, 1) // 當頁面上沒有彈出層了就關閉遮罩層 if (this.instances.length === 0) { this.closeOverlay() } this.changeOverlayStyle() }) }, showOverlay (color, opacity) { let overlay = this.overlay = new Overlay({ el: document.createElement('div') }) const dom = getDOM(overlay.$el) dom.style.zIndex = getZIndex() overlay.color = color overlay.opacity = opacity overlay.onClick = this.handlerOverlayClick.bind(this) overlay.$appendTo(document.body) // 禁止頁面捲動 this.bodyOverflow = document.body.style.overflow document.body.style.overflow = 'hidden' }, closeOverlay () { if (!this.overlay) return document.body.style.overflow = this.bodyOverflow let overlay = this.overlay this.overlay = null overlay.$remove(() => { overlay.$destroy() }) }, changeOverlayStyle () { if (!this.overlay || this.instances.length === 0) return const instance = this.instances[this.instances.length - 1] this.overlay.color = instance.overlayColor this.overlay.opacity = instance.overlayOpacity }, // 遮罩層點擊處理,會自動呼叫彈出層的 overlayClick 方法 handlerOverlayClick () { if (this.instances.length === 0) return const instance = this.instances[this.instances.length - 1] if (instance.overlayClick) { instance.overlayClick() } } } window.addEventListener('keydown', function (event) { if (event.keyCode === 27) { // ESC if (PopupManager.instances.length > 0) { const topInstance = PopupManager.instances[PopupManager.instances.length - 1] if (!topInstance) return if (topInstance.escPress) { topInstance.escPress() } } } }) export default PopupManager
最後再把它封裝成一個 mixin:
import PopupManager from './popup-manager' export default { props: { show: { type: Boolean, default: false }, // 是否顯示遮罩層 overlay: { type: Boolean, default: true }, overlayOpacity: { type: Number, default: 0.4 }, overlayColor: { type: String, default: '#000' } }, // 組件被掛載時會判斷 show 的值來控制打開 attached () { if (this.show && this.overlay) { PopupManager.open(this) } }, // 組件被移除時關閉 detached () { PopupManager.close(this) }, watch: { show (val) { // 修改 show 值時呼叫對應的打開關閉方法 if (val && this.overlay) { PopupManager.open(this) } else { PopupManager.close(this) } } }, beforeDestroy () { PopupManager.close(this) } }
使用方式
以上所有程式碼就完成了彈出層的共有邏輯,使用時只要把它當成一個 mixin 載入即可。
<template> <div class="dialog" v-show="show" transition="dialog-fade"> <div class="dialog-content"> <slot></slot> </div> </div> </template> <style> .dialog { left: 50%; top: 50%; transform: translate(-50%, -50%); position: fixed; width: 90%; } .dialog-content { background: #fff; border-radius: 8px; padding: 20px; text-align: center; } .dialog-fade-transition { transition: opacity .3s linear; } .dialog-fade-enter, .dialog-fade-leave { opacity: 0; } </style> <script> import Popup from '../src' export default { mixins: [Popup], methods: { // 回應 overlay 事件 overlayClick () { this.show = false }, // 回應 esc 按鍵事件 escPress () { this.show = false } } } </script>
總結
以上就是關於 Vue.js 彈窗組件的一些關鍵知識點,希望對大家的工作或學習有所幫助,若有疑問歡迎留言交流。