zhangwei
2024-08-14 9d8994790fe403935ed46ad478f83ae110bb7a01
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID: 16 3,营业执照号:  915  1 0  13 1 332  00 619     3K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view class="fui-horizontal__scroll" :style="{marginTop:marginTop+'rpx',marginBottom:marginBottom+'rpx'}"
        ref="fui_horizontal__scroll">
        <!-- #ifdef APP-VUE  || MP-WEIXIN || H5-->
        <scroll-view :show-scrollbar="false" :scroll-x="true" :upper-threshold="0" :lower-threshold="0" :data-width="width" :data-bgwidth="bgWidth"
            :data-blockwidth="blockWidth" @scroll="handler.scroll" @scrolltoupper="handler.scrolltoupper"
            @scrolltolower="handler.scrolltolower" class="fui-hor__scroll-view">
            <view class="fui-hor__scroll-wrap">
                <slot></slot>
            </view>
        </scroll-view>
 
        <!-- #endif -->
 
        <!-- #ifdef APP-NVUE -->
        <scroller class="fui-hor__scroll-view" ref="fui_scroller_view" scroll-direction="horizontal"
            :show-scrollbar="false" :offset-accuracy="2" @scroll="nvueScrollHandler">
            <view class="fui-hor__scroll-wrap">
                <slot></slot>
            </view>
        </scroller>
        <!-- #endif -->
 
        <!-- #ifndef APP-PLUS || MP-WEIXIN || H5-->
        <scroll-view :show-scrollbar="false" :scroll-x="true" :upper-threshold="0" :lower-threshold="0" class="fui-hor__scroll-view"
            @scroll="scrollHandler" @scrolltoupper="scrolltoupper" @scrolltolower="scrolltolower">
            <view class="fui-hor__scroll-wrap">
                <slot></slot>
            </view>
        </scroll-view>
        <!-- #endif -->
        <view class="fui-hor__scrollbar-wrap" :style="{marginTop:scrollGap+'rpx'}" v-if="scroll">
            <view class="fui-hor__scrollbar" :class="{'fui-hor__scroll-radius':scrollCap==='round'}"
                :style="{height:blockHeight+'px',width:bgWidth+'px',background:background}">
                <view class="fui-hor__scroll-indicator"
                    :class="{'fui-hor__scroll-radius':scrollCap==='round','fui-hor__scroll-bg':!scrollBarColor}"
                    :style="getStyles" ref="fui_hor_indicator"></view>
            </view>
        </view>
    </view>
</template>
<!-- #ifdef APP-VUE || MP-WEIXIN || H5-->
<script src="./index.wxs" module="handler" lang="wxs"></script>
<!-- #endif -->
<script>
    // #ifdef APP-NVUE
    const dom = uni.requireNativePlugin('dom');
    // #endif
    import bindingx from './bindingx.js'
    import mpjs from './mpjs.js'
    export default {
        name: "fui-horizontal-scroll",
        mixins: [mpjs, bindingx],
        emits: ['scrolltoupper', 'scrolltolower'],
        props: {
            marginTop: {
                type: [String, Number],
                default: 0
            },
            marginBottom: {
                type: [String, Number],
                default: 0
            },
            // 是否显示滚动条
            scroll: {
                type: Boolean,
                default: true
            },
            // 滚动条区域宽度/长度
            scrollWidth: {
                type: [String, Number],
                default: 96
            },
            // 滚动条的宽度
            scrollBarWidth: {
                type: [String, Number],
                default: 32
            },
            // 滚动区域/滚动条高度
            scrollHeight: {
                type: [String, Number],
                default: 8
            },
            //滚动条两端样式,可选值为 square、round
            scrollCap: {
                type: String,
                default: 'round'
            },
            scrollBarColor: {
                type: String,
                default: ''
            },
            background: {
                type: String,
                default: '#EEEEEE'
            },
            //自定义滚动条距离上方内容间距
            scrollGap: {
                type: [String, Number],
                default: 24
            }
        },
        data() {
            return {
                width: 0,
                //滚动条背景长度
                bgWidth: 0,
                //滚动条滑块长度
                blockWidth: 0,
                blockHeight: 0,
                transform: '',
                isMounted: false
            };
        },
        created() {
            this.bgWidth = this.getPx(this.scrollWidth || 96)
            this.blockWidth = this.getPx(this.scrollBarWidth || 32)
            this.blockHeight = this.getPx(this.scrollHeight || 8)
        },
        mounted() {
            this.isMounted = true
            this.$nextTick(() => {
                this.init()
            })
        },
        // #ifdef H5
        updated() {
            if(!this.isMounted && !this.width){
                setTimeout(() => {
                    this.init()
                }, 0)
            }
        },
        // #endif
        watch: {
            scrollWidth(val) {
                this.bgWidth = this.getPx(val || 96)
            },
            scrollBarWidth(val) {
                this.blockWidth = this.getPx(this.scrollBarWidth || 32)
            },
            scrollHeight(val) {
                this.blockHeight = this.getPx(this.scrollHeight || 8)
            }
        },
        computed: {
            getStyles() {
                let style = `height:${this.blockHeight}px;width:${this.blockWidth}px;`
                let color = this.scrollBarColor;
                // #ifdef APP-NVUE
                if (!color || color === true) {
                    const app = uni && uni.$fui && uni.$fui.color;
                    color = (app && app.primary) || '#465CFF';
                }
                // #endif
                if (color && color !== true) {
                    style += `background:${color};`
                }
                // #ifndef APP-PLUS || MP-WEIXIN
                style += `transform:${this.transform}`
                // #endif
                return style
            }
        },
        methods: {
            //如果初始化有误,可调用此方法重新初始化
            init() {
                setTimeout(() => {
                    this._getSize()
                }, 80);
            },
            getPx(rpx) {
                let px = Math.floor(uni.upx2px(Number(rpx)));
                px = px % 2 === 0 ? px : px + 1
                return px;
            },
            scrollEvent(edge) {
                const event = edge === 'left' ? 'scrolltoupper' : 'scrolltolower'
                this.$emit(event)
            },
            _getSize() {
                // #ifndef APP-NVUE
                uni.createSelectorQuery()
                    // #ifndef MP-ALIPAY
                    .in(this)
                    // #endif
                    .select('.fui-horizontal__scroll')
                    .boundingClientRect()
                    .exec(ret => {
                        if (ret) {
                            this.width = ret[0].width || 0
                        }
                    })
                // #endif
                // #ifdef APP-NVUE
                dom.getComponentRect(this.$refs['fui_horizontal__scroll'], (ret) => {
                    const size = ret.size
                    if (size) {
                        this.width = size.width
                    }
                })
                // #endif
            }
        }
    }
</script>
 
<style scoped>
    /* #ifndef APP-NVUE */
    ::-webkit-scrollbar {
        width: 0 !important;
        height: 0 !important;
        color: transparent !important;
        display: none;
    }
 
    /* #endif */
    .fui-horizontal__scroll,
    .fui-hor__scroll-view,
    .fui-hor__scroll-wrap {
        /* #ifndef APP-NVUE */
        display: flex;
        flex-shrink: 0;
        /* #endif */
        flex-direction: row;
    }
 
    .fui-horizontal__scroll {
        flex-direction: column;
        align-items: stretch;
        /* #ifndef APP-NVUE */
        align-content: flex-start;
        /* #endif */
    }
 
    .fui-hor__scrollbar-wrap {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        justify-content: center;
    }
 
    .fui-hor__scrollbar {
        position: relative;
        overflow: hidden;
        /* #ifndef APP-NVUE */
        transform: translateZ(0);
        /* #endif */
 
    }
 
    .fui-hor__scroll-radius {
        border-radius: 100px;
    }
 
    .fui-hor__scroll-indicator {
        position: absolute;
        left: 0;
        top: 0;
        /* #ifndef APP-NVUE */
        transition: all 0.1s linear;
        /* #endif */
    }
 
    /* #ifndef APP-NVUE */
    .fui-hor__scroll-bg {
        background-color: var(--fui-color-primary, #465CFF) !important;
    }
 
    /* #endif */
</style>