元素在可视区请求加载某些数据

文章介绍了如何使用IntersectionObserverAPI监测网页元素进入视口时,动态加载相关数据,避免一次性请求过多数据,提高性能。方法涉及数据标记、分块请求和Vue组件中的生命周期管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
    <div class="intersection">
        <ul>
            <li v-for="(item, index) in goodsData" class="intersection-item" :data-key="item.offerId"></li>
        </ul>
    </div>
</template>


//可以放在mounted内,或者页面请求数据渲染完的操作内根据实际放调用位置
this.intersectionFn()

data() {
    return {
        observer:null
    }
},
methods:{
intersectionFn() {
            // 创建IntersectionObserver实例
            const intersection = document.querySelector('.intersection')
            this.observer = new IntersectionObserver(
                entries => {
                    let newdata = []
                    entries.forEach(entry => {
                        if (entry.isIntersecting) {
                            // 元素进入可视区域,执行相应逻辑
                            const item = this.goodsData.find(item => item.offerId === entry.target.dataset.key)
                            if (item && !item.ciphertext) {
                                // 标记该元素已经请求过
                                this.$set(item, 'ciphertext', true)
                                newdata.push(item)
                                this.observer.unobserve(entry.target)
                            }
                        }
                    })
                    //当滚动太快会造成一个接口请求的数据太多,需要拆分开请求
                    if (newdata.length) {
                        let productIdsChunk = []
                        let productIdsArray = newdata.map(v => {
                            return v.offerId
                        })
                        productIdsChunk = this.chunkArray(productIdsArray, 6)
                        for (const item of productIdsChunk) {
                            this.getEncryptOrderSupportChannelFn(item)
                        }
                    }
                },
                {
                    root: intersection, // 使用viewport作为根
                    rootMargin: '60px', // 在viewport之外60px时触发回调
                    threshold: 0.1 // 交叉比例达到10%时触发回调
                }
            )
            // 监听所有带有.intersection-item类的元素
            const elements = document.querySelectorAll('.intersection-item')
            elements.forEach(element => this.observer.observe(element))
        },
        async getEncryptOrderSupportChannelFn(productIds) {
            let params = { productIds }
            let data = await xxxxxxxxxxxx(params)
            this.goodsData.forEach(val => {
                data.forEach(item => {
                    if (val.offerId == item.productId) {
                        val.data= item.data
                    }
                })
            })
            this.$forceUpdate()
        },
        chunkArray(array, size) {
            const result = []
            while (array.length > 0) {
                result.push(array.splice(0, size))
            }
            return result
        },
},
beforeDestroy() {
        if (this.observer) {
            this.observer.disconnect()
            this.observer = null
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值