When it comes to websites performance is king. How long it takes for a page to load can mean the difference of millions of dollars for large ecommerce sites. In this lesson we'll use the IntersectionObserver to check when an image is in the viewport to defer loading the image.

  document.addEventListener('DOMContentLoaded', () => {
const lazyImages = Array.from(document.querySelectorAll('img.lazy')); if ('IntersectionObserver' in window && 'IntersectionObserverEntry' in window && 'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
// Define the observer
let lazyImageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
// logic for handling interstion
if (entry.isIntersecting) {
let lazyImage = entry.target
lazyImage.src = lazyImage.dataset.src
lazyImage.srcset = lazyImage.dataset.srcset
lazyImage.classList.remove('lazy')
lazyImageObserver.unobserve(lazyImage)
}
})
}) // What to observe
lazyImages.forEach(lazyImage => {
lazyImageObserver.observe(lazyImage)
})
} else { }
})

https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API

https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API/Timing_element_visibility

We can add some Margin to preload image even before we scroll to the image:

          let lazyImageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
// logic for handling interstion
if (entry.isIntersecting) {
let lazyImage = entry.target
lazyImage.src = lazyImage.dataset.src
lazyImage.srcset = lazyImage.dataset.srcset
lazyImage.classList.remove('lazy')
lazyImageObserver.unobserve(lazyImage)
}
})
}, {
rootMargin: '50px'
})

rootMargin  Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). The values can be percentages. This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. Defaults to all zeros.

[Javascript] IntersectionObserver -- Lazy Load Images on a Website的更多相关文章

  1. Lazy Load, 延迟加载图片的 jQuery 插件.

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  2. jQuery延迟加载插件(Lazy Load)详解

    最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...

  3. 延迟加载图片的 jQuery 插件:Lazy Load

    网站的速度非常重要,现在有很多网站优化的工具,如 Google 的Page Speed,Yahoo 的 YSlow,对于网页图片,Yahoo 还提供 Smush.it这个工具对图片进行批量压缩,但是对 ...

  4. Lazy Load 图片延迟加载(转)

    jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...

  5. jQuery Lazy Load 图片延迟加载

    基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...

  6. Lazy Load, 延迟加载图片的 jQuery 插件 - NeoEase

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  7. Ionic 3 延迟加载(Lazy Load)实战(一)

    本文分享并演示了在 Ionic 3 框架中如何进行模块的延迟加载(Lazy Load)开发. 在我的实战课程「快速上手Ionic3 多平台开发企业级问答社区」中,因为开发的仿知乎 App 模块间的加载 ...

  8. about hibernate lazy load and solution

    about hibernate lazy load is that used when loaded again.it can increase efficienty and sava memory. ...

  9. [Vuex] Lazy Load a Vuex Module at Runtime using TypeScript

    Sometimes we need to create modules at runtime, for example depending on a condition. We could even ...

随机推荐

  1. 75.培训管理-培训信息发布 Extjs 页面

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...

  2. python 46 边界圆角 、a_img_list标签 、伪类选择器

    一:边界圆角 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  3. C#之考勤系统

    闲来无聊,搞搞C#,下面就是我写的一个Demo 员工类 using System; using System.Collections.Generic; using System.Linq; using ...

  4. C# 如何实现WinForm程序自重启(重新启动自己)

    重启的时间间隔方法 private void Restart() { Thread thtmp = new Thread(new ParameterizedThreadStart(run)); obj ...

  5. Java中last_insert_id的使用

    last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...

  6. Excel常用的小技巧

    1.Excel如何实现单元格内轻松换行:按住ALT+enter就可以了. 2.Excel固定表头:在“视图”>冻结窗口>冻结首行. 3.防止电脑突然断电,导致正在编辑的Excel数据丢失, ...

  7. 超简单:纯CSS实现的进度条

    ——————纯CSS实现的进度条—————— HTML: <div class="wrapper"> <div class="bar"> ...

  8. SLAM: 图像角点检测的Fast算法(时间阈值实验)

    作为角点检测的一种快速方法,FastCornerDetect算法比Harris方法.SIft方法都要快一些,应用于实时性要求较高的场合,可以直接应用于SLAM的随机匹配过程.算法来源于2006年的Ed ...

  9. php知识点(基本上文档都有,只为方便记忆)

    类型转换 (unset)转换为NULL (binary) 转换和 b 前缀转换支持为 PHP 5.2.1 新增   转换二进制 隐藏php后缀名 AddType application/x-httpd ...

  10. (转)Bootstrap 之 Metronic 模板的学习之路 - (1)总览

    https://segmentfault.com/a/1190000006673582#articleHeader0 写在前面 bootstrap 的模板非常多,Envato 上有着各种各样的免费及付 ...