virtual-scroller.vue 1003 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <scroll-view scroll-y="true" :style="{height: height}" upper-threshold="200" @scrolltolower="onScrollToBottom"
  3. scroll-with-animation="true">
  4. <view v-for="(item, idx) in showItems" :key="idx">
  5. <slot :item="item">
  6. </slot>
  7. </view>
  8. </scroll-view>
  9. </template>
  10. <script>
  11. export default {
  12. name: "virtual-scroller",
  13. data() {
  14. return {
  15. page: 1,
  16. isInitEvent: false,
  17. lockTip: false
  18. }
  19. },
  20. props: {
  21. height: {
  22. type: String,
  23. default: '60vh'
  24. },
  25. items: {
  26. type: Array
  27. },
  28. size: {
  29. type: Number,
  30. default: 30
  31. }
  32. },
  33. methods: {
  34. onScrollToBottom(e) {
  35. if (this.showMaxIdx >= this.items.length) {
  36. this.showTip();
  37. } else {
  38. this.page++;
  39. }
  40. },
  41. showTip() {
  42. uni.showToast({
  43. title: "已滚动至底部",
  44. icon: 'none'
  45. });
  46. }
  47. },
  48. computed: {
  49. showMaxIdx() {
  50. return Math.min(this.page * this.size, this.items.length);
  51. },
  52. showItems() {
  53. return this.items.slice(0, this.showMaxIdx);
  54. }
  55. }
  56. }
  57. </script>
  58. <style scoped></style>