VirtualScroller.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <el-scrollbar ref="scrollbar">
  3. <div v-for="(item, idx) in items" :key="idx">
  4. <slot :item="item" v-if=" idx < showMaxIdx">
  5. </slot>
  6. </div>
  7. </el-scrollbar>
  8. </template>
  9. <script>
  10. export default {
  11. name: "virtualScroller",
  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. init() {
  30. this.page = 1;
  31. this.initEvent();
  32. },
  33. initEvent() {
  34. if (!this.isInitEvent) {
  35. let scrollWrap = this.$refs.scrollbar.$el.querySelector('.el-scrollbar__wrap');
  36. scrollWrap.addEventListener('scroll', this.onScroll);
  37. this.isInitEvent = true;
  38. }
  39. },
  40. onScroll(e) {
  41. const scrollbar = e.target;
  42. // 滚到底部
  43. if (scrollbar.scrollTop + scrollbar.clientHeight >= scrollbar.scrollHeight - 30) {
  44. if(this.showMaxIdx >= this.items.length ){
  45. this.showTip();
  46. }else{
  47. this.page++;
  48. }
  49. }
  50. },
  51. showTip(){
  52. // 提示限制最多3秒显示一次
  53. if(!this.lockTip){
  54. this.$message.success("已到滚动到底部")
  55. this.lockTip = true;
  56. setTimeout(()=>{
  57. this.lockTip = false;
  58. },3000)
  59. }
  60. }
  61. },
  62. computed: {
  63. showMaxIdx() {
  64. return Math.min(this.page * this.size, this.items.length);
  65. }
  66. },
  67. mounted(){
  68. this.initEvent();
  69. }
  70. }
  71. </script>
  72. <style scoped></style>