index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <scroll-view
  3. :class="customClass"
  4. :style="{
  5. //#ifdef APP-PLUS || MP-WEIXIN
  6. height: `calc(100vh - (${!isSticky ? '0px' : '44px'}))`,
  7. //#endif
  8. //#ifdef H5
  9. height: `calc(100vh - (${!isSticky ? '44px' : '88px'}))`,
  10. //#endif
  11. ...customStyle,
  12. }"
  13. :scroll-y="true"
  14. scroll-with-animation
  15. :refresher-threshold="refresherThreshold"
  16. :refresher-default-style="refresherDefaultStyle"
  17. :refresherEnabled="refresherEnabled"
  18. :refresher-triggered="triggered"
  19. :refresher-background="refresherBackground"
  20. :scroll-top="scrollTop"
  21. :scroll-into-view="scrollIntoView"
  22. @refresherrefresh="onRefresh"
  23. @scrolltolower="scrolltolower"
  24. >
  25. <slot name="topLoading" v-if="refresherDefaultStyle === 'none'">
  26. <view
  27. class="topBox"
  28. :style="{
  29. marginTop: '-' + refresherThreshold + 'px',
  30. height: refresherThreshold + 'px',
  31. }"
  32. >
  33. <view class="loader">
  34. <view v-for="(v, i) in 10" :key="v" :style="{ transform: 'rotate(' + i * 36 + 'deg)', animationDelay: v == 10 ? 1 + 's' : '0.' + v + 's' }"> </view>
  35. </view>
  36. <view class="title">
  37. {{ topTis }}
  38. </view>
  39. </view>
  40. </slot>
  41. <slot name="default"> </slot>
  42. <slot name="bottomLoading">
  43. <div class="bottoBox">
  44. <span
  45. v-show="total != 0 && refresherLoad && refresherLoadTitle"
  46. :style="{
  47. marginTop: '20px',
  48. marginBottom: '20px',
  49. }"
  50. >
  51. {{ pageSize >= total ? "没有更多啦~" : isScrolltolower }}
  52. </span>
  53. <span
  54. v-show="total == 0 && refresherEnabled && refresherEnabledTitle && !triggered"
  55. :style="{
  56. marginTop: '20px',
  57. marginBottom: '20px',
  58. }"
  59. >
  60. <image style="width: 160px; height: 160px" src="@/static/images/data.png"></image>
  61. <view>暂无数据</view>
  62. </span>
  63. </div>
  64. </slot>
  65. </scroll-view>
  66. </template>
  67. <script setup>
  68. import { onReady, onLoad, onShow, onNavigationBarButtonTap, onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app";
  69. import { ref, onMounted, inject, shallowRef, reactive, getCurrentInstance, watchEffect, toRefs, toRef, watch, computed } from "vue";
  70. const emit = defineEmits(["load", "refresh"]);
  71. const props = defineProps({
  72. //当前页数量
  73. pageSize: {
  74. type: Number,
  75. default: 30,
  76. },
  77. //数据总数
  78. total: {
  79. type: Number,
  80. default: 0,
  81. },
  82. //设置滚动条位置
  83. scrollTop: {
  84. type: String,
  85. default: "",
  86. },
  87. //设置外部class
  88. customClass: {
  89. type: String,
  90. default: "",
  91. },
  92. //设置外部style
  93. customStyle: {
  94. type: [String, Object],
  95. default: {},
  96. },
  97. //是否开启吸顶高度自适应
  98. isSticky: {
  99. type: Boolean,
  100. default: false,
  101. },
  102. //是否开启上拉加载
  103. refresherLoad: {
  104. type: Boolean,
  105. default: false,
  106. },
  107. //是否显示上拉加载文字
  108. refresherLoadTitle: {
  109. type: Boolean,
  110. default: true,
  111. },
  112. //是否开启下拉刷新
  113. refresherEnabled: {
  114. type: Boolean,
  115. default: false,
  116. },
  117. //是否显示下拉刷新文字
  118. refresherEnabledTitle: {
  119. type: Boolean,
  120. default: true,
  121. },
  122. //距离顶部下拉刷新距离
  123. refresherThreshold: {
  124. type: Number,
  125. default: 44,
  126. },
  127. //是否使用默认下拉刷新样式(支持设置 black、white、none/none 表示不使用默认样式)
  128. refresherDefaultStyle: {
  129. type: String,
  130. default: "black",
  131. },
  132. //设置自定义下拉刷新区域背景颜色
  133. refresherBackground: {
  134. type: String,
  135. default: "#fff",
  136. },
  137. //是否滚动到指定id的位置
  138. scrollIntoView: {
  139. type: String,
  140. default: "",
  141. },
  142. });
  143. const {
  144. pageSize,
  145. total,
  146. scrollTop,
  147. customClass,
  148. customStyle,
  149. refresherLoad,
  150. refresherLoadTitle,
  151. refresherEnabled,
  152. refresherEnabledTitle,
  153. refresherThreshold,
  154. refresherDefaultStyle,
  155. refresherBackground,
  156. } = toRefs(props);
  157. const state = reactive({
  158. StatusBarHeight: computed(() => {
  159. let systemInfo = uni.getSystemInfoSync();
  160. return systemInfo.statusBarHeight + "px";
  161. }),
  162. tabBarHeight: computed(() => {
  163. let systemInfo = uni.getSystemInfoSync();
  164. return systemInfo.screenHeight - systemInfo.safeArea.bottom + "px";
  165. }),
  166. triggered: false,
  167. topTis: "松手刷新",
  168. isScrolltolower: "上拉加载更多",
  169. });
  170. const { StatusBarHeight, tabBarHeight, triggered, topTis, isScrolltolower } = toRefs(state);
  171. /**
  172. * @scrollView上拉刷新
  173. */
  174. function onRefresh() {
  175. isScrolltolower.value = "上拉加载更多";
  176. topTis.value = "努力加载中";
  177. //做一个判断,判断triggered 是否为true
  178. if (!triggered.value) {
  179. triggered.value = true;
  180. setTimeout((e) => {
  181. triggered.value = false;
  182. topTis.value = "松手刷新";
  183. emit("refresh");
  184. }, 1000);
  185. }
  186. }
  187. /**
  188. * @scrollView触底事件
  189. */
  190. function scrolltolower(e) {
  191. if (!refresherLoad.value || pageSize.value >= total.value) {
  192. return;
  193. } else {
  194. isScrolltolower.value = "正在加载中~";
  195. setTimeout(() => {
  196. emit("load");
  197. isScrolltolower.value = "上拉加载更多";
  198. }, 1000);
  199. }
  200. }
  201. onLoad((option) => {});
  202. </script>
  203. <style scoped>
  204. .topBox {
  205. width: 100%;
  206. display: flex;
  207. align-items: center;
  208. justify-content: center;
  209. color: gray;
  210. }
  211. .topBox .loader {
  212. display: flex;
  213. justify-content: center;
  214. align-items: center;
  215. height: 100%;
  216. position: relative;
  217. margin-top: -25rpx;
  218. }
  219. .topBox .loader view {
  220. width: 2px;
  221. height: 6px;
  222. background-color: gray;
  223. transform-origin: 50% 150%;
  224. position: absolute;
  225. animation: color-change 1s infinite;
  226. }
  227. .topBox .title {
  228. position: relative;
  229. margin-left: 35rpx;
  230. color: #c0c4cc;
  231. font-size: 14px;
  232. }
  233. @keyframes color-change {
  234. from {
  235. background-color: gray;
  236. }
  237. to {
  238. background-color: white;
  239. }
  240. }
  241. .bottoBox {
  242. display: flex;
  243. align-items: center;
  244. text-align: center;
  245. justify-content: center;
  246. color: #c0c4cc;
  247. font-size: 14px;
  248. }
  249. </style>