zoukankan      html  css  js  c++  java
  • js——图片懒加载

    <img class="js-lazy-image centered" src="./img/dog-running.svg" width="400" height="400" data-src="./img/dog-running.jpg">
    <img class="js-lazy-image centered" src="./img/dog-running.svg" width="400" height="400" data-src="./img/dog-running.jpg">
    <img class="js-lazy-image centered" src="./img/dog-running.svg" width="400" height="400" data-src="./img/dog-running.jpg">
        .fade-in {
          animation-name: fadeIn;
          animation-duration: 1.3s;
          animation-timing-function: cubic-bezier(0, 0, 0.4, 1);
          animation-fill-mode: forwards;
        }
    
        @keyframes fadeIn {
          from {
            opacity: 0;
          }
    
          to {
            opacity: 1;
          }
        }
    
    .centered {
       display:block;
       margin:0 auto;
    }
    'use strict';
    var images = document.querySelectorAll('.js-lazy-image'), config = {
        rootMargin: '50px 0px',
        threshold: 0.01
    }, imageCount = images.length, observer;
    if (!('IntersectionObserver'in window))
        loadImagesImmediately(images);
    else {
        observer = new IntersectionObserver(onIntersection,config);
        for (var image, i = 0; i < images.length; i++)
            (image = images[i],
            !image.classList.contains('js-lazy-image--handled')) && observer.observe(image)
    }
    function fetchImage(a) {
        return new Promise(function(b, c) {
            var d = new Image;
            d.src = a,
            d.onload = b,
            d.onerror = c
        }
        )
    }
    function preloadImage(a) {
        var b = a.dataset.src;
        return b ? fetchImage(b).then(function() {
            applyImage(a, b)
        }) : void 0
    }
    function loadImagesImmediately(a) {
        for (var d, b = Array.from(a), c = 0; c < a.length; c++)
            d = a[c],
            preloadImage(d)
    }
    function disconnect() {
        observer && observer.disconnect()
    }
    function onIntersection(a) {
        0 === imageCount && observer.disconnect();
        for (var c, b = 0; b < a.length; b++)
            c = a[b],
            0 < c.intersectionRatio && (imageCount--,
            observer.unobserve(c.target),
            preloadImage(c.target))
    }
    function applyImage(a, b) {
        a.classList.add('js-lazy-image--handled'),
        a.src = b,
        a.classList.add('fade-in')
    }
  • 相关阅读:
    sharepoint 2007 升级到 sharepoint 2013
    sharepoint 2010中启用RBS及所遇问题
    sharepoint 读取文件夹中所有的数据
    脚本设置ip&自动获取ip
    window 2008 r2 每隔一小时都要重启一次解决办法
    sharepoint 弹出框
    sharepoint powershell 根据报错的GUID查询错误
    java中switch选择结构
    mysql查看表字段相关信息
    mac系统 -postman发送http请求
  • 原文地址:https://www.cnblogs.com/huangmin1992/p/9436878.html
Copyright © 2011-2022 走看看