關(guān)于vue.js彈窗組件的知識點(diǎn)總結(jié)
首先在開發(fā)時(shí)需要考慮以下三點(diǎn):
1、進(jìn)入和彈出的動畫效果。
2、z-index 的控制
3、overlay 遮蓋層
關(guān)于動畫
vue 對于動畫的處理相對簡單,給組件加入css transition 動畫即可
<template>
<div class="modal" transition="modal-scale">
<!--省略其它內(nèi)容-->
</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 的控制
關(guān)于z-index的控制,需要完成以下幾點(diǎn)
1、保證彈出框的 z-index 足夠高能使 其再最外層
2、后彈出的彈出框的 z-index 要比之前彈出的要高
要滿足以上兩點(diǎn), 我們需要以下代碼實(shí)現(xiàn)
const zIndex = 20141223 // 先預(yù)設(shè)較高值
const getZIndex = function () {
return zIndex++ // 每次獲取之后 zindex 自動增加
}
然后綁定把 z-index 在組件上
<template>
<div class="modal" :style="{'z-index': zIndex}" transition="modal-scale">
<!--省略其它內(nèi)容-->
</div>
</template>
<script>
export default {
data () {
return {
zIndex: getZIndex()
}
}
}
</script>
overlay 遮蓋層的控制
遮蓋層是彈窗組件中最難處理的部分, 一個(gè)完美的遮蓋層的控制需要完成以下幾點(diǎn):
1、遮蓋層和彈出層之間的動畫需要并行
2、遮蓋層的 z-index 要較小與彈出層
3、遮蓋層的彈出時(shí)需要組件頁面滾動
4、點(diǎn)擊遮蓋層需要給予彈出層反饋
5、保證整個(gè)頁面最多只能有一個(gè)遮蓋層(多個(gè)疊在一起會使遮蓋層顏色加深)
為了處理這些問題,也保證所有的彈出框組件不用每一個(gè)都解決,所以決定利用 vue 的 mixins 機(jī)制,將這些彈出層的公共邏輯封裝層一個(gè) mixin ,每個(gè)彈出框組件直接引用就好。
vue-popup-mixin
明確了上述所有的問題,開始開發(fā) mixin, 首先需要一個(gè) 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>
然后 需要一個(gè) 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: [], // 用來儲存所有的彈出層實(shí)例
overlay: false,
// 彈窗框打開時(shí) 調(diào)用此方法
open (instance) {
if (!instance || this.instances.indexOf(instance) !== -1) return
// 當(dāng)沒有遮蓋層時(shí),顯示遮蓋層
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()
},
// 彈出框關(guān)閉方法
close (instance) {
let index = this.instances.indexOf(instance)
if (index === -1) return
Vue.nextTick(() => {
this.instances.splice(index, 1)
// 當(dāng)頁面上沒有彈出層了就關(guān)閉遮蓋層
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
},
// 遮蓋層點(diǎn)擊處理,會自動調(diào)用 彈出層的 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
最后再封裝成一個(gè) 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'
}
},
// 組件被掛載時(shí)會判斷show的值開控制打開
attached () {
if (this.show && this.overlay) {
PopupManager.open(this)
}
},
// 組件被移除時(shí)關(guān)閉
detached () {
PopupManager.close(this)
},
watch: {
show (val) {
// 修改 show 值是調(diào)用對于的打開關(guān)閉方法
if (val && this.overlay) {
PopupManager.open(this)
} else {
PopupManager.close(this)
}
}
},
beforeDestroy () {
PopupManager.close(this)
}
}
使用
以上所有的代碼就完成了所有彈出層的共有邏輯, 使用時(shí)只需要當(dāng)做一個(gè)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: {
// 響應(yīng) overlay事件
overlayClick () {
this.show = false
},
// 響應(yīng) esc 按鍵事件
escPress () {
this.show = false
}
}
}
</script>
總結(jié)
以上就是關(guān)于vue.js彈窗組件的一些知識點(diǎn),希望對大家的學(xué)習(xí)或者工作帶來一定的幫助,如果大家有疑問可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
element中el-table與el-form同用并校驗(yàn)
本文主要介紹了element中el-table與el-form同用并校驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue 實(shí)現(xiàn)邊輸入邊搜索功能的實(shí)例講解
今天小編就為大家分享一篇vue 實(shí)現(xiàn)邊輸入邊搜索功能的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
圖文詳解vue中proto文件的函數(shù)調(diào)用
這篇文章主要給大家介紹了vue中proto文件函數(shù)調(diào)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-08-08
vue指令實(shí)現(xiàn)數(shù)字和大寫中文實(shí)時(shí)互轉(zhuǎn)
這篇文章主要介紹了如何使用Vue指令實(shí)現(xiàn)在用戶輸入數(shù)字失焦后實(shí)時(shí)將數(shù)字轉(zhuǎn)為大寫中文,聚焦的時(shí)候?qū)⒋髮懼形霓D(zhuǎn)為數(shù)字以便用戶繼續(xù)修改,需要的可以參考下2024-12-12
Vue3+ElementPlus封裝圖片空間組件的門面實(shí)例
圖片空間是用于管理上傳圖片的工具,可以讓用戶方便地存儲、管理和調(diào)用圖片,提高工作效率,它通常具備多樣的樣式,但操作入口統(tǒng)一,便于使用,通過圖片空間組件,用戶能直接在其他模塊(例如商品圖片)中選擇所需圖片2024-09-09
vue跳轉(zhuǎn)時(shí)根據(jù)url錨點(diǎn)(#xxx)實(shí)現(xiàn)頁面內(nèi)容定位的方法
本前端仔在做頁面跳轉(zhuǎn)的時(shí)候,被要求跳轉(zhuǎn)到頁面時(shí)候,把對應(yīng)部分的內(nèi)容自動滾動到頂部,我一開始想到的就是根據(jù)錨點(diǎn)<a href="#xxid">進(jìn)行處理,但是發(fā)現(xiàn)不太好實(shí)現(xiàn),于是便自己研究了一個(gè)比較取巧的方法,需要的朋友可以參考下2024-04-04
vue如何監(jiān)聽對象或者數(shù)組某個(gè)屬性的變化詳解
這篇文章主要給大家介紹了關(guān)于vue如何監(jiān)聽對象或者數(shù)組某個(gè)屬性的變化,在Vue.js中可以通過watch監(jiān)聽屬性變化并動態(tài)修改其他屬性的值,watch通過監(jiān)聽器函數(shù)接收新舊值,實(shí)現(xiàn)屬性間的數(shù)據(jù)聯(lián)動,需要的朋友可以參考下2024-12-12

