dengjunjie
5 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<template>
  <view class="tn-scroll-view-class tn-scroll-view">
    <scroll-view
      class="scroll-view"
      :style="[scrollViewStyle]"
      scroll-y
      scroll-anchoring
      enable-back-to-top
      :throttle="false"
      :scroll-top="scrollTop"
      :lower-threshold="lowerThreshold"
      @scroll="handleScroll"
      @touchend="handleTouchEnd"
      @touchmove.prevent.stop="handleTouchMove"
      @touchstart="handleTouchStart"
      @scrolltolower="handleScrollTolower"
    >
      <view class="scroll__content" :style="[scrollContentStyle]">
        <view class="scroll__pull-down">
          <slot name="pulldown">
            <view class="scroll__refresh" :style="[refreshStyle]">
              <view><tn-loading :animation="refreshing"></tn-loading></view>
              <view class="scroll__refresh--text" :style="[refreshTextStyle]">{{ refreshStateText }}</view>
            </view>
          </slot>
        </view>
        <view :id="elScrollDataId" class="scroll__data">
          <slot></slot>
        </view>
      </view>
    </scroll-view>
  </view>
</template>
 
<script>
  import componentsColor from '../../libs/mixin/components_color.js'
  export default {
    name: 'tn-scroll-view',
    mixins: [ componentsColor ],
    props: {
      // H5顶部导航栏的高度
      h5NavHeight: {
        type: Number,
        default: 45
      },
      // 自定义顶部导航栏高度
      customNavHeight: {
        type: Number,
        default: 0
      },
      // 可滚动区域顶部偏移高度
      offsetTop: {
        type: Number,
        default: 0
      },
      // 可滚动区域底部偏移高度
      offsetBottom: {
        type: Number,
        default: 0
      },
      // 容器高度 (不设置则自动计算)
      height: {
        type: Number,
        default: null
      },
      // 是否禁用
      disabled: {
        type: Boolean,
        default: false
      },
      // 禁用下拉刷新
      pullDownDisabled: {
        type: Boolean,
        default: false
      },
      // 下拉速率
      pullDownSpeed: {
        type: Number,
        default: 0.5
      },
      // 刷新延迟
      refreshDelayed: {
        type: Number,
        default: 800
      },
      // 刷新完成后延迟
      refreshFinishDelayed: {
        type: Number,
        default: 800
      },
      // 下拉刷新距离
      refresherThreshold: {
        type: Number,
        default: 70
      },
      // 上拉加载距离
      lowerThreshold: {
        type: Number,
        default: 40
      },
      // 刷新状态
      refreshState: {
        type: Boolean,
        default: false
      },
      // 正在刷新文字
      refreshingText: {
        type: String,
        default: '正在刷新'
      },
      // 刷新成功文字
      refreshSuccessText: {
        type: String,
        default: '刷新成功'
      },
      // 下拉中的文字
      pulldownText: {
        type: String,
        default: '下拉刷新'
      },
      // 下拉完成的文字
      pulldownFinishText: {
        type: String,
        default: '松开刷新'
      }
    },
    data() {
      return {
        // 滚动容器内容id
        elScrollDataId: '',
        // 系统信息
        systemInfo: {
          height: 0,
          statusBarHeight: 0
        },
        // 距离顶部滚动高度
        scrollTop: 0,
        // 滚动内容视图顶部位置
        scrollDataTop: -1,
        // 滚动内容视图顶部位置偏移
        scrollDataOffsetTop: -1,
        // 滚动区域的高度
        scrollViewHeight: 0,
        // 当前滚动高度
        currentScrollTop: 0,
        // 当前触摸点Y轴开始坐标
        currentTouchStartY: 0,
        // 刷新状态文字
        refreshStateText: '下拉刷新',
        // 是否刷新中
        refreshing: false,
        // 是否刷新完成
        refreshFinish: false,
        // 是否正在下拉
        pulldowning: false,
        // 下拉高度
        pullDownHeight: 0,
        // 是否显示下拉加载
        showPullDown: false
      }
    },
    computed: {
      scrollViewStyle() {
        let style = {}
        style.height = this.scrollViewHeight + 'px'
        if (!this.backgroundColorClass) {
          style.backgroundColor = this.backgroundColorStyle
        }
        return style
      },
      scrollContentStyle() {
        let style = {}
        style.transform = this.showPullDown ? `translateY(${this.pullDownHeight}px)` : `translateY(0px)`
        style.transition = this.pulldowning ? `transform 100ms ease-out` : `transform 500ms cubic-bezier(0.19,1.64,0.42,0.72)`
        return style
      },
      refreshStyle() {
        let style = {}
        style.opacity = this.showPullDown ? 1 : 0
        return style
      },
      refreshTextStyle() {
        let style = {}
        if (!this.fontColorClass) {
          style.color = this.fontColorStyle
        }
        return style
      },
      loadTextStyle() {
        let style = {}
        if (!this.fontColorClass) {
          style.color = this.fontColorStyle
        }
        return style
      }
    },
    watch: {
      refreshState(nVal, oVal) {
        if (!nVal) {
          if (this.showPullDown) {
            // 关闭正在下拉
            this.pulldowning = false
            // 隐藏下拉刷新
            this.showPullDown = false
            // 关闭正在刷新
            this.refreshing = false
          }
        }
      }
    },
    created() {
      this.elScrollDataId = this.$t.uuid()
      this.getSystemInfo()
    },
    mounted() {
      this.$nextTick(() => {
        this.init()
      })
    },
    methods: {
      // 组件初始化
      init() {
        this.refreshStateText = this.pulldownText
        // 初始化scrollView信息
        this.updateScrollViewInfo()
      },
      // 获取系统信息
      getSystemInfo() {
        const systemInfo = uni.getSystemInfoSync()
        this.systemInfo.height = systemInfo.safeArea.height
        this.systemInfo.statusBarHeight = systemInfo.statusBarHeight
      },
      // 更新scrollView信息
      updateScrollViewInfo() {
        if (this.height) {
          this.scrollViewHeight = this.height
        } else {
          // 设置scrollView的高度和组件顶部位置
          // console.log(this.systemInfo, this.offsetTop, this.customNavHeight);
          // #ifdef H5
          this.scrollViewHeight = this.systemInfo.height - (
            this.offsetTop + 
            (this.customNavHeight ? this.customNavHeight : this.h5NavHeight) + 
            this.offsetBottom)
          this.scrollDataOffsetTop = this.offsetTop + (this.customNavHeight ? this.customNavHeight : this.h5NavHeight)
          // #endif
          // #ifndef H5
          this.scrollViewHeight = this.systemInfo.height - (
            this.offsetTop + 
            this.systemInfo.statusBarHeight +
            this.offsetBottom)
          this.scrollDataOffsetTop = this.offsetTop + this.systemInfo.statusBarHeight
          // #endif
        }
      },
      // 获取scrollView内容信息
      async getScrollDataInfo() {
        const scrollInfo = await this._tGetRect(`#${this.elScrollDataId}`)
        this.scrollDataTop = scrollInfo.top
      },
      // 上拉触底事件
      handleScrollTolower(e) {
        if (this.pullUpDisabled) return
        this.$emit('scrolltolower', e)
      },
      // 滚动事件
      handleScroll(e) {
        this.currentScrollTop = e.detail.scrollTop
        this.$emit('scroll', e.detail)
      },
      // 触摸按下事件
      handleTouchStart(e) {
        if (this.disabled) return
        this.currentTouchStartY = e.touches[0].clientY
        this.getScrollDataInfo()
        this.$emit('touchStart', e)
      },
      // 触摸下滑事件
      handleTouchMove(e) {
        if (this.disabled) return
        if (this.currentScrollTop == 0 && e.touches[0].clientY >= this.currentTouchStartY) {
          // 容器滑动的偏移
          const moveOffset = this.scrollDataTop > 0 ?
            (this.scrollDataOffsetTop - this.scrollDataTop) :
            (Math.abs(this.scrollDataTop) + this.scrollDataOffsetTop)
          
          this.pulldowning = true
          this.showPullDown = true
          let pullDownDistance = ((e.touches[0].clientY - this.currentTouchStartY)  - moveOffset) * this.pullDownSpeed
          this.pullDownHeight = pullDownDistance
          // this.pullDownHeight = pullDownDistance > this.refresherThreshold ? this.refresherThreshold : pullDownDistance
          this.refreshStateText = this.pullDownHeight >= this.refresherThreshold ? this.pulldownFinishText : this.pulldownText
          if (pullDownDistance > this.refresherThreshold) {
            this.$emit('refreshReady')
          }
        }
        this.$emit('touchMove', e)
      },
      // 触摸松开处理
      handleTouchEnd(e) {
        if (this.disabled) return
        // 处理下拉刷新
        if (this.showPullDown) {
          // 当下拉高度小于下拉阈值
          if (this.pullDownHeight < this.refresherThreshold) {
            // 关闭正在下拉
            this.pulldowning = false
            // 重置下拉高度
            this.pullDownHeight = 0
            // 隐藏下拉刷新
            this.showPullDown = false
            // 触发下拉中断事件
            this.$emit('refreshStop')
          } else {
            this.pullDownHeight = this.pullDownHeight > this.refresherThreshold ? this.refresherThreshold : this.pullDownHeight
            this.refresh()
          }
        }
        
        // 触发下拉触摸松开事件
        this.$emit('touchEnd', e)
      },
      
      // 刷新数据
      refresh() {
        // 设置刷新未完成
        this.refreshFinish = false
        // 开启正在刷新
        this.refreshing = true
        // 设置正在刷新状态文字
        this.refreshStateText = this.refreshingText
        // 触发refresh事件
        setTimeout(() => {
          this.$emit('refresh')
        }, this.refreshDelayed)
      },
    }
  }
</script>
 
<style lang="scss" scoped>
  .tn-scroll-view {
    
    .scroll-view {
      position: relative;
      touch-action: none;
      
      .scroll__content {
        display: flex;
        will-change: transform;
        flex-direction: column;
        
        .scroll {
          &__pull-down {
            position: absolute;
            left: 0;
            width: 100%;
            display: flex;
            padding: 30rpx 0;
            align-items: center;
            justify-content: center;
            transform: translateY(-100%);
          }
          
          &__refresh {
            display: flex;
            align-items: center;
            justify-content: center;
            &--text {
              margin-left: 10rpx;
            }
          }
          
          &__data {
            
          }
          
          &__pull-up {
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            transform: translateY(100%);
          }
          &__load {
            padding: 20rpx 0;
            display: flex;
            align-items: center;
            justify-content: center;
            &--text {
              margin-left: 10rpx;
            }
          }
        }
      }
    }
  }
</style>