vue scroll滑动到底部自动加载

vue中常常会遇到滑动加载的情况,为了不让数据一次性请求到太多。

那么接下来就来实现一下这个功能吧

<template>
  <div id="app">
    <div class="showpanel">
      <div class="title">下拉自动加载</div>
      <div class="boxpanel">
        <div class="show-item" v-for="(index,item) in showdata" :key="index">{{index+1}}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showdata: 10
    }
  }
}
</script>
<style lang="less" scoped>
.showpanel {
  height:100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}
.title {
  padding: 20px;
  text-align: center;
}
.boxpanel {
  flex: auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 10%;
  background: blue;
  overflow-y: scroll;
}

.show-item {
  width:48%;
  background: brown;
  height:30%;
  margin-bottom: 20px;
  color: #fff;
  text-align: center;
  font-size: 40px;
}
</style>

 

1.首先创建10个div,在需要显示的内容区域内。为了区分,颜色有可能有点辣眼睛。

2.检测滑动条是否滑倒底部了

在滑动区域中加入@scoll 监听时间

当该区域的的正文区域的高度,减去可见区域的高度,小于设定的高度的时候,就认为这个滑块可以加载新的资源了。

如图在不停往下移动滑块的时候,就会不断增加滑块的个数

 

3.当请求接口的时候也是同理的。添加一个页数的参数,跟是否可以加载的判定。

<template>
  <div id="app">
    <div class="showpanel">
      <div class="title">下拉自动加载</div>
      <div class="boxpanel" @scroll="scrollBottom($event)">
        <div class="show-item" v-for="(index,item) in showdata" :key="index">{{index+1}}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showdata: [],
      page: 0, // 根据接口定义的初始值
      loadflag: false // 是否可以加载数据
    }
  },
  methods: {
    scrollBottom(e) {
      let self = this
      let Scroll = e.target
      // 网页可见区域高:document.body.clientHeight
      // 网页正文全文高:document.body.scrollHeight
      let scrollHeight = Scroll.scrollHeight - Scroll.clientHeight
      self.scrollTop = Scroll.scrollTop
      if (scrollHeight - Scroll.scrollTop < 100 && !self.loadflag) {
        console.log('到底部了')
        self.loadflag = true
        self.page++
        self.loadData()
      }
    },
    loadData() {
      let self = this
      self.$axios
        .post('接口', '参数')
        .then(response => {
          var data = response.data

          if (data.code === 1) {
            if (data.data.length > 0) {
              var list = self.showdata
              self.showdata = list.concat(data.data)
              self.loadflag = false
            } else {
              console.log('没有更多参数了')
            }
          } else {
            console.log(data.meg)
          }
        })
        .catch(function (error) {
          console.log(error)
        })
    },
    created () {
      let self = this
      self.loadData()
    }
  }

}
</script>
<style lang="less" scoped>
.showpanel {
  height:100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}
.title {
  padding: 20px;
  text-align: center;
}
.boxpanel {
  flex: auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 10%;
  background: blue;
  overflow-y: scroll;
}

.show-item {
  width:48%;
  background: brown;
  height:30%;
  margin-bottom: 20px;
  color: #fff;
  text-align: center;
  font-size: 40px;
}
</style>

 

python课程

https://marketing.csdn.net/poster/109?utm_source=NEWFXDT

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现滚动到底部加载更多,可以借助 Vue 自带的 `scroll` 事件和一些计算属性。具体实现步骤如下: 1. 在需要滚动的容器上监听 `scroll` 事件。 ```html <template> <div ref="container" @scroll="handleScroll"> <!-- 列表内容 --> </div> </template> ``` 2. 在 `handleScroll` 方法中,判断容器滚动到了底部,如果是则触发加载更多的方法。 ```js methods: { handleScroll() { const container = this.$refs.container // 容器滚动到底部 if (container.scrollHeight - container.scrollTop === container.clientHeight) { this.loadMore() } }, loadMore() { // 加载更多数据 } } ``` 3. 计算当前是否有更多数据需要加载,用于显示加载更多按钮或者提示信息。 ```js computed: { hasMore() { return this.list.length < this.total } } ``` 4. 在模板中根据 `hasMore` 的值来显示加载更多按钮或者提示信息。 ```html <template> <div ref="container" @scroll="handleScroll"> <!-- 列表内容 --> <div v-if="hasMore" class="load-more" @click="loadMore">加载更多</div> <div v-else class="no-more">没有更多数据了</div> </div> </template> ``` 完整代码示例: ```html <template> <div ref="container" @scroll="handleScroll"> <ul> <li v-for="(item, index) in list" :key="index">{{ item }}</li> </ul> <div v-if="hasMore" class="load-more" @click="loadMore">加载更多</div> <div v-else class="no-more">没有更多数据了</div> </div> </template> <script> export default { data() { return { list: [], // 列表数据 total: 0, // 总共的数据量 pageSize: 10, // 每页数据量 currentPage: 1 // 当前页数 } }, computed: { hasMore() { return this.list.length < this.total } }, methods: { handleScroll() { const container = this.$refs.container // 容器滚动到底部 if (container.scrollHeight - container.scrollTop === container.clientHeight) { this.loadMore() } }, loadMore() { // 模拟加载更多数据 setTimeout(() => { const start = this.pageSize * (this.currentPage - 1) const end = start + this.pageSize this.list = this.list.concat(Array.from({ length: this.pageSize }).map((_, i) => `Item ${start + i + 1}`)) this.currentPage++ }, 500) } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小猴子编程

请支持一下我的分享

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值