Home > Web Front-end > JS Tutorial > body text

How to implement lazy loading of images in js

醉折花枝作酒筹
Release: 2021-04-20 09:28:04
forward
2556 people have browsed it

This article will give you a detailed introduction to the js method of lazy loading of images. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to implement lazy loading of images in js

Lazy loading of images is something that front-end optimization must master. Lazy loading of images can save bandwidth and reduce the load on our web pages. Next, I will record the method I have mastered for lazy loading of images. First of all, one way it is implemented is that the src of our picture within the view window is loaded first, but the src that is not within the view window will not be loaded as our scroll bar slides down.
How to implement lazy loading of images in js
Come down and load
How to implement lazy loading of images in js

The way I implement it is to use weapi, which is relatively simple. InstersectionObserver (official website InstersectionObserver).
Let’s go directly to the code

<div>
			<img  data-src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3225163326,3627210682&fm=26&gp=0.jpg" alt="How to implement lazy loading of images in js" >
			<img  data-src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg" alt="How to implement lazy loading of images in js" >
			<img  data-src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1246451335,909670857&fm=26&gp=0.jpg" alt="How to implement lazy loading of images in js" >
			<img  data-src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3609981743,3469269943&fm=26&gp=0.jpg" alt="How to implement lazy loading of images in js" >
			<img  data-src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3225163326,3627210682&fm=26&gp=0.jpg" alt="How to implement lazy loading of images in js" >
			<img  data-src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg" alt="How to implement lazy loading of images in js" >
			<img  data-src="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1246451335,909670857&fm=26&gp=0.jpg" alt="How to implement lazy loading of images in js" >
			<img  data-src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3609981743,3469269943&fm=26&gp=0.jpg" alt="How to implement lazy loading of images in js" >
		</div>
Copy after login

js part

//获取全部图片的数组
const imgs = [...document.getElementsByTagName(&#39;img&#39;)]
		// IntersectionObserver
		if(IntersectionObserver){
			let lazyloadObser = new IntersectionObserver((entries,observer)=>{
				entries.forEach((entry,index) =>{
					let lazyImage = entry.target
					if(entry.intersectionRatio>0){
						lazyImage.src = lazyImage.getAttribute(&#39;data-src&#39;);
						lazyloadObser.unobserve(lazyImage)
					}
				})
			})	
		  for(let i = 0; i < imgs.length;i++){
			  lazyloadObser.observe(imgs[i])
		  }	
		}
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to implement lazy loading of images in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template