Crunchy
2025-04-29 e5454b769d44a34af423bf87ac8a740bf8c20341
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<template>
  <div class="scroll-pagination"  ref="content" @scroll="onScroll">
    <slot></slot>
    <el-button
        v-if="!finishLoding&&isLoding"
        type="text"
        style="display: flex; margin: 0 auto; color: #909399"
        ><i class="el-icon-loading" style="font-size:20px"></i
      ></el-button>
    <el-button
      type="text"
      v-show="finishLoding"
      style="display: flex; margin: 0 auto; color: #909399"
      >已经没有更多啦~</el-button
    >
  </div>
</template>
 
<script>
export default {
  name: 'ScrollPagination',
  props: {
    finishLoding: {
      type:Boolean,
      default:false
    },
    list:{
      type:Array,
      default:()=>[]
    }
  },
  watch: {
    list:{
      deep:true,
      handler(){
        this.isLoding = false
      }
    }
  },
  data() {
    return {
      isLoding: false,
    }
  },
  created(){
    // 防抖
    this.onScroll = this.debounce(this.scrollFn,500);
    // 节流
    // this.onScroll = this.throttle(this.scrollFn,1000);
  },
  mounted(){
  },
  methods: {
    onScroll(){},
    scrollFn() {
      let content = this.$refs.content
      if (content.scrollTop + content.clientHeight+2 >= content.scrollHeight) {
        if(!this.finishLoding){
          this.loadMore()
        }else{
          this.isLoding = false
        }
      }
    },
    loadMore(){
      if (this.isLoding) return
      this.isLoding = true
      setTimeout(() => {
        this.$emit('load')
      }, 500)
    },
    debounce(func, delay) {
      let timer = null;
      return function () {
        if (timer) {
          clearTimeout(timer);
          timer = null;
        }
        timer = setTimeout(() => {
          func(...arguments);
        }, delay);
      };
    },
    throttle(func, delay) {
      let time = null;
      return function () {
        let args = Array.from(arguments);
        if (time === null) {
          time = setTimeout(() => {
            func(...args);
            clearTimeout(time);
            time = null;
          }, delay);
        }
      };
    }
  }
}
</script>
 
<style scoped>
.scroll-pagination {
  height: 100%;
  overflow-y: auto;
}
</style>