zoukankan      html  css  js  c++  java
  • vue——预先指定高度,进行懒加载

    参考: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" alt=""> <img v-if="!item.isLoadStart" src="占位图片路径" alt="">
      </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;
                }
            }
    },
  • 相关阅读:
    父div不会被子div撑高
    ie6兼容问题
    浏览器兼容性技巧
    css hack基本语法
    网站设置为灰色
    .net cookie跨域请求指定请求域名
    实体对象属性和值转为键值对Dictionary
    C#通过对象属性名修改值
    jQuery.noConflict()解决imgBox.js依赖jquery版本问题
    华为OJ之最长公共子序列
  • 原文地址:https://www.cnblogs.com/linjiangxian/p/12188499.html
Copyright © 2011-2022 走看看