You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/*! * Lazy Load - JavaScript plugin for lazy loading images * * Copyright (c) 2007-2019 Mika Tuupola * * Licensed under the MIT license: * http://www.opensource.org/licenses/mit-license.php * * Project home: * https://appelsiini.net/projects/lazyload * * Version: 2.0.0-rc.2 * */(function(root,factory){// umd exportif(typeofexports==="object"){module.exports=factory(root);}elseif(typeofdefine==="function"&&define.amd){define([],factory);}else{root.LazyLoad=factory(root);}})(typeofglobal!=="undefined" ? global : this.window||this.global,function(root){"use strict";if(typeofdefine==="function"&&define.amd){root=window;}constdefaults={src: "data-src",srcset: "data-srcset",selector: ".lazyload",root: null,rootMargin: "0px",threshold: 0,};/** * Merge two or more objects. Returns a new object. * @private * @param {Boolean} deep If true, do a deep (or recursive) merge [optional] * @param {Object} objects The objects to merge together * @returns {Object} Merged values of defaults and options */constextend=function(){letextended={};letdeep=false;leti=0;letlength=arguments.length;/* Check if a deep merge */if(Object.prototype.toString.call(arguments[0])==="[object Boolean]"){deep=arguments[0];i++;}/* Merge the object into the extended object */letmerge=function(obj){for(letpropinobj){if(Object.prototype.hasOwnProperty.call(obj,prop)){/* If deep merge and property is an object, merge properties */if(deep&&Object.prototype.toString.call(obj[prop])==="[object Object]"){extended[prop]=extend(true,extended[prop],obj[prop]);}else{extended[prop]=obj[prop];}}}};/* Loop through each object and conduct a merge */for(;i<length;i++){letobj=arguments[i];merge(obj);}returnextended;};functionLazyLoad(images,options){// merget default options and optionsthis.settings=extend(defaults,options||{});this.images=images||document.querySelectorAll(this.settings.selector);this.observer=null;this.init();}LazyLoad.prototype={init: function(){/* Without observers load everything and bail out early. */// 不支持IntersectionObserver API,直接加载图片if(!root.IntersectionObserver){this.loadImages();return;}letself=this;letobserverConfig={root: this.settings.root,// 祖先元素,null或未设置,默认使用顶级文档元素rootMargin: this.settings.rootMargin,// 计算交叉时添加到根(root)边界盒bounding box的矩形偏移量threshold: [this.settings.threshold],// 阀值(0-1),监听对象的交叉区域与边界区域的比率};// 使用IntersectionObserver API观察目标元素与root元素的交叉状态this.observer=newIntersectionObserver(function(entries){Array.prototype.forEach.call(entries,function(entry){// 目标元素与root元素交叉区域超过threshold阀值if(entry.isIntersecting){// 停止监听self.observer.unobserve(entry.target);// 目前元素 src等属性赋值letsrc=entry.target.getAttribute(self.settings.src);letsrcset=entry.target.getAttribute(self.settings.srcset);// img元素对src赋值,否则对backgroundImage赋值if("img"===entry.target.tagName.toLowerCase()){if(src){entry.target.src=src;}if(srcset){entry.target.srcset=srcset;}}else{entry.target.style.backgroundImage="url("+src+")";}}});},observerConfig);Array.prototype.forEach.call(this.images,function(image){// 对目标元素进行监听self.observer.observe(image);});},// 加载后销毁loadAndDestroy: function(){if(!this.settings){return;}this.loadImages();this.destroy();},// 加载图片loadImages: function(){if(!this.settings){return;}letself=this;Array.prototype.forEach.call(this.images,function(image){letsrc=image.getAttribute(self.settings.src);letsrcset=image.getAttribute(self.settings.srcset);if("img"===image.tagName.toLowerCase()){if(src){image.src=src;}if(srcset){image.srcset=srcset;}}else{image.style.backgroundImage="url('"+src+"')";}});},// 销毁destroy: function(){if(!this.settings){return;}// 解除监听this.observer.disconnect();this.settings=null;},};// window.lazyload registerroot.lazyload=function(images,options){returnnewLazyLoad(images,options);};// jquery plugin registerif(root.jQuery){const$=root.jQuery;$.fn.lazyload=function(options){options=options||{};options.attribute=options.attribute||"data-src";newLazyLoad($.makeArray(this),options);returnthis;};}returnLazyLoad;});
仓库:
lazyload-Vanilla JavaScript plugin for lazyloading images
源码实现:
收获:
lazyload2.x 版本的核心实现主要是使用了 IntersectionObserver API。
IntersectionObserver 接口提供了一种异步观察目标元素与其祖先元素或顶级文档视窗(viewport)交叉状态的方法。可以很简单优雅的判断目标元素是否出现在可视区域内,从而进行处理。
IntersectionObserver API 的兼容性在 IE 全军覆没,但是 w3c 实现了IntersectionObserver polyfill,使得 IE 可以兼容到 7+,真香!
深入了解了 layload 2.x 实现
对 IntersectionObserver API 使用有了更进一步的理解
The text was updated successfully, but these errors were encountered: