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>
    

      

  • 相关阅读:
    三比三:无线监控优点与传输技术剖解
    手把手教你学习FPGA系列视频教程_救护车鸣笛声
    世纪大争论:Linux还是GNU/Linux?
    [分享]我的LABVIEW快速开发串口测试软件实例
    高能效系统的功耗优化技术
    python学习小脚本多用户登录
    python学习三级菜单省市选择
    python学习购物车小程序
    python学习语句总结
    python学习猜年龄小游戏
  • 原文地址:https://www.cnblogs.com/caoke/p/9263060.html
Copyright © 2011-2022 走看看