licp
2024-04-24 70b128785737df0d6065b09a75cf79b49efe22fa
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
<template>
  <div class="scroll-pagination"  ref="content" @scroll="scrollFn">
    <slot></slot>
    <el-button
        v-if="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-if="finishLoding"
      style="display: flex; margin: 0 auto; color: #909399"
      >已经没有更多啦~</el-button
    >
  </div>
</template>
 
<script>
export default {
  name: 'ScrollPagination',
  props: {
    finishLoding: {
      type:Boolean,
      default:false
    }
  },
  data() {
    return {
      isLoding: false,
    }
  },
  mounted(){
    window.addEventListener("scroll", this.throttle(this.scrollFn, 20000));
  },
  methods: {
    scrollFn() {
      let content = this.$refs.content
      if (content.scrollTop + content.clientHeight >= content.scrollHeight) {
        if(!this.finishLoding){
          this.loadMore()
        }else{
          this.isLoding = false
        }
      }
    },
    loadMore(){
      if (this.isLoding) return
      this.isLoding = true
      // Simulate loading data (replace with your own API call)
      setTimeout(() => {
        this.$emit('load')
        this.isLoding = false
      }, 1000)
    },
    throttle(fn, wait) {
      // 封装函数进行节流
      var timer = null;
      return function () {
        var context = this;
        var args = arguments;
        if (!timer) {
          timer = setTimeout(function () {
            fn.apply(context, args);
            timer = null;
          }, wait);
        }
      };
    },
  },
  destroyed() {
    window.removeEventListener("scroll", this.throttle(), false);
  },
}
</script>
 
<style scoped>
.scroll-pagination {
  height: 100%;
  overflow-y: auto;
}
</style>