zoukankan      html  css  js  c++  java
  • 图片延迟加载组件--减少首屏加载数据

    图片延迟加载组件

    <template>
      <img v-if="src" :src="lazysrc"/>
      <div v-else v-html="lazyhtml"></div>
    </template>
    <script>
    
      import nonepng from './assets/img/none.png';
      var list = []
      var running = false;
      var lazyFuc = function (e) {
    
        if (running || list.length == 0) {
          return;
        }
        running = true;
        var windowHeight = document.documentElement.clientHeight || window.innerHeight;
        var windowWidth = document.documentElement.clientWidth || window.innerWidth;
        var temp = []
        for (var i = 0; i < list.length; i++) {
          var obj = list[i].$el.getBoundingClientRect();
          if ((obj.top < windowHeight + 320) && (obj.left < windowWidth)) {
            list[i].isShow = true
          }
          if (!list[i].isShow) {
            temp.push(list[i])
          }
        }
        list = temp;
        running = false;
      }
    
      window.addEventListener('scroll', lazyFuc);
      window.addEventListener('touchstart', lazyFuc);
      window.addEventListener('touchmove', lazyFuc);
      window.addEventListener('touchend', lazyFuc);
    
    
      var cache = [];
      export default {
        name: 'mvImg',
        data: function () {
          return {
            isShow: false
          }
        },
        mounted: function () {
          if (this.isShow) {
            return;
          }
          list.push(this);
          var windowHeight = document.documentElement.clientHeight || window.innerHeight;
          var windowWidth = document.documentElement.clientWidth || window.innerWidth;
          var obj = this.$el.getBoundingClientRect();
          if ((obj.top < windowHeight) && (obj.left < windowWidth + 100)) {
            this.isShow = true;
          }
        },
        computed: {
          lazyhtml() {
    
            //img标签要把src取出来
    
            if(!this.isShow) {
              return this.html.replace(/src *= *(["'])([^"]*)1/g,'src=$1'+nonepng+'$1')
            }
            else {
              return this.html;
            }
    
          },
          lazysrc() {
            if (cache.indexOf(this.src) > -1) {
              return this.src;
            }
            if (this.isShow) {
              cache.push(this.src);
              return this.src;
            } else {
              return nonepng;
            }
          }
        },
    
        props: {
          html:{
            type: String,
            default: ''
          },
          src: {
            type: String,
            default: ''
          }
        }
      };
    </script>
    
    <style>
    
    </style>
    

      

  • 相关阅读:
    重拾安卓_01_安卓开发环境搭建(eclipse)
    重拾安卓_01_安卓开发环境搭建(android studio)
    【BZOJ】1038: [ZJOI2008]瞭望塔
    【BZOJ】2178: 圆的面积并
    【UR #4】元旦三侠的游戏(博弈论+记忆化)
    【BZOJ】1027: [JSOI2007]合金(凸包+floyd)
    【POJ】1151 Atlantis(线段树)
    【POJ】1228 Grandpa's Estate(凸包)
    【POJ】1556 The Doors(计算几何基础+spfa)
    【POJ】1113 Wall(凸包)
  • 原文地址:https://www.cnblogs.com/caoke/p/9263060.html
Copyright © 2011-2022 走看看