Home > Web Front-end > JS Tutorial > Lazy loading of images and usage examples of jquery.lazyload.js

Lazy loading of images and usage examples of jquery.lazyload.js

零下一度
Release: 2017-06-26 10:35:28
Original
1238 people have browsed it

Sometimes lazy loading of images is used in projects, so what are the benefits of lazy loading?

I think it mainly includes two points. The first is that delaying the loading of images in long pages that contain many large images can speed up page loading; the second is to help reduce the load on the server.

The following introduces the commonly used lazy loading plug-in jquery.lazyload.js and how to implement a lazy loading plug-in.

1: jquery.lazyload.js plug-in

lazyload is a lazy loading plug-in written by jQuery. Images outside the visible area of ​​the browser will not be loaded until the user scrolls the page to Where they are. This is the exact opposite of how image preloading is handled.

Implementation Principle

Firstly, the selected img element is bound to an appear event (processing the img to display the real image address), so that the event can be triggered when the conditions are met in the future;

There is a container attribute configuration in the configuration object. The default is window. If the img element is in the container container viewport, the appear event is triggered;

In order to determine whether the img element is in the container container viewport range, there is The following four methods:

$.belowthefold = function(element, settings) {};    // 在视口下方$.rightoffold = function(element, settings) {};        // 在视口右方$.abovethetop = function(element, settings) {};        // 在视口上方$.leftofbegin = function(element, settings) {};        // 在视口左方
Copy after login

Specific use

1. Page introduction method

Since lazyload relies on jquery, all pages need to be introduced jquery, as follows:

<script src="jquery.js?1.1.11"></script><script src="jquery.lazyload.js?1.1.11"></script>
Copy after login

Basic writing method:

<img class="lazy" data-original="img/example.jpg" width="640" height="480">$(function() {
    $("img.lazy").lazyload();
});
Copy after login

The data-original attribute stores the real image url path.

Tips: You must set the width or height of the image in css, otherwise the plugin may not work properly.

Set Threshold

By default, the image will be loaded when it appears on the screen. If you want to load the image in advance, you can set the threshold option. Set the threshold to 200 so that the image is 200 from the screen. Pixels are loaded ahead of time.

$("img.lazy").lazyload({
    threshold : 200});
Copy after login

Set events to trigger loading

The event can be any jQuery event, such as: click and mouseover. You can also use custom events, such as: sporty and foobar . By default, it is in a waiting state until the user scrolls to the location of the image on the window. To prevent the image from loading until the gray placeholder image is clicked, you can do this:

$("img.lazy").lazyload({
    event : "click"});
Copy after login

Of course, You can also use the following method to implement lazy loading:

$(function() {
    $("img.lazy").lazyload({
        event : "sporty"});
});

$(window).bind("load", function() {var timeout = setTimeout(function() {
        $("img.lazy").trigger("sporty")
    }, 5000);
});
Copy after login

That is, 5 seconds after the page is loaded, perform lazy loading of the image.

Lazy loading effect

When the image is fully loaded, the plug-in uses the show() method by default to display the image. In fact, you can use any special effects you want to process. Below The code uses FadeIn Effect:

$("img.lazy").lazyload({
    effect : "fadeIn"});
Copy after login

How to use browsers that do not support JavaScript

JavaScript is activated in almost all browsers. However, you may still want to Able to display real images on clients that do not support JavaScript. For graceful degradation when the browser does not support JavaScript, you can write real image fragments in the

<img class="lazy" data-original="img/example.jpg"  width="640" heigh="480"><noscript><img src="img/example.jpg" width="640" heigh="480"></noscript>
Copy after login

You can hide the placeholder through CSS:

.lazy {
  display: none;
}
Copy after login

In browsers that support JavaScript, you must add the placeholder when the DOM is ready The bitmap is displayed, this can be done at the same time as the plugin is initialized.

$("img.lazy").show().lazyload();
Copy after login

Set up a lazy-loaded image container

You can use the plug-in on images in scrollable containers, such as DIV elements with scroll bars. What you have to do Just define the container as a jQuery object and pass it as a parameter to the initialization method:

#container {
    height: 600px;
    overflow: scroll;
}

$("img.lazy").lazyload({
    container: $("#container")
});
Copy after login

When the pictures are not arranged in order

When the page is scrolled, Lazy Load will loop as Loaded images. Check in the loop whether the image is within the visible area. By default the loop stops when it finds the first image that is not in the visible area. Images are considered to be distributed in a streaming manner, and the order of the images in the page and the HTML The order in the code is the same. But in some layouts, this assumption is not true. However, you can control the loading behavior through the failurelimit option.

$("img.lazy").lazyload({ 
    failure_limit : 10});
Copy after login

Set the failurelimit to 10 so that the plug-in will stop searching after finding 10 images that are not in the visible area. If you have a cumbersome layout, please set this parameter higher.

Set up to load hidden pictures

There may be many hidden pictures buried on your page. For example, if the plug-in is used to filter the list, you can continuously modify the display of each item in the list. Status. To improve performance, Lazy Load ignores hidden images by default. If you want to load hidden images, set skip_invisible to false.

$("img.lazy").lazyload({
    skip_invisible : true});
Copy after login

Source code

Official website address:

/*!
     * Lazy Load - jQuery plugin for lazy loading images
     *
     * Copyright (c) 2007-2015 Mika Tuupola
     *
     * Licensed under the MIT license:
     *   
     *
     * Project home:
     *   
     *
     * Version:  1.9.7
     *     */
    (function($, window, document, undefined) {var $window = $(window);
    
        $.fn.lazyload = function(options) {var elements = this;var $container;var settings = {
                threshold       : 0,
                failure_limit   : 0,
                event           : "scroll",
                effect          : "show",
                container       : window,
                data_attribute  : "original",
                skip_invisible  : false,
                appear          : null,
                load            : null,
                placeholder     : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC"};    function update() {var counter = 0;
    
                elements.each(function() {var $this = $(this);if (settings.skip_invisible && !$this.is(":visible")) {return;
                    }if ($.abovethetop(this, settings) ||$.leftofbegin(this, settings)) {/* Nothing. */} else if (!$.belowthefold(this, settings) &&
                        !$.rightoffold(this, settings)) {
                            $this.trigger("appear");/* if we found an image we'll load, reset the counter */counter = 0;
                    } else {if (++counter > settings.failure_limit) {return false;
                        }
                    }
                });
    
            }    if(options) {/* Maintain BC for a couple of versions. */if (undefined !== options.failurelimit) {
                    options.failure_limit = options.failurelimit;delete options.failurelimit;
                }if (undefined !== options.effectspeed) {
                    options.effect_speed = options.effectspeed;delete options.effectspeed;
                }
    
                $.extend(settings, options);
            }    /* Cache container as jQuery as object. */$container = (settings.container === undefined ||  settings.container === window) ? $window : $(settings.container);    /* Fire one scroll event per scroll. Not one scroll event per image. */if (0 === settings.event.indexOf("scroll")) {
                $container.bind(settings.event, function() {return update();
                });
            }    this.each(function() {var self = this;var $self = $(self);
    
                self.loaded = false;    /* If no src attribute given use data:uri. */if ($self.attr("src") === undefined || $self.attr("src") === false) {if ($self.is("img")) {
                        $self.attr("src", settings.placeholder);
                    }
                }    /* When appear is triggered load original image. */$self.one("appear", function() {if (!this.loaded) {if (settings.appear) {var elements_left = elements.length;
                            settings.appear.call(self, elements_left, settings);
                        }
                        $("<img />")
                            .bind("load", function() {    var original = $self.attr("data-" + settings.data_attribute);
                                $self.hide();if ($self.is("img")) {
                                    $self.attr("src", original);
                                } else {
                                    $self.css("background-image", "url('" + original + "')");
                                }
                                $self[settings.effect](settings.effect_speed);
    
                                self.loaded = true;    /* Remove image from array so it is not looped next time. */var temp = $.grep(elements, function(element) {return !element.loaded;
                                });
                                elements = $(temp);    if (settings.load) {var elements_left = elements.length;
                                    settings.load.call(self, elements_left, settings);
                                }
                            })
                            .attr("src", $self.attr("data-" + settings.data_attribute));
                    }
                });    /* When wanted event is triggered load original image *//* by triggering appear.                              */if (0 !== settings.event.indexOf("scroll")) {
                    $self.bind(settings.event, function() {if (!self.loaded) {
                            $self.trigger("appear");
                        }
                    });
                }
            });    /* Check if something appears when window is resized. */$window.bind("resize", function() {
                update();
            });    /* With IOS5 force loading images when navigating with back button. *//* Non optimal workaround. */if ((/(?:iphone|ipod|ipad).*os 5/gi).test(navigator.appVersion)) {
                $window.bind("pageshow", function(event) {if (event.originalEvent && event.originalEvent.persisted) {
                        elements.each(function() {
                            $(this).trigger("appear");
                        });
                    }
                });
            }    /* Force initial check if images should appear. */$(document).ready(function() {
                update();
            });    return this;
        };    /* Convenience methods in jQuery namespace.           *//* Use as  $.belowthefold(element, {threshold : 100, container : window}) */
        $.belowthefold = function(element, settings) {var fold;    if (settings.container === undefined || settings.container === window) {
                fold = (window.innerHeight ? window.innerHeight : $window.height()) + $window.scrollTop();
            } else {
                fold = $(settings.container).offset().top + $(settings.container).height();
            }    return fold <= $(element).offset().top - settings.threshold;
        };
    
        $.rightoffold = function(element, settings) {var fold;    if (settings.container === undefined || settings.container === window) {
                fold = $window.width() + $window.scrollLeft();
            } else {
                fold = $(settings.container).offset().left + $(settings.container).width();
            }    return fold <= $(element).offset().left - settings.threshold;
        };
    
        $.abovethetop = function(element, settings) {var fold;    if (settings.container === undefined || settings.container === window) {
                fold = $window.scrollTop();
            } else {
                fold = $(settings.container).offset().top;
            }    return fold >= $(element).offset().top + settings.threshold  + $(element).height();
        };
    
        $.leftofbegin = function(element, settings) {var fold;    if (settings.container === undefined || settings.container === window) {
                fold = $window.scrollLeft();
            } else {
                fold = $(settings.container).offset().left;
            }    return fold >= $(element).offset().left + settings.threshold + $(element).width();
        };
    
        $.inviewport = function(element, settings) {             return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) &&
                    !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
         };    /* Custom selectors for your convenience.   *//* Use as $("img:below-the-fold").something() or *//* $("img").filter(":below-the-fold").something() which is faster */
        $.extend($.expr[":"], {"below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); },"above-the-top"  : function(a) { return !$.belowthefold(a, {threshold : 0}); },"right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); },"left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); },"in-viewport"    : function(a) { return $.inviewport(a, {threshold : 0}); },/* Maintain BC for couple of versions. */"above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); },"right-of-fold"  : function(a) { return $.rightoffold(a, {threshold : 0}); },"left-of-fold"   : function(a) { return !$.rightoffold(a, {threshold : 0}); }
        });
    
    })(jQuery, window, document);
Copy after login

2: Handwrite a simple lazy loading plug-in

js code

window.smallDelay = (function(window, document, undefined) {'use strict';var store = [],poll;var settings = {
        offset:0, //离可视区域多少像素的图片可以被加载throttle: 250 //图片延时多少毫秒加载    }        var _inView = function(el) {var coords = el.getBoundingClientRect();return ((coords.top >= 0 && coords.left >= 0) && coords.top <= ((window.innerHeight || document.documentElement.clientHeight) + parseInt(settings.offset)));
    };var _pollImages = function() {for (var i = store.length; i--;) {var self = store[i];if (_inView(self)) {
                self.src = self.getAttribute(&#39;data-delay&#39;);
                store.splice(i, 1);
            }
        }
    };var _throttle = function() {
        clearTimeout(poll);
        poll = setTimeout(_pollImages, settings.throttle);
    };var init = function(obj) {var nodes = document.querySelectorAll(&#39;[data-delay]&#39;);var opts = obj || {};
        settings.offset = opts.offset || settings.offset;
        settings.throttle = opts.throttle || settings.throttle;for (var i = 0; i < nodes.length; i++) {
            store.push(nodes[i]);
        }

        _throttle();        //滚动监听执行图片懒加载if (document.addEventListener) {
            window.addEventListener(&#39;scroll&#39;, _throttle, false);
        } else {
            window.attachEvent(&#39;onscroll&#39;, _throttle);
        }        //返回该对象进行链式操作return this;
    };return {
        init: init,
        render: _throttle
    };

})(window, document);
Copy after login

Calling method:

smallDelay.init({
     offset: 0,//离可视区域多少像素的图片可以被加载   throttle: 0 //图片延时多少毫秒加载});
Copy after login

html code:

<img src="images/loading.gif" data-delay="images/avatar.png" />
Copy after login

三:根据lazyload插件实现一个不依赖jQuery的懒加载插件

实现内容

1、增加了图片预加载可选

2、修改了图片本身就在可视范围的时候直接显示而不需要滚动条触发

3、修改了Splice删除数组的时候,会跳过下一张图片BUG

4、浏览器窗口resize的时候图片出现也会加载

5、判断图片父层包裹顶部或者底部出现在可视范围内即可显示图片

实现源码

var Lazy = {
        $:function(arg,context){var tagAll,n,eles=[],i,sub = arg.substring(1);
            context = context|| document;if(typeof arg =='string'){switch(arg.charAt(0)){case '#':return document.getElementById(sub);break;case '.':if(context.getElementsByClassName) return context.getElementsByClassName(sub);
                        tagAll = Lazy.$('*');
                        n = tagAll.length;for(i = 0;i<n;i++){if(tagAll[i].className.indexOf(sub) > -1) eles.push(tagAll[i]);
                        }return eles;break;default:return context.getElementsByTagName(arg);break;
                }
            }
        },
        getPos:function (node) {var scrollx = document.documentElement.scrollLeft || document.body.scrollLeft,
                    scrollt = document.documentElement.scrollTop || document.body.scrollTop;var pos = node.getBoundingClientRect();return {top:pos.top + scrollt, right:pos.right + scrollx, bottom:pos.bottom + scrollt, left:pos.left + scrollx }
        },
        bind:function(node,type,handler){
            node.addEventListener?node.addEventListener(type, handler, false):node.attachEvent('on'+ type, handler);
        },
        unbind:function(node,type,handler){
            node.removeEventListener?node.removeEventListener(type, handler, false):node.detachEvent('on'+ type, handler);
        },
        toArray:function(eles){var arr = [];for(var i=0,n=eles.length;i<n;i++){
                arr.push(eles[i]);
            }return arr;
        }
    };function imgLazyLoad(){var timer,screenHeight = document.documentElement.clientHeight;// 选择所有图片var allimg = Lazy.$(&#39;img&#39;);// 筛选CLASS为lazyload的图片var elems = Lazy.$(&#39;.lazyload&#39;,allimg);// 转换为真正的数组elems = Lazy.toArray(elems);if(!elems.length) return;// 没有发生滚动事件时如果图片在可视范围之内,也显示for(var i = 0;i < elems.length;i++){// 获取图像的父元素即包裹图像的元素,判断图像是否在可视区域即直接判断父元素是否可视var parent = elems[i].parentNode;var pos = Lazy.getPos(parent);var posT = pos.top;var posB = pos.bottom;// 没有滚动条情况如果距离顶部的距离小于屏幕的高度则赋值SRCif(posT < screenHeight){
                elems[i].src = elems[i].getAttribute(&#39;data-img&#39;);// 移除后,数组的长度减一,下一个下标需减一elems.splice(i--,1);
            }
        }// 绑定scroll事件Lazy.bind(window,&#39;scroll&#39;,loading);
        Lazy.bind(window,&#39;resize&#39;,loading);function loading(){
            timer && clearTimeout(timer);
            timer = setTimeout(function(){var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                screenHeight = document.documentElement.clientHeight;for(var i = 0;i < elems.length;i++){var parent = elems[i].parentNode;var pos = Lazy.getPos(parent);var posT = pos.top;var posB = pos.bottom;var screenTop = screenHeight+scrollTop;// 元素顶部出现在可视区  或者  元素底部出现在可视区if((posT > scrollTop && posT <  screenTop) || (posB > scrollTop && posB < screenTop)){
                        elems[i].src = elems[i].getAttribute(&#39;data-img&#39;);
                        elems.splice(i--,1);
                    }else{// 去掉以下注释开启图片预加载// new Image().src = elems[i].getAttribute(&#39;data-img&#39;);                    }
                }if(!elems.length){
                    Lazy.unbind(window,&#39;scroll&#39;,loading);
                    Lazy.unbind(window,&#39;resize&#39;,loading);
                }
            },300);
        }
    }
    imgLazyLoad();
Copy after login

使用方法

1、在图片上增加lazyload的类(class='lazyload')

2、把真实的图片地址放入自定义属性data-img 中,把图片的SRC属性设置为一个一像素的透明图片,图片需要设置width,height属性,以免布局混乱

如下:

<img data-img="a.jpg" src="loading.gif" width="640" height="480"   class=&#39;lazyload&#39;>
Copy after login

3、在需要延迟加载的页面调用imgLazyLoad()函数;

该原生js实现的懒加载转载地址:

The above is the detailed content of Lazy loading of images and usage examples of jquery.lazyload.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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