zhangwei
2024-09-03 376ac09a54e8c95190d06bf39f295c890829c103
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
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:16  3,营业执照号:9 1     5  1013133 2 0    0   6193K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view class="fui-sticky__wrap" :class="{'fui-sticky__fixed':!range}" :style="getStyles" :id="elId"
        ref="fui_sticky__el">
        <view :class="{'fui-sticky__fixed':range,'fui-sticky__fixed-mp':!range && isFixed}" :style="getStyle">
            <slot></slot>
        </view>
        <!-- #ifdef MP-BAIDU || MP-QQ || MP-TOUTIAO -->
        <view class="fui-sticky__seat" :class="{'fui-sticky__seat-hidden':!isFixed}" v-if="!range"
            :style="{height:height+'px'}"></view>
        <!-- #endif -->
        <slot name="content"></slot>
    </view>
</template>
 
<script>
    // #ifdef APP-NVUE
    const dom = weex.requireModule('dom')
    // #endif
    export default {
        name: "fui-sticky",
        emits: ['sticky'],
        // #ifdef MP-WEIXIN
        options: {
            virtualHost: true
        },
        // #endif
        props: {
            top: {
                type: [Number, String],
                default: 0
            },
            range: {
                type: Boolean,
                default: false
            },
            scrollTop: {
                type: Number,
                default: 0
            },
            zIndex: {
                type: [Number, String],
                default: 999
            },
            width: {
                type: [Number, String],
                default: 750
            },
            param: {
                type: [Number, String],
                default: 0
            }
        },
        computed: {
            getStyles() {
                let styles = ''
                // #ifdef APP-NVUE
                styles = `width:${this.width}rpx;`
                // #endif
                if (!this.range) {
                    styles += `top:${this.top}px;z-index:${this.zIndex};`
                }
                return styles
            },
            getStyle() {
                let styles = ''
                if (this.range) {
                    styles = `top:${this.top}px;z-index:${this.zIndex};`
                }
                return styles
            }
        },
        watch: {
            scrollTop(val) {
                this.updateStickyChange();
            }
        },
        mounted() {
            this.$nextTick(()=>{
                setTimeout(() => {
                    this.init(() => {
                        this.updateStickyChange();
                    });
                }, 50)
            })
        },
        updated(e) {
            this.$nextTick(() => {
                this.init(() => {
                    this.updateStickyChange();
                });
            })
        },
        data() {
            const elId = `fui_${Math.ceil(Math.random() * 10e5).toString(36)}`
            return {
                elId: elId,
                timer: null,
                elTop: 0,
                height: 0,
                isFixed: false
            };
        },
        methods: {
            updateStickyChange() {
                const et = this.elTop;
                const h = this.height;
                const st = this.scrollTop
                const t = this.top
                if (this.range) {
                    this.isFixed = (st + t >= et && st + t < et + h) ? true : false
                } else {
                    this.isFixed = st + t >= et ? true : false
                }
                //是否吸顶
                this.$emit("sticky", {
                    isFixed: this.isFixed,
                    param: this.param
                })
            },
            init(callback) {
                // #ifndef APP-NVUE
                const elId = `#${this.elId}`;
                uni.createSelectorQuery()
                    // #ifndef MP-ALIPAY
                    .in(this)
                    // #endif
                    .select(elId)
                    .fields({
                        size: true,
                        rect: true
                    }, res => {
                        if (res) {
                            this.elTop = res.top + (this.scrollTop || 0);
                            this.height = res.height;
                            callback && callback()
                        }
                    }).exec()
                // #endif
 
                // #ifdef APP-NVUE
                dom.getComponentRect(this.$refs['fui_sticky__el'], option => {
                    if (option && option.result && option.size) {
                        this.height = option.size.height + 1
                        this.elTop = option.size.top + (this.scrollTop || 0)
                        callback && callback()
                    }
                })
                // #endif
            }
        }
    }
</script>
 
<style scoped>
    .fui-sticky__wrap {
        /* #ifndef APP-VUE */
        width: 100%;
        /* #endif */
    }
 
    .fui-sticky__fixed {
        /* #ifndef APP-NVUE */
        width: 100%;
        /* #endif */
        position: sticky;
        top: 0;
    }
 
    /* #ifdef MP-QQ || MP-BAIDU || MP-TOUTIAO */
    .fui-sticky__fixed-mp {
        width: 100%;
        position: fixed;
        top: 0;
        left: 0;
    }
 
    .fui-sticky__seat {
        width: 100%;
    }
 
    .fui-sticky__seat-hidden {
        position: fixed;
        left: -9999px;
        z-index: -10;
        visibility: hidden;
    }
 
    /* #endif */
</style>