vScroll.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <scroll-view
  3. :scroll-y="true"
  4. @scrolltolower="load"
  5. @scroll="scroll"
  6. @refresherrefresh="onRefresh"
  7. :refresher-enabled="refresherEnabled&&scrollTop<50"
  8. :refresher-triggered="refreshing"
  9. refresher-background="transparent"
  10. :refresher-threshold="200"
  11. class="sroll-view11"
  12. style="height:calc(100vh - 100px)"
  13. >
  14. <slot></slot>
  15. <view v-if="loading" class="fn-center p-y-xs">
  16. <van-loading size="30rpx">
  17. {{$t('common.loadMore')}}...
  18. </van-loading>
  19. </view>
  20. <view v-else-if="finished&&listLen" class="fn-center p-y-xs">
  21. {{$t('common.notMore')}}
  22. </view>
  23. <view v-else-if="finished&&!listLen" class="fn-center p-y-xs">
  24. {{$t('common.notData')}}
  25. </view>
  26. </scroll-view>
  27. </template>
  28. <script>
  29. export default {
  30. name: "vScroll",
  31. props: {
  32. offset: {
  33. default: 300,
  34. required: false
  35. },
  36. refresherEnabled:{
  37. default:true,
  38. required:false,
  39. type:Boolean
  40. },
  41. },
  42. data() {
  43. return {
  44. loading: false,
  45. refreshing: false,
  46. finished: false,
  47. scrollTop:0,
  48. listLen:0
  49. };
  50. },
  51. methods: {
  52. scroll(event){
  53. this.scrollTop = event.detail.scrollTop
  54. },
  55. onRefresh($ev) {
  56. // 触发刷新并暴露结束函数
  57. if(this.refreshing) return;
  58. this.refreshing = true;
  59. this.$emit("ref", callObj => {
  60. this.refreshing = false;
  61. if(!callObj) return;
  62. if (this.isBoolean(callObj.finished)) {
  63. this.finished = callObj.finished;
  64. this.listLen = callObj.listLen
  65. }else{
  66. this.finished = true
  67. this.listLen = 0
  68. }
  69. });
  70. },
  71. isBoolean(ev) {
  72. return typeof ev == "boolean";
  73. },
  74. load() {
  75. // 触发加载并暴露结束函数
  76. if(this.loading||this.finished) return;
  77. this.loading = true;
  78. this.$emit("load", callObj => {
  79. this.loading = false;
  80. if(!callObj) return;
  81. if (this.isBoolean(callObj.finished)) {
  82. this.finished = callObj.finished;
  83. this.listLen = callObj.listLen
  84. }else if (this.isBoolean(callObj.error)) {
  85. this.finished = callObj.error;
  86. this.listLen = 0
  87. }
  88. });
  89. }
  90. },
  91. mounted() {
  92. this.load()
  93. },
  94. };
  95. </script>