[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'
})
rootMargin
Margin around the root. Can have values similar to the CSSmargin
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的更多相关文章
- 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 ...
随机推荐
- CSS--浏览器CSS Hack 收集
所谓的Hack就是只有特定浏览器才能识别这段hack代码.Hack 不是什么好东西,除非没有办法,我们尽量还是不要用着玩意. 下面是各个浏览器的CSS Hack 列表. Firefox 浏览器 @-m ...
- 跟渣渣辉玩ffms
[SQL] /* Navicat MySQL Data Transfer Source Server : root Source Server Version : 50717 Source Host ...
- php write excel
/** * 写excel方法 */ function writeExcel($tabArr, $dataArr,$path) { require_once CODE_BASE2 . '/util/ph ...
- HTTP权威协议笔记-10.HTTP-NG
1.HTTP发展中存在的问题 复杂性 其连接.报文.及功能逻辑之间的混合使用相当复杂,使用容易出错 可扩展性 传统流行下来的http应用很难实现扩展性,且无法兼容 性能 高延时.低吞吐 ...
- JavaScript表格搜索高亮功能模拟
在网页表格中模拟excle的搜索高亮显示功能.当在搜索框中输入需要的姓名时,若表格中存在对应的数据,则该表格背景色变为黄色. 下面为表的HTML源码: <!doctype html> &l ...
- php 图片生成器
一.需求 最近公司由于有大量的海报要做,而且海报的布局规模都是一样的,只是内容不同,所以老板想我开发一个图片的生成器.可以根据你输入的内容生成海报图片. 具体有需求有以下的需求 1.可以根据将每条数据 ...
- POJ 2976 裸的01分数规划
题意:给你n个数对(认为是a数组和b数组吧),从中取n-m个数对,如果选第i个数对,定义x[i]=1,求R=∑(a[i]*x[i])/∑(b[i]*x[i])取得最大值时R的值.输出R*100(保留到 ...
- B - Is your horseshoe on the other hoof?
Problem description Valera the Horse is going to the party with friends. He has been following the f ...
- G - Wrong Subtraction
Problem description Little girl Tanya is learning how to decrease a number by one, but she does it w ...
- Selenium获取input值的两种方法:WebElement.getAttribute("value")和WebElement.getText()
在页面元素的定位中,有时候需要获取到元素的页面显示值,用来作为断言.例如,我需要获取email的值"amy1111@xxx.com". <input class=" ...