vue已經(jīng)成為前端一個(gè)必備技能了,就像當(dāng)年的jquery东涡,所以好好學(xué)吧~
不多說(shuō)上代碼
// index.vue 彈框主體,可以直接組件的形式引用倘待,后面會(huì)介紹如何在使用 this.$xxxx
<template>
<div v-if="!destroyDom" class="panel">
<transition name="fade">
<div v-if="visible" :style="{opacity:data.mask?1:0}" class="panel_mask" @click="action({type:'mask'})" />
</transition>
<transition name="bounce" @after-leave="destroyDomHandle">
<div v-if="visible" class="panel-wrap" :class="data.class">
<div v-if="data.title" class="panel-title" v-html="data.title" />
<div v-if="!component" class="panel-content" v-html="data.content" />
<div v-if="component" class="panel-content">
<component :is="_component" ref="component" />
</div>
<div class="panel-btn">
<div v-for="(btn,index) in btns" :key="index" :class="`panel-${btn.type}`" @click="action(btn)">{{ btn.text }}</div>
</div>
</div>
</transition>
</div>
</template>
<script>
export default {
name: 'Panel',
props: {
component: {
type: Function,
default: null
},
data: {
type: Object,
required: true
},
btns: {
type: Array,
default: () => {
return [
{
text: '知道了',
type: 'cancel'
}
]
}
},
listener: { // 每個(gè)階段會(huì)回調(diào)
type: Function,
default: null
}
},
data() {
return {
visible: false,
destroyDom: false
}
},
computed: {
_component() { // 可以傳入vnode 同時(shí)也保留了傳html
const fn = this.component
let vNode
return {
name: 'panel-component',
render() {
vNode = fn()
return vNode
}
}
}
},
created() {
this.listener && this.listener({ acr: 'start' })
this.data.delay && setTimeout(() => {
this.close()
}, this.data.delay)
},
methods: {
destroyDomHandle() { // 動(dòng)畫(huà)結(jié)束后把這塊這塊銷(xiāo)毀
this.destroyDom = true
this.listener && this.listener({ act: 'destroy' })
},
close() {
this.visible = false
this.listener && this.listener({ act: 'close' })
},
action(btn) {
this.listener && this.listener({ act: 'click', type: btn.type })
if (btn && btn.callback) {
btn.callback(this.close)
} else {
this.close()
}
}
}
}
</script>
// 下面就是一些樣式疮跑,適用移動(dòng)端,并使用rem做的適配
<style scoped lang="less">
@designwidth: 750;
.bounce-enter-active {
animation: bounce-in 0.4s;
}
.bounce-leave-active {
animation: bounce-in 0.4s reverse;
}
@keyframes bounce-in {
0% {
transform: translate3d(-50%, -32%, 0) scale(0);
opacity: 0;
}
50% {
transform: translate3d(-50%, -31%, 0) scale(1.1);
opacity: 0.8;
}
100% {
transform: translate3d(-50%, -30%, 0) scale(1);
opacity: 1;
}
}
.fade-enter-active,
.fade-leave-active {
transition: all 0.4s;
}
.fade-enter,
.fade-leave-to {
opacity: 0;
}
.panel_mask {
position: fixed;
z-index: 99;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.3);
}
.panel-wrap {
position: fixed;
top: 30%;
left: 50%;
z-index: 100;
transform: translate3d(-50%, -30%, 0);
width: 70%;
// max-height: 70%;
background: #f8f8f8;
border-radius: rem(16);
box-sizing: border-box;
}
.panel-title {
padding: rem(10) 0;
width: 100%;
text-align: center;
// max-height: 1rem;
font-size: rem(36);
overflow: hidden;
box-sizing: border-box;
color: #666;
text-overflow: ellipsis;
border-bottom: 1px solid #d9d9d9;
white-space: nowrap;
}
.panel-content {
padding: rem(20) rem(20);
width: 100%;
max-height: 60vh;
overflow-y: auto;
font-size: rem(30);
box-sizing: border-box;
text-align: center;
}
.panel-btn {
text-align: center;
display: flex;
width: 100%;
border-top: 1px solid #d9d9d9;
color: #4e82d7;
}
.panel-btn > div {
flex: 1;
padding: rem(10) 0;
border-left: 1px solid #d9d9d9;
border-radius: 0.12rem;
font-size: rem(30);
&:nth-child(1) {
border-left: 0;
}
}
</style>
渲染到頁(yè)面
// render.js
import Tpl from './index.vue'
export default function create(Vue, { store = {}, router = {}}, options) {
const comp = new Vue({
name: 'Root' + Tpl.name,
router,
store,
data() {
return {
options: { ...options }
}
},
render(h) {
return h(Tpl, {
props: this.options
})
}
})
comp.$mount()
document.body.appendChild(comp.$el)
comp.$children[0].visible = true
return {
close: () => comp.$children[0].close() // 暴露出去一個(gè)關(guān)閉的方法
}
}
掛載
// index.js
import create from './render'
export default {
install(Vue, options = {}) {
Vue.prototype.$panel = create.bind(this, Vue, options)
}
}
使用
// main.js
import Vue from 'vue'
import App from './App.vue'
import Panel from './base/panel'
Vue.use(Panel)
new Vue({
render: h => h(App)
}).$mount('#app')
// 組件中全局使用
this.$panel({
data: {
class: 'xxxx',
mask: true,
title: '提示',
content: 'xxx'
},
btns: [
{
text: '知道了',
type: 'cancel',
callback: () => { }
}
],
component: () => <div />,
listener: (res) => {console.log(res)}
})
通過(guò)控制參數(shù)可以實(shí)現(xiàn)toast
凸舵,可以進(jìn)行二次封裝