forked from N3-components/N3-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n3Toast.vue
104 lines (95 loc) · 1.88 KB
/
n3Toast.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<template>
<div
v-el:dom
:class="classObj"
transition="fade"
@click="handleClick"
v-if="show">
<h5>{{text}}</h5>
</div>
</template>
<script>
export default {
props: {
top: {
type: Boolean,
default: false
},
placement: {
type: String,
default: 'center'
},
type: {
type: String,
default: 'default'
},
duration: {
type: Number,
default: 3000
},
closeOnClick: {
type: Boolean,
default: true
},
show: {
type: Boolean,
twoWay: true,
default: false
},
text: {
type: String
},
width: {
type: String
},
prefixCls: {
type: String,
default: 'n3'
}
},
data () {
return {
setT: ''
}
},
computed: {
classObj () {
let {prefixCls, placement, type} = this
let klass = {}
klass[prefixCls + '-toast'] = true
klass[prefixCls + '-toast-' + type] = true
klass[prefixCls + '-' + placement] = true
return klass
}
},
methods: {
handleClick () {
if (this.closeOnClick) {
this.show = false
}
}
},
watch: {
show: {
handler (val) {
var self = this
this.setT = window.clearTimeout(this.setT)
if (val) {
if (this.placement === 'top' || this.placement === 'bottom') {
this.$els.dom.style.marginLeft = -1 * this.$els.dom.offsetWidth / 2 + 'px'
} else if (this.placement === 'center') {
this.$els.dom.style.marginLeft = -1 * this.$els.dom.offsetWidth / 2 + 'px'
this.$els.dom.style.marginTop = -1 * this.$els.dom.offsetHeight / 2 + 'px'
}
}
if (val && this.duration) {
this.setT = window.setTimeout(() => {
self.show = false
}, this.duration)
}
},
immediate: true
}
}
}
</script>