shareModal.js
5.57 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// components/show-modal/index.js
let app = getApp()
/****
* 功能名称:自定义弹框 (支持三个按钮)
* show:false @description 默认false,true弹框展示
* custom:false @description 默认false,true弹框内容自定义
* title:'' @description 弹框内容文案
* btns:[] @description 弹框按钮文案,最多支持三个
* cntMarginTop:550 @description 弹框距离顶部的距离rpx
* btnBorder:true
* btnTextColor:[] @description 弹框按钮字体颜色,最多支持三个
* tapTitle:"" 顶部标题
* tabTitleShow:false 顶部标题是否展示
* binddidClickBtn="onClickModalBtn" 外面通过e.detail获取,值为按钮所在位置的索引 0/1/2
* onClickModalBtn(e) {
console.log(e.detail)
}
*
*
* 案例
* <show-modal show="{{canShow}}" title="这是一个弹框" btns="{{['取消','确定']}}" binddidClickBtn="onClickModalBtn"></show-modal>
*
* 案例 custom:true
<show-modal show="{{canShow}}" custom="{{true}}" btns="{{['取消','确定']}}" binddidClickBtn="onClickModalBtn">
<view>这是一个弹框</view>
</show-modal>
*/
Component({
// 属性
properties: {
// 弹层开关
show: {
type: Boolean, //目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
value: false, // 默认值
observer(newVal, oldVal) {
if (newVal) {
this.createMaskAnimate(true)
} else {
this.createMaskAnimate(false)
}
}
},
// 是否内容自定义
custom: {
type: Boolean,
value: false
},
// 弹框内容
title: {
type: String,
value: '这是一个弹框'
},
// 按钮文字内容
btns: {
type: Array,
value: ['取消', '确定'] // 默认值
},
// 弹框距离顶部距离
cntMarginTop: {
type: Number,
value: 550 // 默认值
},
// 按钮文字颜色
btnTextColor: {
type: Array,
value: ['#A2A2A8', '#333333', '#303030'], // 默认值
observer(newVal, oldVal) {
if (!newVal && !newVal.length && newVal.length != 3) {
this._btnTextColor = ['#A2A2A8', '#333333', '#303030']
} else {
this._btnTextColor = newVal
}
}
},
// 按钮边框线
btnBorder: {
type: Boolean,
value: true
},
//标题是否显示
tabTitleShow: {
type: Boolean,
value: true
},
// 标题
tapTitle: {
type: String,
value: '提示信息'
}
},
// 初始化数据
data: {
_show: false,
_btnTextColor: ['#A2A2A8', '#333333', '#303030'],
maskAnimate: [],
frameAnimate: [],
title: '', // 提示弹框
success: () => {}, // 成功
error: () => {} // 失败
},
lifetimes: {
attached: function () {
function success() {} // 成功
function error() {}
},
detached: function () {
// 在组件实例被从页面节点树移除时执行
}
},
methods: {
selectMask: function (e) {
this.setData({
show: !this.data.show
})
},
shareBtn: function (e) {
let type = e.currentTarget.dataset.type
const that = this
console.log(type)
this.triggerEvent('didClickBtn', type) // 外面通过e.detail获取index的值
},
// 选中底部的按钮
selectBtn: function (e) {
let that = this
this.setData(
{
show: false
},
() => {
that.data.success()
}
)
// if (index == 0) {
// this.setData(
// {
// show: false
// },
// () => {
// that.data.error()
// }
// )
// } else {
// }
},
move: function () {
return
},
showModal: function (params) {
let { success, error } = this
this.setData({
show: !this.data.show,
title: params.title,
tabTitleShow: params.tabTitleShow,
success: params.success || function () {},
error: params.error || function () {}
})
},
//动画-遮罩层淡入淡出
createMaskAnimate(currentStatu) {
if (currentStatu) {
this.setData({
_show: currentStatu
})
this.createframeAnimate(true)
} else {
setTimeout(() => {
this.setData({
_show: false
})
}, 300)
this.createframeAnimate(false)
}
let maskAnimate = wx.createAnimation({
duration: 300,
timingFunction: 'ease-in-out',
delay: 0
})
this.maskAnimate = maskAnimate
maskAnimate.opacity(currentStatu ? 0.7 : 0).step()
setTimeout(() => {
this.setData({
maskAnimate: maskAnimate.export()
})
}, 50)
},
//动画-弹层内容Q弹
createframeAnimate(currentStatu) {
// return
let frameAnimate = wx.createAnimation({
duration: 300,
timingFunction: 'ease',
delay: 0
})
this.frameAnimate = frameAnimate
frameAnimate
.translate('-50%', '-50%')
.opacity(currentStatu ? 0.5 : 1)
.step()
// .scale(currentStatu ? 1.05 : 1)
this.setData({
frameAnimate: frameAnimate.export()
})
setTimeout(() => {
// 执行第二组动画
frameAnimate
.opacity(currentStatu ? 1 : 0)
.scale(currentStatu ? 1 : 0.9)
.step()
// 给数据对象储存的第一组动画,更替为执行完第二组动画的动画对象
this.setData({
frameAnimate: frameAnimate.export()
})
}, 150)
}
}
})