zhaoxiaoqiang
2022-11-14 96cd6c4657ac4d5e175a58bb92da7302e435b066
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
<!--
 * @Author: 小明丶
 * @Date: 2020-01-07 09:27:05
 * @LastEditors: 小明丶
 * @LastEditTime: 2020-10-24 14:41:23
 * @Description: 下滑分页组件 为保证不重复请求 使用时要根据显示内容的div高度确定一次返回条数,数据足够的情况下一次放回请装满容器
 -->
<template>
    <div class="h-list" :style="{'height':height,'background':background}">
        <div class="box" ref="lisBox">
            <slot></slot>
            <p v-if="loading" class="load">加载中....</p>
            <p v-if="finished" class="finish">没有更多了</p>
        </div>
   </div>
</template>
<script>
export default {
    name:'h-list',
    /**
     * defScroll-滚动条距离底部小于此距离执行加载,可根据需求调整距离(必传)
     * finished-是否已经等到所有数据(必传)
     * loading-是否显示加载(必传)
     */
    props:['defScroll','finished','loading','height','background'],
    data() {
        return {
           previous:0,
           now:0,
        }
    },
    mounted(){
        document.querySelector('.h-list').addEventListener('scroll', this.boxScroll,true)
        //window.addEventListener('scroll', this.boxScroll,true)
        console.log(this.defScroll)
    },
    methods:{
        boxScroll(e){
            this.now = new Date().getTime()
            // 获取滚动条距离容器底部的距离
            let scrollBottom =
                e.target.scrollHeight -
                e.target.scrollTop -
                e.target.clientHeight;
            if(this.now-this.previous > 100){
                if(scrollBottom < this.defScroll && !this.finished && scrollBottom!=0){
                    this.loading = true
                    this.$emit('scroll')
                    this.previous = this.now
                    return
                }else{
                    this.loading = false
                    return
                }
            }
        },
    }
}
</script>
<style lang="less" scoped>
.h-list::-webkit-scrollbar,.withdrawals-box::-webkit-scrollbar {
        display: none;
    }
.h-list{
    overflow: scroll;
    height: 75vh;
    background-color: #fff;
    width: 100%;
    padding-bottom: 20px;
    display: flex;
    justify-content: center;
    .load,.finish{
        text-align: center;
    }
}
</style>