pulldown-refresher.vue 2.5 KB

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