Vue中实现特效下拉加载更多数据

1.功能需求

      其实在很多的页面开发过程中,有些页面尤其是评论页面,在第一次加载的时候并不会加载很多的相关数据,而是加载一部分,当用户下拉旁边的滚动条时,尤其是滚动条移动到底部的时候,就会出现行的相关内容,这是怎么实现的呢?其实和分页大致一个原理,就相当于目前展示的是第一页的相关内容,当我们滑动到底部的时候就默认去获取第二页的相关内容。其实elementUi中也有这种的组件,但在运用的时候其实会发现并不是很好用那么该怎么实现这一需求场景呢?

2.页面展示

      粗糙的页面展示如下,由于没有发送请求,就采取的for循环往数组里面添加相关的内容,如下图一样,且在页面中通常在不只是有这一个页面这里就采取了一个蓝色的盒子来进行相关的站位,来模拟头部的相关内容。大致具体实现就如下面的效果图一样,就滑动到底部是会请求新的相关数据。那么具体实现的代码如下

 

 3.实现代码

     其实其中的核心代码如下,大致实现的具体原理如下,我们首先需要获取窗口的大小以及用来装载数据的父盒子的高度,因为根据这两个其实我们就可以获取到浏览器窗口实际能滚动的距离,

但是往往在页面开发过程中,一个页面中不只是有我这一个元素,通常这个功能用于网页底部,因此我们就需要获取这个父盒子距离页面顶部的距离,因此在滑动的过程中这段距离是不算在我们的scrollY中的,因此我们需要减去,最终我们将误差设置在10 (这个误差可以根据自己的喜好进行相关的设定,但不建议太大 ) 内,这样就可以实现不断获取刷新的功能了。且如果还有底部,则需要减去底部元素的宽度。但通常情况下这种都是下面没有元素。即若下面没有元素,去掉bottom.clientHeight 即可。

    getMainBox(){
      let Main = this.$refs.Main
      let bottom = this.$refs.bottom
     // console.log(Main.offsetTop);      //父盒子距离浏览器顶部的高度
     // console.log(window.innerHeight);  //浏览器的高度,即页面窗口的高度
     // console.log(Main.clientHeight);  //父盒子的高度(加上padding和margin)
     // console.log(window.scrollY);     //浏览器右边的滚动条滚动距离
      if(Math.abs(Main.clientHeight - window.innerHeight - (window.scrollY-Main.offsetTop-bottom.clientHeight)) <= 10){
        console.log('我滑动到底部了');
        alert('12323132')
        //这里在运用在获取新的相关数据即可
        for(let i = 0;i<10;i++){
          this.listData.push({infoData:'新加载的相关数据'})
        }
      }
    }

  4.全部代码

      代码采取的vue框架实现

<template>
  <div class="header"></div>
  <div class="Main" ref="Main">
    <div class="Item" v-for="(item,index) in listData" :key="index">{{ item.infoData }}</div>
  </div>
  <div class="bottom" ref="bottom"></div>
</template>

<script>
export default {
  name: 'scrollLoad',
  data() {
    return {
      listData: [
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        },
        {
          infoData: '123132'
        }
      ]
    }
  },
  mounted(){
    this.scrollWindow()
  },
  methods:{
    scrollWindow(){
      window.addEventListener('scroll',()=>{
        this.getMainBox()
      })
    },
    getMainBox(){
      let Main = this.$refs.Main
      let bottom = this.$refs.bottom    //底部的相关DOM
     // console.log(Main.offsetTop);      //父盒子距离浏览器顶部的高度
     // console.log(window.innerHeight);  //浏览器的高度,即页面窗口的高度
     // console.log(Main.clientHeight);  //父盒子的高度(加上padding和margin)
     // console.log(window.scrollY);     //浏览器右边的滚动条滚动距离
      if(Math.abs(Main.clientHeight - window.innerHeight - (window.scrollY-Main.offsetTop-bottom.clientHeight)) <= 10){
        console.log('我滑动到底部了');
        alert('12323132')
        //这里在运用在获取新的相关数据即可
        for(let i = 0;i<10;i++){
          this.listData.push({infoData:'新加载的相关数据'})
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header{
  background: blue;
  height: 200px;
  margin-bottom: 10px;
}
.Main {
  border: 1px solid #ccc;
  .Item {
    margin-top: 5px;
    height: 30px;
    line-height: 30px;
    background: red;

  }
}
.bottom{
  background: green;
  height: 200px;
  margin-bottom: 10px;
}
</style>

5.相关总结

     虽然这种加载方式可以给用户一种很舒服的体验,但是其实最难的是在页面刷新后的数据展示,是直接返回网站顶部这种用户重新下拉获取,还是保持到当前页中的数据呢?这背后其实也值得让我们进行相关的思考,欢迎大家一起讨论学习,相互进步

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue2 实现 `el-autocomplete` 下拉加载更多的方法可以通过监听 `input` 事件,在输入框输入字符时触发一个方法来获取更多数据。当滚动到下拉框底部时,再次触发这个方法获取更多数据。 下面是一个简单的示例代码: ```html <template> <el-autocomplete v-model="keyword" :fetch-suggestions="querySearch" @select="handleSelect"> <template slot-scope="{ item }"> <div class="autocomplete-item">{{ item }}</div> <div v-if="isLastItem(item)">已经到底了</div> </template> </el-autocomplete> </template> <script> export default { data() { return { keyword: '', items: [ 'Apple', 'Banana', 'Cherry', 'Durian', 'Elderberry', 'Fig', 'Grapefruit', 'Honeydew', 'Iced melon', 'Jackfruit' ], pageSize: 5, // 每页数量 page: 1 // 当前页数 } }, methods: { querySearch(queryString, cb) { const start = (this.page - 1) * this.pageSize const end = start + this.pageSize const results = this.items.slice(start, end) if (end >= this.items.length) { results.push('已经到底了') } cb(results) }, handleSelect(item) { if (this.isLastItem(item)) { // 已经到底了 } }, isLastItem(item) { return item === '已经到底了' } }, watch: { keyword(val) { if (val) { this.page = 1 this.querySearch(val, (results) => { this.$refs.autocomplete.suggestions = results }) } } }, mounted() { const input = this.$refs.autocomplete.$refs.input input.addEventListener('scroll', () => { if (input.scrollHeight - input.scrollTop === input.clientHeight) { // 到达底部 if (!this.isLastItem(this.$refs.autocomplete.suggestions[this.$refs.autocomplete.suggestions.length - 1])) { this.page++ this.querySearch(this.keyword, (results) => { this.$refs.autocomplete.suggestions = this.$refs.autocomplete.suggestions.concat(results) }) } } }) } } </script> ``` 在这个示例,我们在 `querySearch` 方法通过 `this.page` 和 `this.pageSize` 计算出当前需要展示的数据范围。如果已经到达数据末尾,我们返回一个特殊的字符串 `'已经到底了'` 作为提示。 在 `handleSelect` 方法,我们判断用户是否选择了最后一个提示项 `'已经到底了'`,如果是则停止加载更多数据。 在 `watch` ,我们监听输入框的值变化,并调用 `querySearch` 方法获取第一页数据。 在 `mounted` ,我们通过 `input.addEventListener('scroll')` 监听输入框的滚动事件,当滚动到底部时,判断当前的提示项是否为 `'已经到底了'`,如果不是则继续加载更多数据。注意要在 `this.$refs.autocomplete.suggestions` 使用 `concat` 方法将新的数据合并到已有的数据。 以上就是一个简单的实现 `el-autocomplete` 下拉加载更多的示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值