-
zhangwei
2025-03-06 02ad32582e3a3b0e6f4b2d1b50593eff1d0558e3
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:16  3,营业执照号:9    15 1  0  1 3  13 3 20 0 619 3K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view class="fui-drawer__popup-wrap" :class="{'fui-drawer__wrap-show':show}"
        :style="{ zIndex: zIndex,background:maskBackground,alignItems:direction==='left'?'flex-start':'flex-end' }"
        @tap.stop="handleClose" v-if="isShow || !isNvue" ref="fui_dwr_mk_ani">
        <!-- @touchmove.stop.prevent="stop" -->
        <view ref="fui_dwr_ani" class="fui-drawer__popup" :class="['fui-drawer_'+direction,show?'fui-drawer__show':'']"
            :style="{background: background,borderTopLeftRadius:(direction==='right'? radius:0)+'rpx',borderBottomLeftRadius:(direction==='right'? radius:0)+'rpx',borderTopRightRadius:(direction==='left'? radius:0)+'rpx',borderBottomRightRadius:(direction==='left'? radius:0)+'rpx'}"
            @tap.stop="stop($event,true)">
            <slot></slot>
        </view>
    </view>
</template>
 
<script>
    // #ifdef APP-NVUE
    const dom = weex.requireModule('dom');
    const animation = uni.requireNativePlugin('animation');
    // #endif
    /*顶部状态栏部分可使用fui-status-bar组件占位*/
    export default {
        name: 'fui-drawer',
        emits: ['close'],
        props: {
            show: {
                type: Boolean,
                default: false
            },
            //left/right
            direction: {
                type: String,
                default: 'right'
            },
            //背景颜色
            background: {
                type: String,
                default: '#fff'
            },
            zIndex: {
                type: [Number, String],
                default: 996
            },
            //点击遮罩 是否可关闭
            maskClosable: {
                type: Boolean,
                default: true
            },
            maskBackground: {
                type: String,
                default: 'rgba(0,0,0,.6)'
            },
            //圆角值,左侧打开时为右侧圆角,右侧打开时为左侧圆角
            radius: {
                type: [Number, String],
                default: 0
            }
        },
        data() {
            let isNvue = false;
            // #ifdef APP-NVUE
            isNvue = true;
            // #endif
            return {
                isNvue: isNvue,
                isShow: false,
                width: 0,
                isMounted: false
            }
        },
        // #ifdef APP-NVUE
        watch: {
            show: {
                handler(newVal) {
                    if (newVal) {
                        this.open();
                    } else {
                        this.close();
                    }
                },
                immediate: true
            }
        },
        // #endif
        mounted() {
            this.isMounted = true
            this.$nextTick(() => {
                setTimeout(() => {
                    this._getSize((width) => {
                        this.width = width
                    })
                }, 100)
            })
        },
        // #ifdef H5
        updated() {
            if (!this.isMounted && !this.width) {
                setTimeout(() => {
                    this._getSize((width) => {
                        this.width = width
                    })
                }, 50)
            }
        },
        // #endif
        methods: {
            _getSize(callback) {
                // #ifndef APP-NVUE
                uni.createSelectorQuery()
                    // #ifndef MP-ALIPAY
                    .in(this)
                    // #endif
                    .select('.fui-drawer__popup')
                    .boundingClientRect()
                    .exec(ret => {
                        if (ret) {
                            callback && callback(ret[0].width || 0)
                        }
                    })
                // #endif
                // #ifdef APP-NVUE
                dom.getComponentRect(this.$refs['fui_dwr_ani'], (ret) => {
                    const size = ret.size
                    if (size) {
                        callback && callback(size.width)
                    }
                })
                // #endif
            },
            handleClose(e) {
                if (!this.maskClosable) return;
                this.$emit('close', {});
            },
            // #ifdef APP-NVUE
            open() {
                this.isShow = true;
                this.$nextTick(() => {
                    setTimeout(() => {
                        this._animation(true);
                    }, 50);
                })
            },
            close() {
                this._animation(false);
            },
            _animation(type) {
                if (!this.$refs['fui_dwr_ani'] || !this.$refs['fui_dwr_mk_ani']) return;
                animation.transition(
                    this.$refs['fui_dwr_mk_ani'].ref, {
                        styles: {
                            opacity: type ? 1 : 0
                        },
                        duration: 250,
                        timingFunction: 'ease-in-out',
                        needLayout: false,
                        delay: 0 //ms
                    },
                    () => {
                        if (!type) {
                            this.isShow = false
                        }
                    }
                );
                let translateX = this.direction === 'left' ? '-100%' : '100%'
                animation.transition(
                    this.$refs['fui_dwr_ani'].ref, {
                        styles: {
                            transform: `translateX(${type ? '0' : translateX})`
                        },
                        duration: 250,
                        timingFunction: 'ease-in-out',
                        needLayout: false,
                        delay: 0 //ms
                    },
                    () => {}
                );
            },
            // #endif
            stop(e, tap) {
                // #ifdef APP-NVUE
                tap && e.stopPropagation();
                // #endif
            }
        }
    };
</script>
 
<style scoped>
    .fui-drawer__popup-wrap {
        position: fixed;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        z-index: 1001;
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: column;
        /* #ifndef APP-NVUE */
        transition: all ease-in-out .3s;
        visibility: hidden;
        /* #endif */
        opacity: 0;
        overflow: hidden;
    }
 
 
    /* #ifndef APP-NVUE */
    .fui-drawer__wrap-show {
        opacity: 1;
        visibility: visible;
    }
 
    /* #endif */
    .fui-drawer__popup {
        /* #ifndef APP-NVUE */
        height: 100%;
        transition: all 0.3s ease-in-out;
        min-width: 40rpx;
        display: flex;
        /* #endif */
        flex: 1;
        flex-direction: column;
        overflow: hidden;
    }
 
    .fui-drawer_left {
        /* #ifndef APP-NVUE */
        transform: translate3d(-100%, 0, 0);
        /* #endif */
        /* #ifdef APP-NVUE */
        transform: translateX(-100%);
        /* #endif */
    }
 
    .fui-drawer_right {
        /* #ifndef APP-NVUE */
        transform: translate3d(100%, 0, 0);
        /* #endif */
        /* #ifdef APP-NVUE */
        transform: translateX(100%);
        /* #endif */
    }
 
    /* #ifndef APP-NVUE */
    .fui-drawer__show {
        transform: translate3d(0, 0, 0);
    }
 
    /* #endif */
</style>