u-pull-refresh.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <view
  3. class="u-pull-refresh"
  4. @touchstart="onTouchStart"
  5. @touchmove="onTouchMove"
  6. @touchend="onTouchEnd"
  7. @touchcancel="onTouchEnd"
  8. >
  9. <!-- 下拉刷新区域 -->
  10. <view
  11. class="refresh-area"
  12. :style="{ height: refreshDistance + 'px' }"
  13. :class="{ refreshing: isRefreshing }"
  14. >
  15. <!-- 不同状态的插槽 -->
  16. <slot
  17. v-if="refreshStatus === 'pull'"
  18. name="pull"
  19. :distance="refreshDistance"
  20. :threshold="threshold"
  21. >
  22. <!-- 默认下拉状态 -->
  23. <view class="refresh-content">
  24. <view class="refresh-indicator">
  25. <up-icon name="arrow-downward" size="26px"></up-icon>
  26. </view>
  27. <text class="refresh-text">{{ t("up.pullRefresh.pull") }}</text>
  28. </view>
  29. </slot>
  30. <slot
  31. v-else-if="refreshStatus === 'release'"
  32. name="release"
  33. :distance="refreshDistance"
  34. :threshold="threshold"
  35. >
  36. <!-- 默认释放状态 -->
  37. <view class="refresh-content">
  38. <view class="refresh-indicator">
  39. <up-icon name="arrow-upward" size="26px"></up-icon>
  40. </view>
  41. <text class="refresh-text">{{ t("up.pullRefresh.release") }}</text>
  42. </view>
  43. </slot>
  44. <slot
  45. v-else-if="refreshStatus === 'refreshing'"
  46. name="refreshing"
  47. >
  48. <!-- 默认刷新中状态 -->
  49. <view class="refresh-content">
  50. <view class="refresh-indicator">
  51. <view class="spinner"></view>
  52. </view>
  53. <text class="refresh-text">{{ t("up.pullRefresh.refreshing") }}...</text>
  54. </view>
  55. </slot>
  56. </view>
  57. <!-- 内容区域 -->
  58. <view
  59. class="refresh-content-wrapper"
  60. :style="{ transform: `translateY(${contentTranslateY}px)` }"
  61. >
  62. <scroll-view
  63. v-if="useScrollView"
  64. class="scroll-wrapper"
  65. :scroll-y="true"
  66. :enable-back-to-top="enableBackToTop"
  67. :scroll-top="scrollTop"
  68. :lower-threshold="lowerThreshold"
  69. @scroll="handleScroll"
  70. @scrolltolower="handleScrollToLower"
  71. >
  72. <slot></slot>
  73. <!-- 使用 u-loadmore 组件实现上拉加载更多 -->
  74. <u-loadmore
  75. v-if="showLoadmore"
  76. v-bind="loadmoreProps"
  77. />
  78. </scroll-view>
  79. <view v-else>
  80. <slot></slot>
  81. <!-- 使用 u-loadmore 组件实现上拉加载更多 -->
  82. <u-loadmore
  83. v-if="showLoadmore"
  84. v-bind="loadmoreProps"
  85. />
  86. </view>
  87. </view>
  88. </view>
  89. </template>
  90. <script>
  91. import { t } from '../../libs/i18n'
  92. export default {
  93. name: 'u-pull-refresh',
  94. props: {
  95. // 是否正在刷新
  96. refreshing: {
  97. type: Boolean,
  98. default: false
  99. },
  100. // 下拉刷新阈值
  101. threshold: {
  102. type: Number,
  103. default: 80
  104. },
  105. // 阻尼系数
  106. damping: {
  107. type: Number,
  108. default: 0.4
  109. },
  110. // 最大下拉距离
  111. maxDistance: {
  112. type: Number,
  113. default: 120
  114. },
  115. // 是否显示加载更多
  116. showLoadmore: {
  117. type: Boolean,
  118. default: false
  119. },
  120. // u-loadmore 组件的 props 配置
  121. loadmoreProps: {
  122. type: Object,
  123. default: () => ({
  124. status: 'loadmore',
  125. // loadmoreText: '加载更多',
  126. // loadingText: '正在加载...',
  127. // nomoreText: '没有更多了'
  128. })
  129. },
  130. // 是否使用 scroll-view 包装内容
  131. useScrollView: {
  132. type: Boolean,
  133. default: true
  134. },
  135. // scroll-view 相关属性
  136. enableBackToTop: {
  137. type: Boolean,
  138. default: false
  139. },
  140. lowerThreshold: {
  141. type: [Number, String],
  142. default: 50
  143. },
  144. scrollTop: {
  145. type: [Number, String],
  146. default: 0
  147. }
  148. },
  149. data() {
  150. return {
  151. // 下拉刷新相关
  152. isRefreshing: false,
  153. refreshStatus: 'pull', // pull, release, refreshing
  154. refreshDistance: 0,
  155. startY: 0,
  156. currentY: 0,
  157. touching: false,
  158. // 动画相关
  159. contentTranslateY: 0
  160. }
  161. },
  162. emits: ['refresh', 'loadmore', 'scroll'],
  163. watch: {
  164. refreshing: {
  165. handler(newVal) {
  166. if (!newVal) {
  167. this.finishRefresh()
  168. } else {
  169. this.startRefresh()
  170. }
  171. }
  172. }
  173. },
  174. methods: {
  175. t,
  176. // 触摸开始
  177. onTouchStart(e) {
  178. if (this.isRefreshing) return
  179. this.touching = true
  180. this.startY = e.touches[0].pageY
  181. this.currentY = this.startY
  182. this.refreshStatus = 'pull'
  183. },
  184. // 触摸移动
  185. onTouchMove(e) {
  186. if (!this.touching || this.isRefreshing) return
  187. this.currentY = e.touches[0].pageY
  188. const diff = this.currentY - this.startY
  189. // 只有在顶部且下拉时才触发下拉刷新
  190. if (diff > 0 && this.isScrollViewAtTop()) {
  191. this.refreshDistance = Math.min(diff * this.damping, this.maxDistance)
  192. this.contentTranslateY = this.refreshDistance
  193. // 更新状态
  194. if (this.refreshDistance >= this.threshold) {
  195. this.refreshStatus = 'release'
  196. } else {
  197. this.refreshStatus = 'pull'
  198. }
  199. // 阻止默认滚动行为,防止触发页面级滚动
  200. e.preventDefault()
  201. e.stopPropagation()
  202. }
  203. },
  204. // 触摸结束
  205. onTouchEnd() {
  206. if (!this.touching) return
  207. this.touching = false
  208. if (this.refreshDistance >= this.threshold && !this.isRefreshing) {
  209. // 触发刷新
  210. this.startRefresh()
  211. this.$emit('refresh')
  212. } else {
  213. // 回弹
  214. this.resetRefresh()
  215. }
  216. },
  217. // 开始刷新
  218. startRefresh() {
  219. this.isRefreshing = true
  220. this.refreshStatus = 'refreshing'
  221. this.refreshDistance = this.threshold
  222. this.contentTranslateY = this.threshold
  223. },
  224. // 完成刷新
  225. finishRefresh() {
  226. this.isRefreshing = false
  227. this.refreshStatus = 'pull'
  228. this.resetRefresh()
  229. },
  230. // 重置刷新状态
  231. resetRefresh() {
  232. this.refreshDistance = 0
  233. this.contentTranslateY = 0
  234. },
  235. // 检查 scroll-view 是否在顶部
  236. isScrollViewAtTop() {
  237. // 这里可以更精确地判断,但简单起见直接返回 true
  238. // 实际项目中可能需要通过 scroll 事件获取 scrollTop 判断
  239. return true
  240. },
  241. // 处理滚动事件
  242. handleScroll(e) {
  243. this.$emit('scroll', e)
  244. },
  245. // 处理滚动到底部事件
  246. handleScrollToLower(e) {
  247. // 只有当 loadmore 状态为 loadmore 时才触发
  248. if (this.showLoadmore && this.loadmoreProps.status === 'loadmore') {
  249. this.$emit('loadmore')
  250. }
  251. }
  252. }
  253. }
  254. </script>
  255. <style scoped lang="scss">
  256. .u-pull-refresh {
  257. position: relative;
  258. height: 100%;
  259. overflow: hidden;
  260. }
  261. .refresh-area {
  262. position: absolute;
  263. left: 0;
  264. right: 0;
  265. top: 0;
  266. display: flex;
  267. align-items: flex-end;
  268. justify-content: center;
  269. overflow: hidden;
  270. transition: height 0.2s ease-out;
  271. }
  272. .refresh-content-wrapper {
  273. height: 100%;
  274. transition: transform 0.2s ease-out;
  275. }
  276. .scroll-wrapper {
  277. height: 100%;
  278. }
  279. .refresh-content {
  280. display: flex;
  281. flex-direction: column;
  282. align-items: center;
  283. justify-content: center;
  284. width: 100%;
  285. padding-bottom: 10px;
  286. }
  287. .spinner {
  288. width: 16px;
  289. height: 16px;
  290. border: 2px solid #f3f3f3;
  291. border-top: 2px solid #666;
  292. border-radius: 50%;
  293. animation: spin 1s linear infinite;
  294. }
  295. @keyframes spin {
  296. 0% { transform: rotate(0deg); }
  297. 100% { transform: rotate(360deg); }
  298. }
  299. .refresh-text {
  300. font-size: 14px;
  301. color: #666;
  302. }
  303. </style>