首页 > 其他 > 详细

vue——预先指定高度,进行懒加载

时间:2020-01-13 18:38:14      阅读:86      评论:0      收藏:0      [点我收藏+]

参考:https://www.jianshu.com/p/48ecd9ba8d74

 

页面:

<div @scroll="checkLazyLoad" ref="home" v-for="item in list">
  <div class="gimg"> <img v-if="item.isLoadStart" :src="item.imgpath" > <img v-if="!item.isLoadStart" src="占位图片路径" >
  </div> </div>

 

script:

getList(){
       let _this = this;
     _this.axios.get(‘***‘).then(function(res) {
          if (res.status == 200 && res.data.result == 0) {
            var _data = res.data.message;
            for (let i in _data.list) {
              _this.list.push(_data.list[i]);
            }
            _this.setClass();
            _this.$nextTick(() => {
              _this.checkLazyLoad();
            })
          } else {
            console.log(‘fail:‘ + res.data.error);
          }
        }).catch(function(err) {
          console.log(‘error:‘ + err);
         }) 
} ,

setClass(){
    let _this = this;
    for (var i in _this.list) {
         _this.$set(_this.list[i], ‘isLoadStart‘, false); //初始化
    }
} ,

checkLazyLoad() { //预先一个可视区域进行懒加载
        let _this = this,
          classArr = document.getElementsByClassName(‘gimg‘),
          home = _this.$refs.home,
          clientHeight = home.clientHeight, //可视区域的高度
          scrollTop = home.scrollTop, //滚动条与页面内容顶部的距离
          count = 0;
        for (var i = 0; i < _this.list.length; i++) {
            let obj = _this.list[i],
              toTop = classArr[i].offsetTop; //图片距离页面全内容顶部的距离
            if (toTop - scrollTop - clientHeight <= clientHeight) {  //<=clientHeight,即预先一个可视区域
              obj.isLoadStart = true;
            }
        }
},

vue——预先指定高度,进行懒加载

原文:https://www.cnblogs.com/linjiangxian/p/12188499.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!