cl-scroller.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <view class="cl-scroller__wrap">
  3. <view
  4. class="cl-scroller__loading"
  5. :style="{
  6. transform,
  7. transition,
  8. }"
  9. >
  10. <slot name="loading" :text="text" :status="status" :move="touch.move">
  11. <cl-loading :size="20" v-if="status == 'loading'"></cl-loading>
  12. <cl-text :size="26" :margin="[0, 0, 0, 14]" :value="text"></cl-text>
  13. </slot>
  14. </view>
  15. <view
  16. class="cl-scroller"
  17. :style="{
  18. transform,
  19. transition,
  20. }"
  21. @touchmove="onTouchMove"
  22. @touchstart="onTouchStart"
  23. @touchend="onTouchEnd"
  24. >
  25. <scroll-view
  26. class="cl-scroller__view"
  27. scroll-y
  28. :lower-top="bottom"
  29. :scroll-top="scrollTop2"
  30. :scroll-into-view="scrollIntoView"
  31. :scroll-with-animation="scrollWithAnimation"
  32. :enable-back-to-top="enableBackToTop"
  33. :show-scrollbar="showScrollbar"
  34. :enable-flex="enableFlex"
  35. @scroll="onScroll"
  36. @scrolltolower="up"
  37. >
  38. <slot></slot>
  39. </scroll-view>
  40. </view>
  41. <!-- 回到顶部 -->
  42. <view
  43. class="cl-scroller__back-top"
  44. :class="[
  45. {
  46. fadeIn: backTopButtonFadeIn,
  47. },
  48. ]"
  49. @tap="backTop"
  50. v-if="showBackTopButton"
  51. >
  52. <cl-icon name="top" color="#666"></cl-icon>
  53. <text class="cl-scroller__back-top-text">顶部</text>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. /**
  59. * scroller 滚动区域
  60. * @description
  61. * @tutorial https://docs.cool-js.com/uni/components/layout/scroller.html
  62. * @property {Number} top 距离顶部多少px触发
  63. * @property {Number} bottom 距离底部多少px触发
  64. * @property {String} loadingText 正在刷新文案
  65. * @property {String} pullingText 下拉刷新文案
  66. * @property {String} releaseText 释放刷新文案
  67. * @property {Number} scrollTop 滚动条距离顶部位置
  68. * @property {String} scrollIntoView 滚动到对应元素id
  69. * @example 见教程
  70. */
  71. export default {
  72. props: {
  73. // 距离顶部多少px触发
  74. top: {
  75. type: Number,
  76. default: 80,
  77. },
  78. // 距离底部多少px触发
  79. bottom: {
  80. type: Number,
  81. default: 100,
  82. },
  83. // 正在刷新文案
  84. loadingText: {
  85. type: String,
  86. default: "正在刷新",
  87. },
  88. // 下拉刷新文案
  89. pullingText: {
  90. type: String,
  91. default: "下拉刷新",
  92. },
  93. // 释放刷新文案
  94. releaseText: {
  95. type: String,
  96. default: "释放刷新",
  97. },
  98. // 滚动条距离顶部位置
  99. scrollTop: Number,
  100. // 滚动到对应元素id
  101. scrollIntoView: String,
  102. // 在设置滚动条位置时使用动画过渡
  103. scrollWithAnimation: {
  104. type: Boolean,
  105. default: true,
  106. },
  107. // 滚动条返回顶部
  108. enableBackToTop: Boolean,
  109. // 是否显示回到顶部按钮
  110. showBackTopButton: {
  111. type: Boolean,
  112. default: true,
  113. },
  114. // 控制是否出现滚动条
  115. showScrollbar: Boolean,
  116. // 启用 flexbox 布局
  117. enableFlex: Boolean,
  118. // 启用下拉刷新
  119. refresherEnabled: {
  120. type: Boolean,
  121. default: true,
  122. },
  123. },
  124. data() {
  125. return {
  126. touch: {
  127. start: 0,
  128. move: 0,
  129. },
  130. scrollTop2: 0,
  131. backTopButtonFadeIn: false,
  132. status: "end", // pulling, loading, end
  133. };
  134. },
  135. watch: {
  136. scrollTop: {
  137. immediate: true,
  138. handler(val) {
  139. this.scrollTop2 = val || "";
  140. },
  141. },
  142. },
  143. computed: {
  144. transform() {
  145. return this.touch.move ? `translate3d(0, ${this.touch.move}px, 0)` : "";
  146. },
  147. transition() {
  148. return ["end", "loading"].includes(this.status) ? "transform 0.3s" : "";
  149. },
  150. isReleasable() {
  151. return this.touch.move >= this.top;
  152. },
  153. text() {
  154. switch (this.status) {
  155. case "pulling":
  156. return this.isReleasable ? this.releaseText : this.pullingText;
  157. case "loading":
  158. return this.loadingText;
  159. default:
  160. return this.pullingText;
  161. }
  162. },
  163. },
  164. methods: {
  165. onTouchStart(e) {
  166. if (this.status == "end" && this.refresherEnabled) {
  167. this.touch.start = e.changedTouches[0].clientY;
  168. this.status = "pulling";
  169. }
  170. },
  171. onTouchMove(e) {
  172. if (this.status == "pulling" && this.scrollTop2 <= 10) {
  173. let offset = e.changedTouches[0].clientY - this.touch.start;
  174. if (offset <= 200) {
  175. this.touch.move = offset;
  176. }
  177. }
  178. },
  179. onTouchEnd(e) {
  180. if (this.isReleasable) {
  181. this.down();
  182. } else {
  183. this.end();
  184. }
  185. },
  186. // 滚动监听
  187. onScroll(e) {
  188. this.backTopButtonFadeIn = e.detail.scrollTop >= 200;
  189. this.$emit("scroll", e);
  190. },
  191. // 下拉刷新
  192. down() {
  193. uni.createSelectorQuery()
  194. .in(this)
  195. .select(".cl-scroller__loading")
  196. .fields({ size: true }, (d) => {
  197. this.status = "loading";
  198. this.touch.move = d.height;
  199. this.$emit("down");
  200. })
  201. .exec();
  202. },
  203. // 上拉加载
  204. up() {
  205. this.$emit("up");
  206. },
  207. // 收起,结束
  208. end() {
  209. this.status = "end";
  210. this.touch.move = 0;
  211. },
  212. // 滚动到
  213. scrollTo(top) {
  214. this.scrollTop2 = top;
  215. },
  216. // 回到顶部
  217. backTop() {
  218. this.scrollTop2 = Math.random();
  219. },
  220. },
  221. };
  222. </script>