Image lazy loading imgLazyLoading.js
This article mainly introduces in detail the use of imgLazyLoading.js for lazy loading of images. It has certain reference value. Interested friends can refer to it.
This article mainly introduces the use of images on the web front-end. Lazy loading imgLazyLoading, for your reference, the specific content is as follows
1, html code
//懒加载对象目标代码 <img originalSrc="__PUBLIC__/images/home/icon_pingtuan.png"> //引用本地js <script src="__PUBLIC__/js/imgLazyLoading.min.js"></script> <script src="__PUBLIC__/js/imgLazyLoading.js"></script>
2, js code
imgLazyLoading.min.js
jQuery.fn.extend({ delayLoading: function (a) { function g(d) { var b, c; if (a.container === undefined || a.container === window) { b = $(window).scrollTop(); c = $(window).height() + $(window).scrollTop() } else { b = $(a.container).offset().top; c = $(a.container).offset().top + $(a.container).height() } return d.offset().top + d.height() + a.beforehand >= b && c >= d.offset().top - a.beforehand } function h(d) { var b, c; if (a.container === undefined || a.container === window) { b = $(window).scrollLeft(); c = $(window).width() + $(window).scrollLeft() } else { b = $(a.container).offset().left; c = $(a.container).offset().left + $(a.container).width() } return d.offset().left + d.width() + a.beforehand >= b && c >= d.offset().left - a.beforehand } function f() { e.filter("img[" + a.imgSrcAttr + "]").each(function (d, b) { if ($(b).attr(a.imgSrcAttr) !== undefined && $(b).attr(a.imgSrcAttr) !== null && $(b).attr(a.imgSrcAttr) !== "" && g($(b)) && h($(b))) { var c = new Image; c.onload = function () { $(b).attr("src", c.src); a.duration !== 0 && $(b).hide().fadeIn(a.duration); $(b).removeAttr(a.imgSrcAttr); a.success($(b)) }; c.onerror = function () { $(b).attr("src", a.errorImg); $(b).removeAttr(a.imgSrcAttr); a.error($(b)) }; c.src = $(b).attr(a.imgSrcAttr) } }) } a = jQuery.extend({ defaultImg: "", errorImg: "", imgSrcAttr: "originalSrc", beforehand: 0, event: "scroll", duration: "normal", container: window, success: function () { }, error: function () { } }, a || {}); if (a.errorImg === undefined || a.errorImg === null || a.errorImg === "")a.errorImg = a.defaultImg; var e = $(this); if (e.attr("src") === undefined || e.attr("src") === null || e.attr("src") === "")e.attr("src", a.defaultImg); f(); $(a.container).bind(a.event, function () { f() }) } });
imgLazyLoading.js
$(function () { $("img").delayLoading({ //defaultImg: "__PUBLIC__/images/img/loading.gif", // 预加载前显示的图片 errorImg: "", // 读取图片错误时替换图片(默认:与defaultImg一样) imgSrcAttr: "originalSrc", // 记录图片路径的属性(默认:originalSrc,页面img的src属性也要替换为originalSrc) beforehand: 0, // 预先提前多少像素加载图片(默认:0) event: "scroll", // 触发加载图片事件(默认:scroll) duration: "normal", // 三种预定淡出(入)速度之一的字符串("slow", "normal", or "fast")或表示动画时长的毫秒数值(如:1000),默认:"normal" container: window, // 对象加载的位置容器(默认:window) success: function (imgObj) { }, // 加载图片成功后的回调函数(默认:不执行任何操作) error: function (imgObj) { } // 加载图片失败后的回调函数(默认:不执行任何操作) }); });
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
vue-cli configuration file (detailed tutorial)
Use jQuery to encapsulate animate.css (detailed tutorial)
How to reset the idle state in vuex
The above is the detailed content of Image lazy loading imgLazyLoading.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use Vue and Element-UI to implement lazy loading of images Lazy loading (Lazyloading) is a technology that delays loading of images, which can effectively increase page loading speed, save bandwidth and improve user experience. In the Vue project, we can use Element-UI and some plug-ins to implement the image lazy loading function. This article will introduce how to use Vue and Element-UI to implement lazy loading of images, and attach corresponding code examples. 1. Install the necessary dependencies before starting

How to optimize the lazy loading effect of images through PHP functions? With the development of the Internet, the number of images in web pages is increasing, which puts pressure on page loading speed. In order to improve user experience and reduce loading time, we can use image lazy loading technology. Lazy loading of images can delay the loading of images. Images are only loaded when the user scrolls to the visible area, which can reduce the loading time of the page and improve the user experience. When writing PHP web pages, we can optimize the lazy loading effect of images by writing some functions. Details below

How to use Vue for lazy loading and optimization of images Lazy loading is a technology for optimizing website performance, which is especially important in websites that handle a large number of images. Vue provides a simple method to implement lazy loading of images. This article will introduce how to use Vue for lazy loading and optimization of images. Introducing the vue-lazyload plug-in First, we need to introduce the vue-lazyload plug-in. This plug-in is a lightweight lazy loading plug-in for Vue that can help us implement lazy loading of images. You can install it through npm

Vue is a popular JavaScript framework that helps us build interactive web applications. During the development process, we often encounter situations where we need to load a large number of images, which often results in slower page loading and affects the user experience. This article will introduce how to use Vue’s keep-alive component to optimize the image loading experience. Why do you need to optimize the image loading experience? Images play a very important role in web pages, which can increase the attractiveness and readability of web pages and improve user experience. Ran

How to use image lazy loading technology to improve page loading speed in uniapp Overview: With the rapid development of mobile Internet, users have higher and higher requirements for the loading speed of web pages. As an indispensable element in web pages, pictures are often one of the main reasons for slow page loading. In order to improve the page loading speed, we can use image lazy loading technology to request loading when images need to be loaded, thereby reducing the initial loading time of the page. This article will introduce how to use image lazy loading technology in uniapp, and

How to use HTML, CSS and jQuery to implement advanced techniques for lazy loading of images. Lazy loading technology (LazyLoading) is a technical means to improve the performance of web pages, especially suitable for web pages that contain a large number of images. By using HTML, CSS and jQuery, we can easily implement lazy loading of images to speed up web page loading and improve user experience. This article will introduce how to use these three technologies to implement advanced techniques for lazy loading of images, and give specific code examples. 1. HTML preparation work in

To achieve the lazy loading effect of images in WeChat mini programs, specific code examples are required. With the rapid development of the mobile Internet, WeChat mini programs have become an indispensable part of people's lives. When developing WeChat mini programs, lazy loading of images is a common requirement, which can effectively improve the loading speed and user experience of the mini program. This article will introduce how to implement lazy loading of images in WeChat mini programs and give specific code examples. What is lazy loading of images? Lazy loading of images refers to delaying the loading of images on the page. Only when the image enters the user

How to optimize the access speed of PHP website through lazy loading of images? With the development of mobile Internet, more and more users use mobile devices to access websites. However, due to the relatively slow network speeds of mobile devices, loading speed becomes even more important. Among them, the loading speed of images has a great impact on website performance. In order to improve the access speed of PHP website, you can optimize it by lazy loading of images. Lazy loading of images means that when a web page is loaded, only images in the visible area are loaded instead of loading all images at once. In this way, first
