[Javascript] IntersectionObserver -- Lazy Load Images on a Website
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'
})
rootMarginMargin around the root. Can have values similar to the CSSmarginproperty, 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的更多相关文章
- Lazy Load, 延迟加载图片的 jQuery 插件.
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- jQuery延迟加载插件(Lazy Load)详解
最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...
- 延迟加载图片的 jQuery 插件:Lazy Load
网站的速度非常重要,现在有很多网站优化的工具,如 Google 的Page Speed,Yahoo 的 YSlow,对于网页图片,Yahoo 还提供 Smush.it这个工具对图片进行批量压缩,但是对 ...
- Lazy Load 图片延迟加载(转)
jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...
- jQuery Lazy Load 图片延迟加载
基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...
- Lazy Load, 延迟加载图片的 jQuery 插件 - NeoEase
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- Ionic 3 延迟加载(Lazy Load)实战(一)
本文分享并演示了在 Ionic 3 框架中如何进行模块的延迟加载(Lazy Load)开发. 在我的实战课程「快速上手Ionic3 多平台开发企业级问答社区」中,因为开发的仿知乎 App 模块间的加载 ...
- about hibernate lazy load and solution
about hibernate lazy load is that used when loaded again.it can increase efficienty and sava memory. ...
- [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 ...
随机推荐
- bzoj2115
线性基+dfs树 我们先搞出dfs树,其实最终路径就是最初的路径和一些环异或. 环最多只有m-n+1,因为一共有m条边,然后有n-1条边在dfs树上,所以还剩m-n+1条边,都可以构成环. 所以dfs ...
- Android第三方登陆之新浪微博Weibo篇(原生登陆授权)
前言 Android第三方登录可以说是非常的常见,今天主要先说一下新浪微博第三方登陆授权. SDK版本支持 SDK v3.0已经发布了支持iPhone和Android的版本. 须将你的应用的包名签名信 ...
- 【Arduino】LCD 1602 转接板 的默认接线
原来的1602屏需要7个IO口才能驱动起来LCD 1602转接板可以帮你省5个IO口. 在Arduino中,LCD 1602 转接板可以使用函数库LiquidCrystal_I2C1602: 该函数的 ...
- C# 彻底关闭程序,包括循环
System.Environment.Exit(System.Environment.ExitCode); this.Dispose(); this.Close();
- Deutsch lernen (12)
1. hinweisen - wies hin - hingewiesen 向...指出,指明 auf etw.(A) hinweisen Ich möchte (Sie) darauf hiweis ...
- SLAM: Inverse Depth Parametrization for Monocular SALM
首语: 此文实现客观的评测了使线性化的反转深度的效果.整篇只在表明反转可以线性化,解决距离增加带来的增长问题,有多少优势--%! 我的天呢!我竟然完整得翻译了一遍. 使用标记点地图构建SLAM的方法, ...
- 虚拟机+linux+大杂烩
出于项目需要,需要用到linux系统.这玩意儿平时很少用,要说体验度还是windows更人性化一些. 1.虚拟机的安装,这个没说的,百度VMware直接下最新版安装就好. 2.接着是linux系统的安 ...
- Javascript语法,变量类型,条件,循环语句,函数,面向对象
1.JavaScript代码革两种存在形式: <!-- 方式一 --> <script type='txt/javascript' src='/js/comment.js'>& ...
- .NET 解决方案 核心库整理
一系列令人敬畏的.NET核心库,工具,框架和软件: https://www.cnblogs.com/weifeng123/p/11039345.html 企业级解决方案收录: https://www ...
- vue scss 安装
1.开始在vue项目中使用sass,在命令行输入一下命令进行安装(使用git命令行要用shift+insert 进行粘贴否则粘贴不上) cnpm install node-sass --save-de ...