监听滚动事件,实现列表分页

监听滚动事件,实现列表分页

element+Vue3表格中没有滚动到底部,别表刷新的的api,这时候我们需要手动的去监听滚动事件,当滚动到底部时,触发请求事件代码如下:

onMounted(()=>{
nextTick(()=>{
const scroll = document.querySelector(‘.el-scrollbar__wrap’) /给元素添加滚动监听事件/
scroll.addEventListener(‘scroll’,handleScroll);
})
})
onBeforeUnmount(() => { /
* 解绑滚动监听 */
const scroll = document.querySelector(‘.el-scrollbar__wrap’)
scroll.removeEventListener(‘scroll’,handleScroll);
});

const handleScroll = ()=>{ /** 滚动到底部刷新数据 */
const scroll = document.querySelector(‘.el-scrollbar__wrap’)
const { scrollTop, scrollHeight, clientHeight } = scroll
if (scrollTop + clientHeight >= scrollHeight) {
if (state.queryForm.pageNo * state.queryForm.pageSize < state.queryForm.total) {
state.queryForm.pageNo++
setAgent()
}
}
}

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现滑动分页列表,可以使用Vue.js配合一些插件来完成,以下是一些步骤: 1. 安装Vue.js并创建Vue实例; 2. 引入Vue插件vue-infinite-loading,它提供了无限滚动加载数据的功能; 3. 创建一个包含列表数据的组件,使用v-for指令来渲染列表; 4. 在组件中使用vue-infinite-loading的指令,在滚动到底部时自动加载下一页数据; 5. 监听滚动事件,当滚动到底部时触发加载数据; 6. 在组件中使用CSS样式来实现滑动效果。 以下是一个简单的示例代码: ```html <template> <div class="list-container" ref="listContainer"> <div v-for="(item, index) in listData" :key="index" class="list-item">{{ item }}</div> <div v-infinite-loading="loadMore" :infinite-disabled="isLoading" infinite-distance="10%"> <div v-if="isLoading"><i class="fa fa-spinner fa-spin"></i> 加载中...</div> <div v-else><i class="fa fa-arrow-down"></i> 下拉加载更多</div> </div> </div> </template> <script> import InfiniteLoading from 'vue-infinite-loading'; export default { components: { InfiniteLoading, }, data() { return { listData: [], currentPage: 1, isLoading: false, }; }, mounted() { this.loadData(); window.addEventListener('scroll', this.handleScroll); }, methods: { loadData() { // 模拟异步加载数据 this.isLoading = true; setTimeout(() => { for (let i = 0; i < 10; i++) { this.listData.push(`Item ${this.listData.length + 1}`); } this.currentPage++; this.isLoading = false; }, 1000); }, loadMore() { this.loadData(); }, handleScroll() { const container = this.$refs.listContainer; const scrollTop = container.scrollTop; const scrollHeight = container.scrollHeight; const clientHeight = container.clientHeight; if (scrollTop + clientHeight >= scrollHeight) { this.loadData(); } }, }, }; </script> <style> .list-container { height: 300px; overflow-y: scroll; } .list-item { height: 50px; line-height: 50px; border-bottom: 1px solid #eee; } </style> ``` 在上面的示例中,我们使用了vue-infinite-loading插件来实现滑动分页列表的功能。同时,我们还监听滚动事件,在滚动到底部时触发自动加载下一页数据。通过CSS样式设置了列表的高度和滑动效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值