vue上拉加载新数据每次加载10条

<template>
  <div>
      <div class="banner">
          <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
            <van-swipe-item><img src="/static/img/banner_01.jpg" alt=""></van-swipe-item>
            <van-swipe-item><img src="/static/img/banner_01.jpg" alt=""></van-swipe-item>
            <van-swipe-item><img src="/static/img/banner_01.jpg" alt=""></van-swipe-item>
            <van-swipe-item><img src="/static/img/banner_01.jpg" alt=""></van-swipe-item>
          </van-swipe>
      </div>
      <div class="tp">
          <img src="/static/img/banner_02.jpg" alt="">
      </div>
      <div class="list">
          <ul>
              <li v-for="(e,i) in list" :key="i">
                  <img v-lazy="e.img"/>
                  <!-- 图片懒加载 -->
                  <div>
                      <p class="p1">{{e.t1}}</p>
                  <p class="p2">
                      <span>{{e.t2}}</span>
                      <s>{{e.t3}}</s>
                  </p>
                  <p class="p3">
                      <span>{{e.t4}}</span>
                  </p>
                  </div>
              </li>
          </ul>
          <div v-show="loading"><van-loading type="spinner" /></div>
          <span>{{sj}}</span>
      </div>
  </div>
</template>

<script>
export default {
    data()
    {
        return{
            list:[],
            page:0,
            number:10,
            loading:true,
            sj:"",
            all:""
        }
    },
    methods:{
        	lazyLoading () { // 滚动到底部,再加载的处理事件
            let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            let clientHeight = document.documentElement.clientHeight;
            let scrollHeight = document.documentElement.scrollHeight;
            if (scrollTop + clientHeight >= scrollHeight) { // 滚动到底部,逻辑代码
                //事件处理
                console.log("我已经滚动到底部了触发这个事件了")//此处可以添加数据请求

                setTimeout(()=>{

                          this.page=this.page+10
                this.number=this.number+10
                this.$axios.get("/static/data.json").then((res)=>{
           var data=res.data.list.slice(this.page,this.number);
           data.forEach((e)=>{
               this.list.push(e)
           })
        })

                if(this.list.length>=this.all)
                {
                    this.loading=false
                    this.sj="数据加载完毕"
                }
                },1500)


                }
        }
    },
    mounted()
    {
     
        window.addEventListener('scroll', this.lazyLoading); // 滚动到底部,再加载的处理事件
        this.$axios.get("/static/data.json").then((res)=>{
            this.all=res.data.list.length
            this.list=res.data.list.slice(this.page,this.number);
        })
    }
}
</script>

<style lang="scss" scoped>
.banner{
    width: 100%;
    img{
        width: 100%;
    }
}
.list{
    width: 100%;
    ul{
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        li{
            width: 48%;
            img{
                width: 100%;
                display: block;
            }
            div{
                padding: 10px;
                .p1{
                    line-height: 40px;
                    border-bottom: 1px solid #eee;
                }
                .p2{
                    line-height: 40px;
                    span{
                        font-size: 0.24rem;
                        font-weight: 700;
                        color: red;
                    }
                }
                .p3{
                    width: 100%;
                    span{
                        display: inline-block;
                        padding: 4px;
                        border: 1px solid orangered;
                        color: orangered;
                        border-radius: 4px;
                    }
                }
            }
        }
    }
}
</style>

默认显示几条和每次加载几条page 和 number自定义

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值