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>
    

      

  • 相关阅读:
    VC++6.0程序打开文件内存错误解决方法
    c++ vc6.0环境sp6补丁
    Net 应用程序如何在32位操作系统下申请超过2G的内存
    DataTable 排序
    VC UI 界面库
    让CSS兼容IE和Firefox的技巧集合
    两句CSS属性让点击图片链接时的虚线框消失
    一个常用的表单文本框input输入提示
    Css优先级分析
    清除浮动四种方法
  • 原文地址:https://www.cnblogs.com/caoke/p/9263060.html
Copyright © 2011-2022 走看看