virtual-scroller.vue 958 B

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