Image delay loading plug-in based on jQuery or Zepto

Li Yang's Blog 2022-10-08 4.6 K Reading 6 Comments

Tips: This article has exceeded five hundred and fifty-nine No update in days, please note whether relevant content is still available!

When our website has too many page images, the loading speed will be slow. In particular, accessing pages with 2G/3G mobile phones is not only slow, but also uses a lot of traffic. Our themes have used the lazy loading form of images before, but none of them is perfect. Some themes also use placeholder images to control lazy loading. Today, we use the lazy loading method of js images. Its principle is to replace all images that need to be loaded in the page with a default image (generally small in size). Only images in the visible area can be loaded, In this way, we should be able to meet our daily needs. Well, let's not talk nonsense first course Try the operation. Later themes will be updated one by one. Don't be impatient!!!

 The first image delay loading plug-in based on jQuery or Zepto

Implementation principle

<img class="lazy" src="loading.png" data-src="img/example.jpg">

When the page is opened, the image in src will be loaded first, that is, a very small loading image; By listening to the scroll event, when the image is in the visual area, replace the src with data src to load the real image.

use

The existing plug-in unit , such as lazyload plug-in.

Lazyload plug-ins can be found on the Internet. The common ones are:
1. Jquery. lazyload. js: Depends on jQuery

 /*!  * Lazy Load - jQuery plugin for lazy loading images *  * Copyright (c) 2007-2015 Mika Tuupola  * * Licensed under the MIT license: *    http://www.opensource.org/licenses/mit-license.php  *  * Project home:  *    http://www.appelsiini.net/projects/lazyload  *  * Version:  1.9.7  *  */

http://www.appelsiini.net/projects/lazyload

2. Lazyload. js: Depends on jQuery or Zepto

 /*!  * An jQuery | zepto plugin for lazy loading images.  * author -> jieyou * see  https://github.com/jieyou/lazyload  * use some tuupola's code  https://github.com/tuupola/jquery_lazyload  (BSD)  * use component's throttle  https://github.com/component/throttle  (MIT)  */

Download: https://github.com/52fhy/lazyload

The usage of the above two is the same. The second one is based on the first modification to support zepto. Now take the second example:

lazyload.js

1. Introduction

 <script src="jQuery.js"></script><script src="lazyload.min.js"></script>

2. Using

 <!-- Lazyload.js default lazy loading original image attribute is data original --><img class="lazy" src="loading. png" data original="img/example. jpg"> $(".lazy").lazyload();

3. Configuration

Default:

 defaultOptions = { Threshold: 0,//How many images are loaded in advance, in px     failure_limit               : 0, Event: 'scroll',//Trigger event Effect: 'show',//effect Effect_params: null,//the parameter array of effect Container: w,//Use the container. The default is window Data_attribute: 'original',//can be changed to src, which corresponds to the data src attribute     data_srcset_attribute       : 'original-srcset',     skip_invisible              : true,     appear                      : emptyFn,     load                        : emptyFn, Vertical_only: false,//used in vertical scrolling pages     check_appear_throttle_time  : 300,     url_rewriter_fn             : emptyFn,     no_fake_img_loader          : false,     placeholder_data_img        : 'data:image/png; base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC',    // for IE6\7 that does not support data image     placeholder_real_img        : ' http://ditu.baidu.cn/yyfm/lazyload/0.0.1/img/placeholder.png ' //Todo: use global to configure some attributes, instead of configuring} YAML copy full screen in $(selector). lazyload ({}) every time }

Example:

 $(function() {   $(".lazy").lazyload({          Effect: "fadeIn",//effect Data_attribute: 'src',//can be changed to src, which corresponds to the data src attribute Event: 'scroll',//default value Container: $(". content"),//It is generally unnecessary to specify, that is, window. Must be specified in the sui frame  }); });

The code recording is completed, and the results will be seen after a few days of testing. If there is no problem, it will be updated one by one. If there is a problem, it depends on the situation. Well, there must be a lot of work after the vacation. Hurry up to work, and leave a message if there is a problem!

Disclaimer: This article is from Flying Goose Shadow , does not represent Lao Li's Notes If there is infringement, please contact this platform for handling.

Comment

Quick reply: expression:
 Addoil Applause Badlaugh Bomb Coffee Fabulous Facepalm Feces Frown Heyha Insidious KeepFighting NoProb PigHead Shocked Sinistersmile Slap Social Sweat Tolaugh Watermelon Witty Wow Yeah Yellowdog
Comment List (Yes six Comments, four thousand five hundred and ninety-six People around)
 Netizen nickname: Wuseyu Blog
Colorful Fish Blog V Iron powder  Google Chrome 86.0.4240.198  Windows 7 x64 Bench
2022-10-23 From Hebei reply
Blog lies in toss and ruin
 Netizen nickname: Li Yang Blog
Li Yang's Blog V Blogger  Sogou Explorer  Windows 10 x64
2022-10-24 From Liaoning reply
@Colorful Fish Blog Blog lies in toss and ruin in ease.
 Netizen nickname: A~administrator
A~dministrator V Iron powder  Google Chrome 86.0.4240.198  Windows 10 x64 chair
2022-10-10 From Hunan reply
In the vast network, choosing the theme of Li Yang is a very normal choice!
 Netizen nickname: Li Yang Blog
Li Yang's Blog V Blogger  Sogou Explorer  Windows 10 x64
2022-10-11 From Liaoning reply
@A~dministrator Thank you for your support!
 Netizen nickname: Yanzhi Online
Yanzhi Online V Iron powder  Google Chrome 86.0.4240.198  Windows 10 x64 sofa
2022-10-09 From Guangdong reply
Good way, support
 Netizen nickname: Li Yang Blog
Li Yang's Blog V Blogger  Sogou Explorer  Windows 10 x64
2022-10-09 From Liaoning reply
@Yanzhi Online thank you!
 cancel
 WeChat QR code
 WeChat QR code
 Alipay QR code