vue 中通过监测滚动条加载数据(懒加载数据)

vue 中监测滚动条加载数据(懒加载数据)

methods: {
    titleAlert(title) {
        console.log(title);
        alert(title)
    },
    // 获取滚动条当前的位置
    getScrollTop() {
        let scrollTop = 0
        if (document.documentElement && document.documentElement.scrollTop) {
            scrollTop = document.documentElement.scrollTop
        } else if (document.body) {
            scrollTop = document.body.scrollTop
        }
        return scrollTop
    },
    // 获取当前可视范围的高度
    getClientHeight() {
        let clientHeight = 0
        if (document.body.clientHeight && document.documentElement.clientHeight) {
            clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight)
        } else {
            clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight)
        }
        return clientHeight
    },

    // 获取文档完整的高度
    getScrollHeight() {
        return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
    },
    // 滚动事件触发下拉加载
    onScroll() {
        if (this.getScrollHeight() - this.getClientHeight() - this.getScrollTop() <= 0) {
            if (this.status <= 5) {
                this.status++;
                // 调用请求函数
                this.axios.get('/rock/book/recommend?start=128&count=16&user_type=3&top_count=0&filter_count=6'
                ).then(data => {
                    this.data = this.data.concat(data.data.items)
                    // console.log(data.data.items);
                    console.log(this.data)
                });
            } else {
                alert('没有更多内容了!!!');
            }
        }
    },
},

mounted钩子函数监听滚动事件

mounted() {
    this.$nextTick(function () {
        window.addEventListener('scroll', this.onScroll);
        console.log(this.data)
    })
},
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue实现滚动条加载数据的一种常见做法是使用Intersection Observer API来监听滚动事件,并根据滚动位置来触发数据加载。以下是一个基本的示例代码: 首先,在Vue组件添加一个滚动容器和一个占位符元素: ```html <template> <div class="scroll-container" ref="scrollContainer" @scroll="handleScroll"> <ul> <!-- 数据列表 --> <li v-for="item in items" :key="item.id">{{ item.name }}</li> <!-- 占位符元素 --> <li ref="placeholder"></li> </ul> </div> </template> ``` 然后,在组件的`mounted`钩子设置Intersection Observer来监听占位符元素与滚动容器的交叉情况: ```javascript <script> export default { data() { return { items: [], // 加载数据列表 placeholderVisible: false, // 占位符元素的可见性 }; }, mounted() { const options = { root: this.$refs.scrollContainer, rootMargin: '0px', threshold: 1.0, }; this.observer = new IntersectionObserver(this.handleObserver, options); this.observer.observe(this.$refs.placeholder); }, methods: { handleObserver(entries) { entries.forEach((entry) => { if (entry.isIntersecting) { // 当占位符元素进入视口时加载数据 this.loadMoreData(); } }); }, loadMoreData() { // 模拟异步加载数据 setTimeout(() => { const newData = /* 从服务器获取新数据 */; this.items = [...this.items, ...newData]; this.placeholderVisible = false; }, 1000); }, handleScroll() { // 显示占位符元素 this.placeholderVisible = true; }, }, }; </script> ``` 以上代码,Intersection Observer API被用来监听占位符元素与滚动容器的交叉情况。当占位符元素进入视口时,即滚动到页面底部时,会触发`loadMoreData`方法来加载更多数据。在`loadMoreData`方法,你可以通过异步请求从服务器获取新数据,并将其添加到已有的数据列表。 请注意,以上代码只是一个简单的示例,你可以根据自己的需求进行进一步的定制和优化。同时,为了提高用户体验,你可能还需要添加一些加载状态的提示,如加载的loading动画等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值