vue实现滚动下拉加载更多功能

<template>
  <div>
    <ul class="box">
      <li v-for="item in dataList">
        {{item}}
      </li>
    </ul>
    <!--<p style="height: 100px;" v-if="flag"> 加载中... </p>-->
  </div>

</template>

<script>
    import _ from 'lodash';
    export default {
        data () {
          return {
              dataList: [1,2,3,4,5,6,7,8,9,0],
          }
        },
        mounted () {
            const that = this;
            window.addEventListener('scroll',function(){
                // 滚动视口高度(也就是当前元素的真实高度)
                let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
                console.log(scrollHeight)
                // 可见区域高度
                let clientHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
                // 滚动条顶部到浏览器顶部高度
                let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
                if(clientHeight + scrollTop == scrollHeight){
                	// 触底执行更多操作(如:发起ajax请求)
                    // this.dataList = [...this.dataList, ...[1,2,3,4,5]]

                    // const data = [1,2,3,4];
                    // that.dataList = that.dataList.concat(data)
                    const data = []
                    for (let i=0;i<10;i++) {
                        data.push(_.random(0,1000))
                    }
                    that.dataList = that.dataList.concat(data)
                  // console.log(_.random(0,100))
                }
            })
        },

    }
</script>


<style>
  div{
    background: pink;
  }
  .box{
    /*height: 500px;*/
    /*min-height: 1000px;*/
    width: 500px;
    margin-top: 1000px;
    background: pink;
    overflow: auto;
  }
</style>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值