pulldown-refresher.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div class="pulldown-refresher">
  3. <swiper class="pulldown" vertical @touchend="touchend" @transition="pulldown">
  4. <swiper-item class="pulldown-item" :style="{ top: pulldownRefreshingTop + 'px' }">
  5. <slot></slot>
  6. </swiper-item>
  7. </swiper>
  8. <view class="pulldown-text"
  9. :style="{
  10. top: pullDownRefreshDistance / 2 + 'px',
  11. height: pullDownRefreshDistance + 'px',
  12. opacity: pulldownRefreshing ? 1 : pulldownRefreshingTop ? pulldownRefreshingTop / 90 : -pulldownRefresh / 90
  13. }"
  14. >{{ -pulldownRefresh > 90 ? '... 松开手指刷新列表 ...' : (pulldownRefreshing ? '... 刷新中 ...' : '... 继续下拉刷新列表 ...') }}</view>
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. pulldownRefresh: 0, // 下拉刷新容器距离顶部的距离
  22. pullDownStopDistance: 0, //手指松开时下拉刷新容器距离顶部的距离
  23. pulldownRefreshingTop: 0, //手指松开后下拉刷新容器距离顶部的距离
  24. pulldownRefreshing: false, // 页面是否正处于下拉刷新状态
  25. pullDownRefreshDistance: 80 // 下拉刷新距离
  26. }
  27. },
  28. methods: {
  29. pulldown({ detail: { dy } }) { // 下拉刷新
  30. this.pulldownRefresh = dy
  31. if (this.pulldownRefreshing) {
  32. this.pulldownRefreshingTop = (1 - -dy / this.pullDownStopDistance) * this.pullDownRefreshDistance
  33. }
  34. },
  35. touchend() {
  36. if (this.pulldownRefresh < -this.pullDownRefreshDistance) {
  37. this.$emit('pulldownRefresh')
  38. this.pulldownRefreshing = true
  39. this.pullDownStopDistance = -this.pulldownRefresh
  40. }
  41. },
  42. pullup() {
  43. this.pulldownRefreshing = false
  44. const timer = setInterval(() => {
  45. this.pulldownRefreshingTop -= 4
  46. if (this.pulldownRefreshingTop <= 0) {
  47. this.pulldownRefreshingTop = 0
  48. clearInterval(timer)
  49. }
  50. }, 10)
  51. }
  52. }
  53. }
  54. </script>
  55. <style lang="scss" scoped>
  56. .pulldown-refresher {
  57. z-index: 0;
  58. width: 100%;
  59. height: 100%;
  60. position: relative;
  61. .pulldown {
  62. width: 100%;
  63. height: 100%;
  64. position: relative;
  65. .pulldown-item {
  66. position: absolute;
  67. width: 100%;
  68. height: 100%;
  69. }
  70. }
  71. .pulldown-text {
  72. left: 50%;
  73. width: 100%;
  74. z-index: -1;
  75. display: flex;
  76. color: #333333;
  77. font-size: 26rpx;
  78. position: absolute;
  79. align-items: center;
  80. justify-content: center;
  81. transform: translate(-50%, -50%);
  82. }
  83. }
  84. </style>