-
zhangwei
2025-03-14 6e961fafc0f921d575772a3c89f2c5cad28c270d
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
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:1 6 3,营业执照号:9 1 5  101      31  33  2  006193 K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <!-- #ifdef APP-NVUE -->
    <view ref="fui_bd_ani" class="fui-backdrop__wrap"
        :style="{background:background,position:absolute?'absolute':'fixed',width:absolute && full ?winWidth+'px':'100%',height:absolute && full ?winHeight+'px':'100%'}"
        @tap.stop="handleClick">
        <slot></slot>
    </view>
    <!-- #endif -->
    <!-- #ifndef APP-NVUE -->
    <view class="fui-backdrop__wrap" :class="{'fui-backdrop__bg':!background,'fui-backdrop__show':show}"
        :style="{background:background,position:absolute?'absolute':'fixed',zIndex:zIndex}" @tap.stop="handleClick">
        <slot></slot>
    </view>
    <!-- #endif -->
</template>
 
<script>
    // #ifdef APP-NVUE
    const animation = uni.requireNativePlugin('animation');
    // #endif
    export default {
        name: "fui-backdrop",
        emits: ['click'],
        props: {
            show: {
                type: Boolean,
                default: false
            },
            background: {
                type: String,
                // #ifdef APP-NVUE
                default: 'rgba(0, 0, 0, 0.6)',
                // #endif
 
                // #ifndef APP-NVUE
                default: ''
                // #endif
            },
            //是否绝对定位,默认固定定位fixed
            absolute: {
                type: Boolean,
                default: false
            },
            //absolute定位且组件在根目录下时是否铺满屏幕,仅nvue有效
            full: {
                type: Boolean,
                default: false
            },
            zIndex: {
                type: Number,
                default: 980
            },
            closable: {
                type: Boolean,
                default: false
            }
        },
        // #ifdef APP-NVUE
        watch: {
            show(val) {
                if (val) {
                    this.openAni()
                } else {
                    this.closeAni()
                }
            }
        },
        mounted() {
            this.$nextTick(() => {
                setTimeout(() => {
                    if (this.show) {
                        this._ani(true)
                    } else {
                        this._aniHidden(this.show)
                    }
                }, 50)
            })
        },
        data() {
            return {
                winWidth: 300,
                winHeight: 600
            }
        },
        created() {
            let sys = uni.getSystemInfoSync()
            this.winWidth = sys.windowWidth
            this.winHeight = sys.windowHeight
        },
        // #endif
        methods: {
            // #ifdef APP-NVUE
            openAni() {
                this._aniHidden(true, () => {
                    this._ani(true)
                })
            },
            closeAni(type) {
                this._ani(false, () => {
                    this._aniHidden(false)
                });
            },
            _ani(type, callback) {
                let styles = {
                    opacity: type ? 1 : 0
                };
                if (!this.$refs['fui_bd_ani']) return;
                animation.transition(
                    this.$refs['fui_bd_ani'].ref, {
                        styles,
                        duration: 200, //ms
                        timingFunction: 'ease-in',
                        needLayout: false,
                        delay: 0 //ms
                    },
                    () => {
                        callback && callback()
                    }
                );
            },
            _aniHidden(isShow, callback) {
                if (!this.$refs['fui_bd_ani']) return;
                let styles = {
                    transform: isShow ? 'translateX(0px)' : 'translateX(-1500px)'
                };
                animation.transition(
                    this.$refs['fui_bd_ani'].ref, {
                        styles,
                        duration: 0,
                        needLayout: false,
                        delay: 0
                    },
                    () => {
                        callback && callback()
                    }
                );
            },
            // #endif
            handleClick() {
                if (this.closable && this.show) {
                    this.$emit('click')
                }
            }
        }
    }
</script>
 
<style scoped>
    .fui-backdrop__wrap {
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        /* #ifndef APP-NVUE */
        display: flex;
        visibility: hidden;
        transition: all 0.3s;
        opacity: 0;
        /* #endif */
        align-items: center;
        justify-content: center;
        /* #ifdef APP-NVUE */
        opacity: 0.001;
        /* #endif */
    }
 
    /* #ifndef APP-NVUE */
    .fui-backdrop__bg {
        background: var(--fui-bg-color-mask, rgba(0, 0, 0, 0.6)) !important;
    }
 
    .fui-backdrop__show {
        visibility: visible !important;
        opacity: 1 !important;
    }
 
    /* #endif */
</style>