【vue】实现滚动条滚动到底部时发送请求获取数据

34 篇文章 0 订阅
15 篇文章 2 订阅

当后端给我们一个分页获取数据的接口时,我们前端可以通过设置分页按钮来获取到所有的数据,也可以通过判断滚动条位置,当滚动条滚动到底部时就发送请求获取下一页的数据,这样我们就可以不设置分页按钮也能获取到所有的数据。

要实现这个功能最主要的就是判断滚动条的位置:

如下图所示:

clientHeight表示可视区域的高;

scrollTop表示滚动条距离可视区域顶部的距离;

scrollHeight表示滚动内容的高度;

由上图我们可以得出滚动条到达底部的条件:

scrollHeight = scrollTop + clientHeight

代码实现:

<template>
  <div>
    <div ref="scrollBox" class="box" @scroll="toLoadMore">
      <div v-for="item in boxData" :key="item.id">{{item.name}}</div>
      <div v-loading='loading'></div>
    </div>
  </div>
</template>

<script>
import { getEnumValuePage } from "@/api/index"
export default {
  data(){
    return {
      boxData: [],
      // 当前页码
      currentPage: 1,
      // 添加loading动画 
      loading: true,
      // 是否最后一页,如果最后一页了就不再发送请求了
      lastPage: false
    }
  },
  methods:{
    getBoxData(){
      this.loading = true
      //传给后端的参数
      let params = {
          currentPage: this.currentPage
        }
        //发送请求
        if(!this.lastPage){
          getEnumValuePage(params).then(res=>{
            if(res.status === 0){
              this.boxData.push(...(res.data.list))
              this.loading = false
              if(res.data.list.length < 36){ //一页总共36条数据,小于36条时,表示是最后一页了
                this.lastPage = true
              }
            }
          })
        }
    },
    toLoadMore(){
      let scrollDom = this.$refs.scrollBox
      // console.log(scrollDom.clientHeight,'clientHeight'); //scrollDom 可视区域的高 290px
      // console.log(scrollDom.scrollHeight,'scrollHeight'); //scrollDom 里面的内容的高
      // console.log(scrollDom.scrollTop,'scrollTop'); //滚动条距离scrollDom顶部的距离
      // 由于scrollTop不太准确,所以这里加了个1
      if(scrollDom.clientHeight + scrollDom.scrollTop + 1 >= scrollDom.scrollHeight){
        this.currentPage ++ //页数加一
        this.getBoxData() //发送请求获取数据
      }
    },
  },
  mounted(){
    this.getBoxData()
  }
}
</script>

<style scoped lang='scss'>
.box{
  width: 200px;
  height: 300px;
  border: 5px solid black;
  overflow-y: auto;
  &>div{
    padding-left: 15px;
    line-height: 30px;
    background: rgba(255, 192, 203, 0.7);
  }
}
</style>

效果展示:

  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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动画等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值