wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
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
<template>
    <view class="cl-scroller__wrap">
        <view
            class="cl-scroller__loading"
            :style="{
                transform,
                transition,
            }"
        >
            <slot name="loading" :text="text" :status="status" :move="touch.move">
                <cl-loading :size="35" v-if="status == 'loading'"></cl-loading>
                <cl-text :size="26" :margin="[0, 0, 0, 14]" :value="text"></cl-text>
            </slot>
        </view>
 
        <view
            class="cl-scroller"
            :style="{
                transform,
                transition,
            }"
            @touchmove.stop="onTouchMove"
            @touchstart="onTouchStart"
            @touchend="onTouchEnd"
        >
            <scroll-view
                class="cl-scroller__view"
                scroll-y
                :lower-top="bottom"
                :scroll-top="scrollTop2"
                :scroll-into-view="scrollIntoView"
                :scroll-with-animation="scrollWithAnimation"
                :enable-back-to-top="enableBackToTop"
                :show-scrollbar="showScrollbar"
                :enable-flex="enableFlex"
                @scroll="onScroll"
                @scrolltolower="up"
            >
                <slot></slot>
            </scroll-view>
        </view>
 
        <!-- 回到顶部 -->
        <view
            class="cl-scroller__back-top"
            :class="[
                {
                    fadeIn: backTopButtonFadeIn,
                },
            ]"
            @tap="backTop"
            v-if="showBackTopButton"
        >
            <cl-icon name="top" color="#666"></cl-icon>
            <text class="cl-scroller__back-top-text">{{ $t("顶部") }}</text>
        </view>
    </view>
</template>
 
<script lang="ts">
import { computed, defineComponent, getCurrentInstance, reactive, ref, watch } from "vue";
import { t } from "/@/locale";
 
export default defineComponent({
    props: {
        // 距离顶部多少px触发
        top: {
            type: Number,
            default: 80,
        },
        // 距离底部多少px触发
        bottom: {
            type: Number,
            default: 100,
        },
        // 正在刷新文案
        loadingText: {
            type: String,
            default: t("正在刷新"),
        },
        // 下拉刷新文案
 
        pullingText: {
            type: String,
            default: t("下拉刷新"),
        },
        // 释放刷新文案
 
        releaseText: {
            type: String,
            default: t("释放刷新"),
        },
        // 滚动条距离顶部位置
        scrollTop: Number,
        // 滚动到对应元素id
        scrollIntoView: String,
 
        // 滚动是否动画
        scrollWithAnimation: {
            type: Boolean,
            default: true,
        },
        // 点击回顶部
        enableBackToTop: Boolean,
        // 是否显示返回顶部按钮
        showBackTopButton: {
            type: Boolean,
            default: true,
        },
        // 是否显示滚动条
        showScrollbar: Boolean,
        // 开启 flex 布局
        enableFlex: Boolean,
        // 开启刷新
        refresherEnabled: {
            type: Boolean,
            default: true,
        },
    },
 
    emits: ["down", "up", "scroll"],
 
    setup(props, { emit }) {
        const { proxy }: any = getCurrentInstance();
 
        // 按下
        const touch = reactive({
            start: 0,
            move: 0,
        });
 
        // 滚动距离
        const scrollTop2 = ref(0);
 
        watch(
            () => props.scrollTop,
            (val) => {
                scrollTop2.value = val || 0;
            },
            {
                immediate: true,
            }
        );
 
        // 回到顶部
        const backTopButtonFadeIn = ref(false);
 
        // 状态
        const status = ref("end");
 
        // 过渡效果
        const transform = computed(() => {
            return touch.move ? `translate3d(0, ${touch.move}px, 0)` : "";
        });
 
        // 动画
        const transition = computed(() => {
            return ["end", "loading"].includes(status.value) ? "transform 0.3s" : "";
        });
 
        // 是否可释放
        const isReleasable = computed(() => touch.move >= props.top);
 
        //文案
        const text = computed(() => {
            switch (status.value) {
                case "pulling":
                    return isReleasable.value ? props.releaseText : props.pullingText;
                case "loading":
                    return props.loadingText;
                default:
                    return props.pullingText;
            }
        });
 
        // 滚动开始
        function onTouchStart(e: TouchEvent) {
            if (status.value == "end" && props.refresherEnabled) {
                touch.start = e.changedTouches[0].clientY;
                status.value = "pulling";
            }
        }
 
        // 滚动中
        function onTouchMove(e: TouchEvent) {
            if (status.value == "pulling" && scrollTop2.value <= 10) {
                let offset = e.changedTouches[0].clientY - touch.start;
 
                if (offset <= 200) {
                    touch.move = offset;
                }
            }
        }
 
        // 滚动结束
        function onTouchEnd() {
            if (isReleasable.value) {
                down();
            } else {
                end();
            }
        }
 
        // 滚动监听
        function onScroll(e: any) {
            backTopButtonFadeIn.value = e.detail.scrollTop >= 200;
            emit("scroll", e);
        }
 
        // 下拉刷新
        function down() {
            uni.createSelectorQuery()
                .in(proxy)
                .select(".cl-scroller__loading")
                .fields({ size: true }, (d: any) => {
                    status.value = "loading";
                    touch.move = d.height || 0;
                    emit("down", end);
                })
                .exec();
        }
 
        // 上拉加载
        function up() {
            emit("up");
        }
 
        // 收起,结束
        function end() {
            status.value = "end";
            touch.move = 0;
        }
 
        // 滚动到
        function scrollTo(top: number) {
            scrollTop2.value = top;
        }
 
        // 回到顶部
        function backTop() {
            scrollTop2.value = Math.random();
        }
 
        return {
            touch,
            scrollTop2,
            backTopButtonFadeIn,
            status,
            transform,
            transition,
            isReleasable,
            text,
            onScroll,
            onTouchStart,
            onTouchMove,
            onTouchEnd,
            down,
            up,
            end,
            scrollTo,
            backTop,
        };
    },
});
</script>