vanilla-lazyload
Advanced tools
Comparing version 8.15.0 to 8.15.1
@@ -5,2 +5,11 @@ # CHANGELOG | ||
#### 10.16.1 | ||
**BUGFIX**: Autoplaying video not loaded correctly after entering the viewport (issue #240). Thanks to @maeligg. | ||
#### 10.16.0 | ||
Added new option `load_delay` to skip loading when fast scrolling occurs, as requested in issues #235 and #166. | ||
Pass in a number of milliseconds, and each image will be loaded after it stayed inside that viewport for that time. | ||
#### 10.15.0 | ||
@@ -149,2 +158,6 @@ | ||
#### 8.15.1 | ||
**BUGFIX**: Autoplaying video not loaded correctly after entering the viewport (issue #240). Thanks to @maeligg. | ||
#### 8.15.0 | ||
@@ -151,0 +164,0 @@ |
@@ -187,2 +187,3 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
setAttributeIfValue(element, "src", srcDataValue); | ||
element.load(); | ||
}; | ||
@@ -189,0 +190,0 @@ |
@@ -1,2 +0,2 @@ | ||
var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e};define(function(){"use strict";function e(e,t,n){return!(o(e,t,n)||l(e,t,n)||r(e,t,n)||a(e,t,n))}var t=function(){return{elements_selector:"img",container:window,threshold:300,throttle:150,data_src:"src",data_srcset:"srcset",data_sizes:"sizes",class_loading:"loading",class_loaded:"loaded",class_error:"error",class_initial:"initial",skip_invisible:!0,callback_load:null,callback_error:null,callback_set:null,callback_processed:null,callback_enter:null,to_webp:!1}},n=function(e,t){e&&e(t)},i=function(e){return e.getBoundingClientRect().top+window.pageYOffset-e.ownerDocument.documentElement.clientTop},o=function(e,t,n){return(t===window?window.innerHeight+window.pageYOffset:i(t)+t.offsetHeight)<=i(e)-n},s=function(e){return e.getBoundingClientRect().left+window.pageXOffset-e.ownerDocument.documentElement.clientLeft},r=function(e,t,n){var i=window.innerWidth;return(t===window?i+window.pageXOffset:s(t)+i)<=s(e)-n},l=function(e,t,n){return(t===window?window.pageYOffset:i(t))>=i(e)+n+e.offsetHeight},a=function(e,t,n){return(t===window?window.pageXOffset:s(t))>=s(e)+n+e.offsetWidth},c=function(e,t){var n,i=new e(t);try{n=new CustomEvent("LazyLoad::Initialized",{detail:{instance:i}})}catch(e){(n=document.createEvent("CustomEvent")).initCustomEvent("LazyLoad::Initialized",!1,!1,{instance:i})}window.dispatchEvent(n)},u=function(e,t){return e.getAttribute("data-"+t)},d=function(e,t,n){return e.setAttribute("data-"+t,n)},h=function(e){return d(e,"was-processed","true")},_=function(e){return"true"===u(e,"was-processed")},f=function(e,t){return t?e.replace(/\.(jpe?g|png)/gi,".webp"):e},p="undefined"!=typeof window,g=p&&!("onscroll"in window)||/(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent),m=p&&"classList"in document.createElement("p"),v=p&&function(){var e=document.createElement("canvas");return!(!e.getContext||!e.getContext("2d"))&&0===e.toDataURL("image/webp").indexOf("data:image/webp")}(),w=function(e,t,n,i){for(var o,s=0;o=e.children[s];s+=1)if("SOURCE"===o.tagName){var r=u(o,n);E(o,t,r,i)}},E=function(e,t,n,i){n&&e.setAttribute(t,f(n,i))},b=function(e,t){var n=v&&t.to_webp,i=u(e,t.data_src);if(i){var o=f(i,n);e.style.backgroundImage='url("'+o+'")'}},L={IMG:function(e,t){var n=v&&t.to_webp,i=t.data_srcset,o=e.parentNode;o&&"PICTURE"===o.tagName&&w(o,"srcset",i,n);var s=u(e,t.data_sizes);E(e,"sizes",s);var r=u(e,i);E(e,"srcset",r,n);var l=u(e,t.data_src);E(e,"src",l,n)},IFRAME:function(e,t){var n=u(e,t.data_src);E(e,"src",n)},VIDEO:function(e,t){var n=t.data_src,i=u(e,n);w(e,"src",n),E(e,"src",i)}},T=function(e,t){var n=e.tagName,i=L[n];i?i(e,t):b(e,t)},O=function(e,t){m?e.classList.add(t):e.className+=(e.className?" ":"")+t},S=function(e,t){m?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s+)"+t+"(\\s+|$)")," ").replace(/^\s+/,"").replace(/\s+$/,"")},H=function(e){this._settings=_extends({},t(),e),this._queryOriginNode=this._settings.container===window?document:this._settings.container,this._previousLoopTime=0,this._loopTimeout=null,this._boundHandleScroll=this.handleScroll.bind(this),this._isFirstLoop=!0,window.addEventListener("resize",this._boundHandleScroll),this.update()};return H.prototype={_reveal:function(e,t){if(t||!_(e)){var i=this._settings,o=function t(){i&&(e.removeEventListener("load",s),e.removeEventListener("error",t),S(e,i.class_loading),O(e,i.class_error),n(i.callback_error,e))},s=function t(){i&&(S(e,i.class_loading),O(e,i.class_loaded),e.removeEventListener("load",t),e.removeEventListener("error",o),n(i.callback_load,e))};n(i.callback_enter,e),["IMG","IFRAME","VIDEO"].indexOf(e.tagName)>-1&&(e.addEventListener("load",s),e.addEventListener("error",o),O(e,i.class_loading)),T(e,i),n(i.callback_set,e)}},_loopThroughElements:function(t){var i=this._settings,o=this._elements,s=o?o.length:0,r=void 0,l=[],a=this._isFirstLoop;for(r=0;r<s;r++){var c=o[r];i.skip_invisible&&null===c.offsetParent||(g||t||e(c,i.container,i.threshold))&&(a&&O(c,i.class_initial),this.load(c),l.push(r),h(c))}for(;l.length;)o.splice(l.pop(),1),n(i.callback_processed,o.length);0===s&&this._stopScrollHandler(),a&&(this._isFirstLoop=!1)},_purgeElements:function(){var e=this._elements,t=e.length,n=void 0,i=[];for(n=0;n<t;n++){var o=e[n];_(o)&&i.push(n)}for(;i.length>0;)e.splice(i.pop(),1)},_startScrollHandler:function(){this._isHandlingScroll||(this._isHandlingScroll=!0,this._settings.container.addEventListener("scroll",this._boundHandleScroll))},_stopScrollHandler:function(){this._isHandlingScroll&&(this._isHandlingScroll=!1,this._settings.container.removeEventListener("scroll",this._boundHandleScroll))},handleScroll:function(){var e=this._settings.throttle;if(0!==e){var t=Date.now(),n=e-(t-this._previousLoopTime);n<=0||n>e?(this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._previousLoopTime=t,this._loopThroughElements()):this._loopTimeout||(this._loopTimeout=setTimeout(function(){this._previousLoopTime=Date.now(),this._loopTimeout=null,this._loopThroughElements()}.bind(this),n))}else this._loopThroughElements()},loadAll:function(){this._loopThroughElements(!0)},update:function(){this._elements=Array.prototype.slice.call(this._queryOriginNode.querySelectorAll(this._settings.elements_selector)),this._purgeElements(),this._loopThroughElements(),this._startScrollHandler()},destroy:function(){window.removeEventListener("resize",this._boundHandleScroll),this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._stopScrollHandler(),this._elements=null,this._queryOriginNode=null,this._settings=null},load:function(e,t){this._reveal(e,t)}},p&&function(e,t){if(t)if(t.length)for(var n,i=0;n=t[i];i+=1)c(e,n);else c(e,t)}(H,window.lazyLoadOptions),H}); | ||
var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e};define(function(){"use strict";function e(e,t,n){return!(o(e,t,n)||l(e,t,n)||r(e,t,n)||a(e,t,n))}var t=function(){return{elements_selector:"img",container:window,threshold:300,throttle:150,data_src:"src",data_srcset:"srcset",data_sizes:"sizes",class_loading:"loading",class_loaded:"loaded",class_error:"error",class_initial:"initial",skip_invisible:!0,callback_load:null,callback_error:null,callback_set:null,callback_processed:null,callback_enter:null,to_webp:!1}},n=function(e,t){e&&e(t)},i=function(e){return e.getBoundingClientRect().top+window.pageYOffset-e.ownerDocument.documentElement.clientTop},o=function(e,t,n){return(t===window?window.innerHeight+window.pageYOffset:i(t)+t.offsetHeight)<=i(e)-n},s=function(e){return e.getBoundingClientRect().left+window.pageXOffset-e.ownerDocument.documentElement.clientLeft},r=function(e,t,n){var i=window.innerWidth;return(t===window?i+window.pageXOffset:s(t)+i)<=s(e)-n},l=function(e,t,n){return(t===window?window.pageYOffset:i(t))>=i(e)+n+e.offsetHeight},a=function(e,t,n){return(t===window?window.pageXOffset:s(t))>=s(e)+n+e.offsetWidth},c=function(e,t){var n,i=new e(t);try{n=new CustomEvent("LazyLoad::Initialized",{detail:{instance:i}})}catch(e){(n=document.createEvent("CustomEvent")).initCustomEvent("LazyLoad::Initialized",!1,!1,{instance:i})}window.dispatchEvent(n)},u=function(e,t){return e.getAttribute("data-"+t)},d=function(e,t,n){return e.setAttribute("data-"+t,n)},h=function(e){return d(e,"was-processed","true")},_=function(e){return"true"===u(e,"was-processed")},f=function(e,t){return t?e.replace(/\.(jpe?g|png)/gi,".webp"):e},p="undefined"!=typeof window,g=p&&!("onscroll"in window)||/(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent),m=p&&"classList"in document.createElement("p"),v=p&&function(){var e=document.createElement("canvas");return!(!e.getContext||!e.getContext("2d"))&&0===e.toDataURL("image/webp").indexOf("data:image/webp")}(),w=function(e,t,n,i){for(var o,s=0;o=e.children[s];s+=1)if("SOURCE"===o.tagName){var r=u(o,n);E(o,t,r,i)}},E=function(e,t,n,i){n&&e.setAttribute(t,f(n,i))},b=function(e,t){var n=v&&t.to_webp,i=u(e,t.data_src);if(i){var o=f(i,n);e.style.backgroundImage='url("'+o+'")'}},L={IMG:function(e,t){var n=v&&t.to_webp,i=t.data_srcset,o=e.parentNode;o&&"PICTURE"===o.tagName&&w(o,"srcset",i,n);var s=u(e,t.data_sizes);E(e,"sizes",s);var r=u(e,i);E(e,"srcset",r,n);var l=u(e,t.data_src);E(e,"src",l,n)},IFRAME:function(e,t){var n=u(e,t.data_src);E(e,"src",n)},VIDEO:function(e,t){var n=t.data_src,i=u(e,n);w(e,"src",n),E(e,"src",i),e.load()}},T=function(e,t){var n=e.tagName,i=L[n];i?i(e,t):b(e,t)},O=function(e,t){m?e.classList.add(t):e.className+=(e.className?" ":"")+t},S=function(e,t){m?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s+)"+t+"(\\s+|$)")," ").replace(/^\s+/,"").replace(/\s+$/,"")},H=function(e){this._settings=_extends({},t(),e),this._queryOriginNode=this._settings.container===window?document:this._settings.container,this._previousLoopTime=0,this._loopTimeout=null,this._boundHandleScroll=this.handleScroll.bind(this),this._isFirstLoop=!0,window.addEventListener("resize",this._boundHandleScroll),this.update()};return H.prototype={_reveal:function(e,t){if(t||!_(e)){var i=this._settings,o=function t(){i&&(e.removeEventListener("load",s),e.removeEventListener("error",t),S(e,i.class_loading),O(e,i.class_error),n(i.callback_error,e))},s=function t(){i&&(S(e,i.class_loading),O(e,i.class_loaded),e.removeEventListener("load",t),e.removeEventListener("error",o),n(i.callback_load,e))};n(i.callback_enter,e),["IMG","IFRAME","VIDEO"].indexOf(e.tagName)>-1&&(e.addEventListener("load",s),e.addEventListener("error",o),O(e,i.class_loading)),T(e,i),n(i.callback_set,e)}},_loopThroughElements:function(t){var i=this._settings,o=this._elements,s=o?o.length:0,r=void 0,l=[],a=this._isFirstLoop;for(r=0;r<s;r++){var c=o[r];i.skip_invisible&&null===c.offsetParent||(g||t||e(c,i.container,i.threshold))&&(a&&O(c,i.class_initial),this.load(c),l.push(r),h(c))}for(;l.length;)o.splice(l.pop(),1),n(i.callback_processed,o.length);0===s&&this._stopScrollHandler(),a&&(this._isFirstLoop=!1)},_purgeElements:function(){var e=this._elements,t=e.length,n=void 0,i=[];for(n=0;n<t;n++){var o=e[n];_(o)&&i.push(n)}for(;i.length>0;)e.splice(i.pop(),1)},_startScrollHandler:function(){this._isHandlingScroll||(this._isHandlingScroll=!0,this._settings.container.addEventListener("scroll",this._boundHandleScroll))},_stopScrollHandler:function(){this._isHandlingScroll&&(this._isHandlingScroll=!1,this._settings.container.removeEventListener("scroll",this._boundHandleScroll))},handleScroll:function(){var e=this._settings.throttle;if(0!==e){var t=Date.now(),n=e-(t-this._previousLoopTime);n<=0||n>e?(this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._previousLoopTime=t,this._loopThroughElements()):this._loopTimeout||(this._loopTimeout=setTimeout(function(){this._previousLoopTime=Date.now(),this._loopTimeout=null,this._loopThroughElements()}.bind(this),n))}else this._loopThroughElements()},loadAll:function(){this._loopThroughElements(!0)},update:function(){this._elements=Array.prototype.slice.call(this._queryOriginNode.querySelectorAll(this._settings.elements_selector)),this._purgeElements(),this._loopThroughElements(),this._startScrollHandler()},destroy:function(){window.removeEventListener("resize",this._boundHandleScroll),this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._stopScrollHandler(),this._elements=null,this._queryOriginNode=null,this._settings=null},load:function(e,t){this._reveal(e,t)}},p&&function(e,t){if(t)if(t.length)for(var n,i=0;n=t[i];i+=1)c(e,n);else c(e,t)}(H,window.lazyLoadOptions),H}); | ||
//# sourceMappingURL=lazyload.amd.min.js.map |
@@ -211,2 +211,3 @@ var getDefaultSettings = () => ({ | ||
setAttributeIfValue(element, "src", srcDataValue); | ||
element.load(); | ||
}; | ||
@@ -213,0 +214,0 @@ |
@@ -187,2 +187,3 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
setAttributeIfValue(element, "src", srcDataValue); | ||
element.load(); | ||
}; | ||
@@ -189,0 +190,0 @@ |
@@ -1,2 +0,2 @@ | ||
var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},LazyLoad=function(){"use strict";function e(e,t,n){return!(o(e,t,n)||l(e,t,n)||r(e,t,n)||a(e,t,n))}var t=function(){return{elements_selector:"img",container:window,threshold:300,throttle:150,data_src:"src",data_srcset:"srcset",data_sizes:"sizes",class_loading:"loading",class_loaded:"loaded",class_error:"error",class_initial:"initial",skip_invisible:!0,callback_load:null,callback_error:null,callback_set:null,callback_processed:null,callback_enter:null,to_webp:!1}},n=function(e,t){e&&e(t)},i=function(e){return e.getBoundingClientRect().top+window.pageYOffset-e.ownerDocument.documentElement.clientTop},o=function(e,t,n){return(t===window?window.innerHeight+window.pageYOffset:i(t)+t.offsetHeight)<=i(e)-n},s=function(e){return e.getBoundingClientRect().left+window.pageXOffset-e.ownerDocument.documentElement.clientLeft},r=function(e,t,n){var i=window.innerWidth;return(t===window?i+window.pageXOffset:s(t)+i)<=s(e)-n},l=function(e,t,n){return(t===window?window.pageYOffset:i(t))>=i(e)+n+e.offsetHeight},a=function(e,t,n){return(t===window?window.pageXOffset:s(t))>=s(e)+n+e.offsetWidth},c=function(e,t){var n,i=new e(t);try{n=new CustomEvent("LazyLoad::Initialized",{detail:{instance:i}})}catch(e){(n=document.createEvent("CustomEvent")).initCustomEvent("LazyLoad::Initialized",!1,!1,{instance:i})}window.dispatchEvent(n)},u=function(e,t){return e.getAttribute("data-"+t)},d=function(e,t,n){return e.setAttribute("data-"+t,n)},h=function(e){return d(e,"was-processed","true")},_=function(e){return"true"===u(e,"was-processed")},f=function(e,t){return t?e.replace(/\.(jpe?g|png)/gi,".webp"):e},p="undefined"!=typeof window,g=p&&!("onscroll"in window)||/(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent),m=p&&"classList"in document.createElement("p"),v=p&&function(){var e=document.createElement("canvas");return!(!e.getContext||!e.getContext("2d"))&&0===e.toDataURL("image/webp").indexOf("data:image/webp")}(),w=function(e,t,n,i){for(var o,s=0;o=e.children[s];s+=1)if("SOURCE"===o.tagName){var r=u(o,n);E(o,t,r,i)}},E=function(e,t,n,i){n&&e.setAttribute(t,f(n,i))},b=function(e,t){var n=v&&t.to_webp,i=u(e,t.data_src);if(i){var o=f(i,n);e.style.backgroundImage='url("'+o+'")'}},L={IMG:function(e,t){var n=v&&t.to_webp,i=t.data_srcset,o=e.parentNode;o&&"PICTURE"===o.tagName&&w(o,"srcset",i,n);var s=u(e,t.data_sizes);E(e,"sizes",s);var r=u(e,i);E(e,"srcset",r,n);var l=u(e,t.data_src);E(e,"src",l,n)},IFRAME:function(e,t){var n=u(e,t.data_src);E(e,"src",n)},VIDEO:function(e,t){var n=t.data_src,i=u(e,n);w(e,"src",n),E(e,"src",i)}},T=function(e,t){var n=e.tagName,i=L[n];i?i(e,t):b(e,t)},O=function(e,t){m?e.classList.add(t):e.className+=(e.className?" ":"")+t},S=function(e,t){m?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s+)"+t+"(\\s+|$)")," ").replace(/^\s+/,"").replace(/\s+$/,"")},y=function(e){this._settings=_extends({},t(),e),this._queryOriginNode=this._settings.container===window?document:this._settings.container,this._previousLoopTime=0,this._loopTimeout=null,this._boundHandleScroll=this.handleScroll.bind(this),this._isFirstLoop=!0,window.addEventListener("resize",this._boundHandleScroll),this.update()};return y.prototype={_reveal:function(e,t){if(t||!_(e)){var i=this._settings,o=function t(){i&&(e.removeEventListener("load",s),e.removeEventListener("error",t),S(e,i.class_loading),O(e,i.class_error),n(i.callback_error,e))},s=function t(){i&&(S(e,i.class_loading),O(e,i.class_loaded),e.removeEventListener("load",t),e.removeEventListener("error",o),n(i.callback_load,e))};n(i.callback_enter,e),["IMG","IFRAME","VIDEO"].indexOf(e.tagName)>-1&&(e.addEventListener("load",s),e.addEventListener("error",o),O(e,i.class_loading)),T(e,i),n(i.callback_set,e)}},_loopThroughElements:function(t){var i=this._settings,o=this._elements,s=o?o.length:0,r=void 0,l=[],a=this._isFirstLoop;for(r=0;r<s;r++){var c=o[r];i.skip_invisible&&null===c.offsetParent||(g||t||e(c,i.container,i.threshold))&&(a&&O(c,i.class_initial),this.load(c),l.push(r),h(c))}for(;l.length;)o.splice(l.pop(),1),n(i.callback_processed,o.length);0===s&&this._stopScrollHandler(),a&&(this._isFirstLoop=!1)},_purgeElements:function(){var e=this._elements,t=e.length,n=void 0,i=[];for(n=0;n<t;n++){var o=e[n];_(o)&&i.push(n)}for(;i.length>0;)e.splice(i.pop(),1)},_startScrollHandler:function(){this._isHandlingScroll||(this._isHandlingScroll=!0,this._settings.container.addEventListener("scroll",this._boundHandleScroll))},_stopScrollHandler:function(){this._isHandlingScroll&&(this._isHandlingScroll=!1,this._settings.container.removeEventListener("scroll",this._boundHandleScroll))},handleScroll:function(){var e=this._settings.throttle;if(0!==e){var t=Date.now(),n=e-(t-this._previousLoopTime);n<=0||n>e?(this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._previousLoopTime=t,this._loopThroughElements()):this._loopTimeout||(this._loopTimeout=setTimeout(function(){this._previousLoopTime=Date.now(),this._loopTimeout=null,this._loopThroughElements()}.bind(this),n))}else this._loopThroughElements()},loadAll:function(){this._loopThroughElements(!0)},update:function(){this._elements=Array.prototype.slice.call(this._queryOriginNode.querySelectorAll(this._settings.elements_selector)),this._purgeElements(),this._loopThroughElements(),this._startScrollHandler()},destroy:function(){window.removeEventListener("resize",this._boundHandleScroll),this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._stopScrollHandler(),this._elements=null,this._queryOriginNode=null,this._settings=null},load:function(e,t){this._reveal(e,t)}},p&&function(e,t){if(t)if(t.length)for(var n,i=0;n=t[i];i+=1)c(e,n);else c(e,t)}(y,window.lazyLoadOptions),y}(); | ||
var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},LazyLoad=function(){"use strict";function e(e,t,n){return!(o(e,t,n)||l(e,t,n)||r(e,t,n)||a(e,t,n))}var t=function(){return{elements_selector:"img",container:window,threshold:300,throttle:150,data_src:"src",data_srcset:"srcset",data_sizes:"sizes",class_loading:"loading",class_loaded:"loaded",class_error:"error",class_initial:"initial",skip_invisible:!0,callback_load:null,callback_error:null,callback_set:null,callback_processed:null,callback_enter:null,to_webp:!1}},n=function(e,t){e&&e(t)},i=function(e){return e.getBoundingClientRect().top+window.pageYOffset-e.ownerDocument.documentElement.clientTop},o=function(e,t,n){return(t===window?window.innerHeight+window.pageYOffset:i(t)+t.offsetHeight)<=i(e)-n},s=function(e){return e.getBoundingClientRect().left+window.pageXOffset-e.ownerDocument.documentElement.clientLeft},r=function(e,t,n){var i=window.innerWidth;return(t===window?i+window.pageXOffset:s(t)+i)<=s(e)-n},l=function(e,t,n){return(t===window?window.pageYOffset:i(t))>=i(e)+n+e.offsetHeight},a=function(e,t,n){return(t===window?window.pageXOffset:s(t))>=s(e)+n+e.offsetWidth},c=function(e,t){var n,i=new e(t);try{n=new CustomEvent("LazyLoad::Initialized",{detail:{instance:i}})}catch(e){(n=document.createEvent("CustomEvent")).initCustomEvent("LazyLoad::Initialized",!1,!1,{instance:i})}window.dispatchEvent(n)},u=function(e,t){return e.getAttribute("data-"+t)},d=function(e,t,n){return e.setAttribute("data-"+t,n)},h=function(e){return d(e,"was-processed","true")},_=function(e){return"true"===u(e,"was-processed")},f=function(e,t){return t?e.replace(/\.(jpe?g|png)/gi,".webp"):e},p="undefined"!=typeof window,g=p&&!("onscroll"in window)||/(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent),m=p&&"classList"in document.createElement("p"),v=p&&function(){var e=document.createElement("canvas");return!(!e.getContext||!e.getContext("2d"))&&0===e.toDataURL("image/webp").indexOf("data:image/webp")}(),w=function(e,t,n,i){for(var o,s=0;o=e.children[s];s+=1)if("SOURCE"===o.tagName){var r=u(o,n);E(o,t,r,i)}},E=function(e,t,n,i){n&&e.setAttribute(t,f(n,i))},b=function(e,t){var n=v&&t.to_webp,i=u(e,t.data_src);if(i){var o=f(i,n);e.style.backgroundImage='url("'+o+'")'}},L={IMG:function(e,t){var n=v&&t.to_webp,i=t.data_srcset,o=e.parentNode;o&&"PICTURE"===o.tagName&&w(o,"srcset",i,n);var s=u(e,t.data_sizes);E(e,"sizes",s);var r=u(e,i);E(e,"srcset",r,n);var l=u(e,t.data_src);E(e,"src",l,n)},IFRAME:function(e,t){var n=u(e,t.data_src);E(e,"src",n)},VIDEO:function(e,t){var n=t.data_src,i=u(e,n);w(e,"src",n),E(e,"src",i),e.load()}},T=function(e,t){var n=e.tagName,i=L[n];i?i(e,t):b(e,t)},O=function(e,t){m?e.classList.add(t):e.className+=(e.className?" ":"")+t},S=function(e,t){m?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s+)"+t+"(\\s+|$)")," ").replace(/^\s+/,"").replace(/\s+$/,"")},y=function(e){this._settings=_extends({},t(),e),this._queryOriginNode=this._settings.container===window?document:this._settings.container,this._previousLoopTime=0,this._loopTimeout=null,this._boundHandleScroll=this.handleScroll.bind(this),this._isFirstLoop=!0,window.addEventListener("resize",this._boundHandleScroll),this.update()};return y.prototype={_reveal:function(e,t){if(t||!_(e)){var i=this._settings,o=function t(){i&&(e.removeEventListener("load",s),e.removeEventListener("error",t),S(e,i.class_loading),O(e,i.class_error),n(i.callback_error,e))},s=function t(){i&&(S(e,i.class_loading),O(e,i.class_loaded),e.removeEventListener("load",t),e.removeEventListener("error",o),n(i.callback_load,e))};n(i.callback_enter,e),["IMG","IFRAME","VIDEO"].indexOf(e.tagName)>-1&&(e.addEventListener("load",s),e.addEventListener("error",o),O(e,i.class_loading)),T(e,i),n(i.callback_set,e)}},_loopThroughElements:function(t){var i=this._settings,o=this._elements,s=o?o.length:0,r=void 0,l=[],a=this._isFirstLoop;for(r=0;r<s;r++){var c=o[r];i.skip_invisible&&null===c.offsetParent||(g||t||e(c,i.container,i.threshold))&&(a&&O(c,i.class_initial),this.load(c),l.push(r),h(c))}for(;l.length;)o.splice(l.pop(),1),n(i.callback_processed,o.length);0===s&&this._stopScrollHandler(),a&&(this._isFirstLoop=!1)},_purgeElements:function(){var e=this._elements,t=e.length,n=void 0,i=[];for(n=0;n<t;n++){var o=e[n];_(o)&&i.push(n)}for(;i.length>0;)e.splice(i.pop(),1)},_startScrollHandler:function(){this._isHandlingScroll||(this._isHandlingScroll=!0,this._settings.container.addEventListener("scroll",this._boundHandleScroll))},_stopScrollHandler:function(){this._isHandlingScroll&&(this._isHandlingScroll=!1,this._settings.container.removeEventListener("scroll",this._boundHandleScroll))},handleScroll:function(){var e=this._settings.throttle;if(0!==e){var t=Date.now(),n=e-(t-this._previousLoopTime);n<=0||n>e?(this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._previousLoopTime=t,this._loopThroughElements()):this._loopTimeout||(this._loopTimeout=setTimeout(function(){this._previousLoopTime=Date.now(),this._loopTimeout=null,this._loopThroughElements()}.bind(this),n))}else this._loopThroughElements()},loadAll:function(){this._loopThroughElements(!0)},update:function(){this._elements=Array.prototype.slice.call(this._queryOriginNode.querySelectorAll(this._settings.elements_selector)),this._purgeElements(),this._loopThroughElements(),this._startScrollHandler()},destroy:function(){window.removeEventListener("resize",this._boundHandleScroll),this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._stopScrollHandler(),this._elements=null,this._queryOriginNode=null,this._settings=null},load:function(e,t){this._reveal(e,t)}},p&&function(e,t){if(t)if(t.length)for(var n,i=0;n=t[i];i+=1)c(e,n);else c(e,t)}(y,window.lazyLoadOptions),y}(); | ||
//# sourceMappingURL=lazyload.iife.min.js.map |
@@ -191,2 +191,3 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
setAttributeIfValue(element, "src", srcDataValue); | ||
element.load(); | ||
}; | ||
@@ -193,0 +194,0 @@ |
@@ -1,2 +0,2 @@ | ||
var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};!function(e,t){"object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.LazyLoad=t()}(this,function(){"use strict";function e(e,t,n){return!(i(e,t,n)||l(e,t,n)||r(e,t,n)||a(e,t,n))}var t=function(){return{elements_selector:"img",container:window,threshold:300,throttle:150,data_src:"src",data_srcset:"srcset",data_sizes:"sizes",class_loading:"loading",class_loaded:"loaded",class_error:"error",class_initial:"initial",skip_invisible:!0,callback_load:null,callback_error:null,callback_set:null,callback_processed:null,callback_enter:null,to_webp:!1}},n=function(e,t){e&&e(t)},o=function(e){return e.getBoundingClientRect().top+window.pageYOffset-e.ownerDocument.documentElement.clientTop},i=function(e,t,n){return(t===window?window.innerHeight+window.pageYOffset:o(t)+t.offsetHeight)<=o(e)-n},s=function(e){return e.getBoundingClientRect().left+window.pageXOffset-e.ownerDocument.documentElement.clientLeft},r=function(e,t,n){var o=window.innerWidth;return(t===window?o+window.pageXOffset:s(t)+o)<=s(e)-n},l=function(e,t,n){return(t===window?window.pageYOffset:o(t))>=o(e)+n+e.offsetHeight},a=function(e,t,n){return(t===window?window.pageXOffset:s(t))>=s(e)+n+e.offsetWidth},c=function(e,t){var n,o=new e(t);try{n=new CustomEvent("LazyLoad::Initialized",{detail:{instance:o}})}catch(e){(n=document.createEvent("CustomEvent")).initCustomEvent("LazyLoad::Initialized",!1,!1,{instance:o})}window.dispatchEvent(n)},u=function(e,t){return e.getAttribute("data-"+t)},d=function(e,t,n){return e.setAttribute("data-"+t,n)},f=function(e){return d(e,"was-processed","true")},_=function(e){return"true"===u(e,"was-processed")},h=function(e,t){return t?e.replace(/\.(jpe?g|png)/gi,".webp"):e},p="undefined"!=typeof window,m=p&&!("onscroll"in window)||/(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent),g=p&&"classList"in document.createElement("p"),v=p&&function(){var e=document.createElement("canvas");return!(!e.getContext||!e.getContext("2d"))&&0===e.toDataURL("image/webp").indexOf("data:image/webp")}(),w=function(e,t,n,o){for(var i,s=0;i=e.children[s];s+=1)if("SOURCE"===i.tagName){var r=u(i,n);b(i,t,r,o)}},b=function(e,t,n,o){n&&e.setAttribute(t,h(n,o))},y=function(e,t){var n=v&&t.to_webp,o=u(e,t.data_src);if(o){var i=h(o,n);e.style.backgroundImage='url("'+i+'")'}},E={IMG:function(e,t){var n=v&&t.to_webp,o=t.data_srcset,i=e.parentNode;i&&"PICTURE"===i.tagName&&w(i,"srcset",o,n);var s=u(e,t.data_sizes);b(e,"sizes",s);var r=u(e,o);b(e,"srcset",r,n);var l=u(e,t.data_src);b(e,"src",l,n)},IFRAME:function(e,t){var n=u(e,t.data_src);b(e,"src",n)},VIDEO:function(e,t){var n=t.data_src,o=u(e,n);w(e,"src",n),b(e,"src",o)}},L=function(e,t){var n=e.tagName,o=E[n];o?o(e,t):y(e,t)},T=function(e,t){g?e.classList.add(t):e.className+=(e.className?" ":"")+t},S=function(e,t){g?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s+)"+t+"(\\s+|$)")," ").replace(/^\s+/,"").replace(/\s+$/,"")},O=function(e){this._settings=_extends({},t(),e),this._queryOriginNode=this._settings.container===window?document:this._settings.container,this._previousLoopTime=0,this._loopTimeout=null,this._boundHandleScroll=this.handleScroll.bind(this),this._isFirstLoop=!0,window.addEventListener("resize",this._boundHandleScroll),this.update()};return O.prototype={_reveal:function(e,t){if(t||!_(e)){var o=this._settings,i=function t(){o&&(e.removeEventListener("load",s),e.removeEventListener("error",t),S(e,o.class_loading),T(e,o.class_error),n(o.callback_error,e))},s=function t(){o&&(S(e,o.class_loading),T(e,o.class_loaded),e.removeEventListener("load",t),e.removeEventListener("error",i),n(o.callback_load,e))};n(o.callback_enter,e),["IMG","IFRAME","VIDEO"].indexOf(e.tagName)>-1&&(e.addEventListener("load",s),e.addEventListener("error",i),T(e,o.class_loading)),L(e,o),n(o.callback_set,e)}},_loopThroughElements:function(t){var o=this._settings,i=this._elements,s=i?i.length:0,r=void 0,l=[],a=this._isFirstLoop;for(r=0;r<s;r++){var c=i[r];o.skip_invisible&&null===c.offsetParent||(m||t||e(c,o.container,o.threshold))&&(a&&T(c,o.class_initial),this.load(c),l.push(r),f(c))}for(;l.length;)i.splice(l.pop(),1),n(o.callback_processed,i.length);0===s&&this._stopScrollHandler(),a&&(this._isFirstLoop=!1)},_purgeElements:function(){var e=this._elements,t=e.length,n=void 0,o=[];for(n=0;n<t;n++){var i=e[n];_(i)&&o.push(n)}for(;o.length>0;)e.splice(o.pop(),1)},_startScrollHandler:function(){this._isHandlingScroll||(this._isHandlingScroll=!0,this._settings.container.addEventListener("scroll",this._boundHandleScroll))},_stopScrollHandler:function(){this._isHandlingScroll&&(this._isHandlingScroll=!1,this._settings.container.removeEventListener("scroll",this._boundHandleScroll))},handleScroll:function(){var e=this._settings.throttle;if(0!==e){var t=Date.now(),n=e-(t-this._previousLoopTime);n<=0||n>e?(this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._previousLoopTime=t,this._loopThroughElements()):this._loopTimeout||(this._loopTimeout=setTimeout(function(){this._previousLoopTime=Date.now(),this._loopTimeout=null,this._loopThroughElements()}.bind(this),n))}else this._loopThroughElements()},loadAll:function(){this._loopThroughElements(!0)},update:function(){this._elements=Array.prototype.slice.call(this._queryOriginNode.querySelectorAll(this._settings.elements_selector)),this._purgeElements(),this._loopThroughElements(),this._startScrollHandler()},destroy:function(){window.removeEventListener("resize",this._boundHandleScroll),this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._stopScrollHandler(),this._elements=null,this._queryOriginNode=null,this._settings=null},load:function(e,t){this._reveal(e,t)}},p&&function(e,t){if(t)if(t.length)for(var n,o=0;n=t[o];o+=1)c(e,n);else c(e,t)}(O,window.lazyLoadOptions),O}); | ||
var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};!function(e,t){"object"===("undefined"==typeof exports?"undefined":_typeof(exports))&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.LazyLoad=t()}(this,function(){"use strict";function e(e,t,n){return!(i(e,t,n)||l(e,t,n)||r(e,t,n)||a(e,t,n))}var t=function(){return{elements_selector:"img",container:window,threshold:300,throttle:150,data_src:"src",data_srcset:"srcset",data_sizes:"sizes",class_loading:"loading",class_loaded:"loaded",class_error:"error",class_initial:"initial",skip_invisible:!0,callback_load:null,callback_error:null,callback_set:null,callback_processed:null,callback_enter:null,to_webp:!1}},n=function(e,t){e&&e(t)},o=function(e){return e.getBoundingClientRect().top+window.pageYOffset-e.ownerDocument.documentElement.clientTop},i=function(e,t,n){return(t===window?window.innerHeight+window.pageYOffset:o(t)+t.offsetHeight)<=o(e)-n},s=function(e){return e.getBoundingClientRect().left+window.pageXOffset-e.ownerDocument.documentElement.clientLeft},r=function(e,t,n){var o=window.innerWidth;return(t===window?o+window.pageXOffset:s(t)+o)<=s(e)-n},l=function(e,t,n){return(t===window?window.pageYOffset:o(t))>=o(e)+n+e.offsetHeight},a=function(e,t,n){return(t===window?window.pageXOffset:s(t))>=s(e)+n+e.offsetWidth},c=function(e,t){var n,o=new e(t);try{n=new CustomEvent("LazyLoad::Initialized",{detail:{instance:o}})}catch(e){(n=document.createEvent("CustomEvent")).initCustomEvent("LazyLoad::Initialized",!1,!1,{instance:o})}window.dispatchEvent(n)},u=function(e,t){return e.getAttribute("data-"+t)},d=function(e,t,n){return e.setAttribute("data-"+t,n)},f=function(e){return d(e,"was-processed","true")},_=function(e){return"true"===u(e,"was-processed")},h=function(e,t){return t?e.replace(/\.(jpe?g|png)/gi,".webp"):e},p="undefined"!=typeof window,m=p&&!("onscroll"in window)||/(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent),g=p&&"classList"in document.createElement("p"),v=p&&function(){var e=document.createElement("canvas");return!(!e.getContext||!e.getContext("2d"))&&0===e.toDataURL("image/webp").indexOf("data:image/webp")}(),w=function(e,t,n,o){for(var i,s=0;i=e.children[s];s+=1)if("SOURCE"===i.tagName){var r=u(i,n);b(i,t,r,o)}},b=function(e,t,n,o){n&&e.setAttribute(t,h(n,o))},y=function(e,t){var n=v&&t.to_webp,o=u(e,t.data_src);if(o){var i=h(o,n);e.style.backgroundImage='url("'+i+'")'}},E={IMG:function(e,t){var n=v&&t.to_webp,o=t.data_srcset,i=e.parentNode;i&&"PICTURE"===i.tagName&&w(i,"srcset",o,n);var s=u(e,t.data_sizes);b(e,"sizes",s);var r=u(e,o);b(e,"srcset",r,n);var l=u(e,t.data_src);b(e,"src",l,n)},IFRAME:function(e,t){var n=u(e,t.data_src);b(e,"src",n)},VIDEO:function(e,t){var n=t.data_src,o=u(e,n);w(e,"src",n),b(e,"src",o),e.load()}},L=function(e,t){var n=e.tagName,o=E[n];o?o(e,t):y(e,t)},T=function(e,t){g?e.classList.add(t):e.className+=(e.className?" ":"")+t},S=function(e,t){g?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s+)"+t+"(\\s+|$)")," ").replace(/^\s+/,"").replace(/\s+$/,"")},O=function(e){this._settings=_extends({},t(),e),this._queryOriginNode=this._settings.container===window?document:this._settings.container,this._previousLoopTime=0,this._loopTimeout=null,this._boundHandleScroll=this.handleScroll.bind(this),this._isFirstLoop=!0,window.addEventListener("resize",this._boundHandleScroll),this.update()};return O.prototype={_reveal:function(e,t){if(t||!_(e)){var o=this._settings,i=function t(){o&&(e.removeEventListener("load",s),e.removeEventListener("error",t),S(e,o.class_loading),T(e,o.class_error),n(o.callback_error,e))},s=function t(){o&&(S(e,o.class_loading),T(e,o.class_loaded),e.removeEventListener("load",t),e.removeEventListener("error",i),n(o.callback_load,e))};n(o.callback_enter,e),["IMG","IFRAME","VIDEO"].indexOf(e.tagName)>-1&&(e.addEventListener("load",s),e.addEventListener("error",i),T(e,o.class_loading)),L(e,o),n(o.callback_set,e)}},_loopThroughElements:function(t){var o=this._settings,i=this._elements,s=i?i.length:0,r=void 0,l=[],a=this._isFirstLoop;for(r=0;r<s;r++){var c=i[r];o.skip_invisible&&null===c.offsetParent||(m||t||e(c,o.container,o.threshold))&&(a&&T(c,o.class_initial),this.load(c),l.push(r),f(c))}for(;l.length;)i.splice(l.pop(),1),n(o.callback_processed,i.length);0===s&&this._stopScrollHandler(),a&&(this._isFirstLoop=!1)},_purgeElements:function(){var e=this._elements,t=e.length,n=void 0,o=[];for(n=0;n<t;n++){var i=e[n];_(i)&&o.push(n)}for(;o.length>0;)e.splice(o.pop(),1)},_startScrollHandler:function(){this._isHandlingScroll||(this._isHandlingScroll=!0,this._settings.container.addEventListener("scroll",this._boundHandleScroll))},_stopScrollHandler:function(){this._isHandlingScroll&&(this._isHandlingScroll=!1,this._settings.container.removeEventListener("scroll",this._boundHandleScroll))},handleScroll:function(){var e=this._settings.throttle;if(0!==e){var t=Date.now(),n=e-(t-this._previousLoopTime);n<=0||n>e?(this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._previousLoopTime=t,this._loopThroughElements()):this._loopTimeout||(this._loopTimeout=setTimeout(function(){this._previousLoopTime=Date.now(),this._loopTimeout=null,this._loopThroughElements()}.bind(this),n))}else this._loopThroughElements()},loadAll:function(){this._loopThroughElements(!0)},update:function(){this._elements=Array.prototype.slice.call(this._queryOriginNode.querySelectorAll(this._settings.elements_selector)),this._purgeElements(),this._loopThroughElements(),this._startScrollHandler()},destroy:function(){window.removeEventListener("resize",this._boundHandleScroll),this._loopTimeout&&(clearTimeout(this._loopTimeout),this._loopTimeout=null),this._stopScrollHandler(),this._elements=null,this._queryOriginNode=null,this._settings=null},load:function(e,t){this._reveal(e,t)}},p&&function(e,t){if(t)if(t.length)for(var n,o=0;n=t[o];o+=1)c(e,n);else c(e,t)}(O,window.lazyLoadOptions),O}); | ||
//# sourceMappingURL=lazyload.min.js.map |
{ | ||
"name": "vanilla-lazyload", | ||
"version": "8.15.0", | ||
"version": "8.15.1", | ||
"description": "A fast, lightweight script to load images as they enter the viewport. SEO friendly, it supports responsive images (both srcset + sizes and picture) and progressive JPEG", | ||
@@ -5,0 +5,0 @@ "main": "dist/lazyload.min.js", |
427
README.md
LazyLoad is a fast, lightweight and flexible script that _speeds up your web application_ by **loading images, video or iframes as they enter the viewport**. It's written in plain "vanilla" JavaScript, uses [Intersection Observers](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API), and supports [responsive images](https://alistapart.com/article/responsive-images-in-practice). It's also SEO-friendly and it has some other [notable features](#notable-features). | ||
Jump to: | ||
➡️ Jump to: [👨💻 Include the script](#-include-the-script) - [🥧 Recipes](#-recipes) - [📺 Demos](#-demos) - [😋 Tips & tricks](#-tips--tricks) - [🔌 API](#-api) - [😯 Notable features](#-notable-features) | ||
[Include the script](#include-the-script) | [Recipes](#recipes) | [Demos](#demos) | [Tips & tricks](#tips--tricks) | [API](#api) | [Notable features](#notable-features) | ||
--- | ||
## 👨💻 Include the script | ||
## Include the script / browser support | ||
### Versions information | ||
### Simple: direct include from cdnjs | ||
The **universal, recommended version** of LazyLoad is **8.x** as it **supports ALL browsers** from IE9 up. | ||
The **universal, recommended version** of LazyLoad is 8.x since it **supports ALL browsers** from IE9 up. | ||
Version **10.x** is best for performance since it leverages IntersectionObserver API, which is [not supported by Internet Explorer and Safari](https://caniuse.com/#feat=intersectionobserver), therefore all the images would be loaded at once in those browsers. | ||
Version **8.x** is recommended for [local install](#local-install), but you can be smart and [conditionally load the best version](#conditional-load) from cdnjs instead. | ||
### Include as script from cdnjs | ||
Version 8.x - [versions info](#versions-information) | ||
```html | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/8.15.0/lazyload.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/8.15.1/lazyload.min.js"></script> | ||
``` | ||
Starting from version 9, LazyLoad uses the IntersectionObserver API, which is not supported by Internet Explorer and Safari (yet). As a result, if you included the latest version of LazyLoad, all the images would be loaded at once in those browsers. | ||
Version 10.x - [versions info](#versions-information) | ||
To include the [latest version](https://cdnjs.com/libraries/vanilla-lazyload) of LazyLoad, use the following script: | ||
```html | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/10.15.0/lazyload.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/10.16.1/lazyload.min.js"></script> | ||
``` | ||
### Advanced and best option: conditionally load version 8 or 10 | ||
The file `lazyload.min.js` is provided as UMD (<small>Universal Module Definition</small>). | ||
<br>See [bundles](#bundles) for more module types like AMD, IIFE and ES6 module. | ||
The best thing you can do is to conditionally load the best version of LazyLoad **depending on the browser's support of the IntersectionObserver API**. | ||
#### Async script | ||
It's possible to include it as an `async` script, see the [recipes](#recipes) section for more information. | ||
#### Conditional load | ||
The best thing you can do for **runtime performance** is to **conditionally load** the appropriate version of LazyLoad depending on browser support of IntersectionObserver. | ||
You can do it with the following script: | ||
@@ -33,35 +46,104 @@ | ||
(function(w, d){ | ||
var b = d.getElementsByTagName('body')[0]; | ||
var s = d.createElement("script"); s.async = true; | ||
var v = !("IntersectionObserver" in w) ? "8.15.0" : "10.15.0"; | ||
s.src = "https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/" + v + "/lazyload.min.js"; | ||
w.lazyLoadOptions = {}; // Your options here. See "recipes" for more information about async. | ||
b.appendChild(s); | ||
var b = d.getElementsByTagName('body')[0]; | ||
var s = d.createElement("script"); | ||
var v = !("IntersectionObserver" in w) ? "8.15.1" : "10.16.1"; | ||
s.async = true; // This includes the script as async. See the "recipes" section for more information about async loading of LazyLoad. | ||
s.src = "https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/" + v + "/lazyload.min.js"; | ||
w.lazyLoadOptions = {/* Your options here */}; | ||
b.appendChild(s); | ||
}(window, document)); | ||
``` | ||
See the conditional_load.html file in the demos folder to try it or play around with it. | ||
See `demos/conditional_load.html` to try and play around with it. | ||
The file `lazyload.min.js` is provided as UMD (<small>Universal Module Definition</small>). | ||
<br>See [bundles](#bundles) for more module types like AMD, IIFE and ES6 module. | ||
### Include via RequireJS | ||
If you use [RequireJS](https://requirejs.org) to dynamically load modules in your website, you can take advantage of it. | ||
```js | ||
define("vanilla-lazyLoad", ["https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/8.15.1/lazyload.amd.min.js"], function (LazyLoad) { | ||
return LazyLoad; | ||
}); | ||
``` | ||
You can also [conditionally load](#conditional-load) the best version. | ||
```js | ||
var v = !("IntersectionObserver" in window) ? "8.15.1" : "10.16.1"; | ||
define("vanilla-lazyLoad", ["https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/" + v + "/lazyload.amd.min.js"], function (LazyLoad) { | ||
return LazyLoad; | ||
}); | ||
``` | ||
### Local install | ||
If you prefer to install LazyLoad locally in your project, you can either: | ||
- **download it** from the [`dist` folder](https://github.com/verlok/lazyload/tree/master/dist) | ||
The file you typically want to use is `lazyload.min.js` | ||
If you prefer the ES2015 version, use `lazyload.es2015.js` | ||
- **install it with npm** | ||
Recommended version `npm install vanilla-lazyload@8.15.0` | ||
Latest version `npm install vanilla-lazyload` | ||
- **install it with bower** | ||
Recommended version `bower install vanilla-lazyload#8.15.0` | ||
Latest version `bower install vanilla-lazyload` | ||
### Async script | ||
It's possible to include it as an `async` script, see [Recipes](#recipes) below. | ||
#### Install with npm | ||
## Recipes | ||
Version 8.x, _recommended_ - [versions info](#versions-information) | ||
``` | ||
npm install vanilla-lazyload@8.15.1 | ||
``` | ||
Version 10.x - [versions info](#versions-information) | ||
``` | ||
npm install vanilla-lazyload@10.16.1 | ||
``` | ||
#### Install with bower | ||
Install with bower is also possible using `bower install vanilla-lazyload#{version}` | ||
#### Manual download | ||
Download one the latest [releases](https://github.com/verlok/lazyload/releases/). The files you need are inside the `dist` folder. | ||
The file `lazyload.min.js` is provided as UMD (<small>Universal Module Definition</small>). | ||
<br>See [bundles](#bundles) for more module types like AMD, IIFE and ES6 module. | ||
### Local usage | ||
Should you install LazyLoad locally, you can import it as ES module like the following: | ||
```js | ||
import LazyLoad from "vanilla-lazyload"; | ||
``` | ||
It's also possible (but unadvised) to use the `require` commonJS syntax. | ||
More information about bundling LazyLoad with WebPack are available on [this specific repo](https://github.com/verlok/lazyload-es2015-webpack-test). | ||
### Usage with React | ||
Take a look at this example of [usage of React with LazyLoad](https://codesandbox.io/s/20306yk96p) on Sandbox. | ||
This implementation takes the same props that you would normally pass to the `img` tag, but it renders a lazy image. Feel free to fork and improve it! | ||
### Bundles | ||
Inside `dist` folder you find different bundles. | ||
| Filename | Module Type | Advantages | | ||
| ---------------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| `lazyload.min.js` | UMD <small>(Universal Module Definition)</small> | Works pretty much everywhere, even in common-js contexts | | ||
| `lazyload.iife.min.js` | IIFE <small>(Immediately Invoked Function Expression)</small> | Works as in-page `<script src="...">`, ~0.5kb smaller than UMD version | | ||
| `lazyload.amd.min.js` | AMD <small>(Asynchronous Module Definition)</small> | Works with *RequireJS* module loader, ~0.5kb smaller than UMD version | | ||
| `lazyload.es2015.js` | ES6 Module | Exports `LazyLoad` so you can import it in your project both using `<script type="module" src="...">` and a bundler like WebPack or Rollup | | ||
--- | ||
## 🥧 Recipes | ||
This is the section where you can find _copy & paste_ code for your convenience. | ||
### Simple | ||
**When to use**: your lazy images are (normally) located in the body of a scrolling page. | ||
> 💡 **Use case**: your lazy images are (normally) located in the body of a scrolling page. | ||
@@ -84,8 +166,85 @@ HTML | ||
[DEMO](http://verlok.github.io/lazyload/demos/simple.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/simple.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/simple.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/simple.html) - [API](#-api) | ||
### Responsive images - srcset and sizes | ||
### Scrolling panel | ||
> **When to use**: you want to lazily load responsive images using the `srcset` and the `sizes` attribute. | ||
> 💡 **Use case**: when your scrolling container is not the main browser window, but a scrolling container. | ||
HTML | ||
```html | ||
<div class="scrollingPanel"> | ||
<img alt="Image description" | ||
data-src="../img/44721746JJ_15_a.jpg" | ||
width="220" height="280"> | ||
<!-- More images --> | ||
</div> | ||
``` | ||
CSS | ||
```css | ||
.scrollingPanel { | ||
overflow-y: scroll; | ||
-webkit-overflow-scrolling: touch; | ||
} | ||
``` | ||
Javascript | ||
```js | ||
var myLazyLoad = new LazyLoad({ | ||
container: document.getElementById('scrollingPanel') | ||
}); | ||
``` | ||
[DEMO](http://verlok.github.io/lazyload/demos/single_container.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/single_container.html) - [API](#-api) | ||
### Multiple scrolling panels | ||
> 💡 **Use case**: when your scrolling container is not the main browser window, and you have multiple scrolling containers. | ||
HTML | ||
```html | ||
<div id="scrollingPanel1" class="scrollingPanel"> | ||
<img alt="Image description" | ||
data-src="../img/44721746JJ_15_a.jpg" | ||
width="220" height="280"> | ||
<!-- More images --> | ||
</div> | ||
<div id="scrollingPanel2" class="scrollingPanel"> | ||
<img alt="Image description" | ||
data-src="../img/44721746JJ_15_a.jpg" | ||
width="220" height="280"> | ||
<!-- More images --> | ||
</div> | ||
``` | ||
CSS | ||
```css | ||
.scrollingPanel { | ||
overflow-y: scroll; | ||
-webkit-overflow-scrolling: touch; | ||
} | ||
``` | ||
Javascript | ||
```js | ||
var myLazyLoad1 = new LazyLoad({ | ||
container: document.getElementById('scrollingPanel1') | ||
}); | ||
var myLazyLoad2 = new LazyLoad({ | ||
container: document.getElementById('scrollingPanel2') | ||
}); | ||
``` | ||
[DEMO](http://verlok.github.io/lazyload/demos/multiple_container.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/multiple_container.html) - [API](#-api) | ||
### Responsive images - img tag with srcset / sizes | ||
> 💡 **Use case**: you want to lazily load responsive images using the `srcset` and the `sizes` attribute. | ||
HTML | ||
@@ -107,7 +266,7 @@ | ||
[DEMO](http://verlok.github.io/lazyload/demos/with_srcset_lazy_sizes.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/with_srcset_lazy_sizes.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/with_srcset_lazy_sizes.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/with_srcset_lazy_sizes.html) - [API](#-api) | ||
### Responsive images - picture | ||
### Responsive images - picture tag | ||
> **When to use**: you want to lazily load responsive images using the `picture` tag. | ||
> 💡 **Use case**: you want to lazily load responsive images using the `picture` tag. | ||
@@ -124,2 +283,4 @@ HTML | ||
Please note that you just need to put the `lazy` class on the `<img>` tag but **not in the `<source>` tags**. | ||
Javascript | ||
@@ -133,7 +294,57 @@ | ||
[DEMO](http://verlok.github.io/lazyload/demos/with_picture.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/with_picture.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/with_picture.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/with_picture.html) - [API](#-api) | ||
### Switch to WebP | ||
> 💡 **Use case**: you want to dynamically switch your images' filename extension to `.webp` if the user's browser supports it. | ||
HTML | ||
```html | ||
<img class="lazy" data-src="/your/image1.jpg" | ||
data-srcset="/your/image1.jpg 200w, /your/image1@2x.jpg 400w" | ||
data-sizes="(min-width: 20em) 35vw, 100vw"> | ||
``` | ||
Javascript | ||
```js | ||
var myLazyLoad = new LazyLoad({ | ||
elements_selector: ".lazy", | ||
to_webp: true | ||
}); | ||
``` | ||
**Hint**: if you provide **only some images** in the WebP format, it's advisable to create 2 different instances of LazyLoad, as shown in the [this demo](http://verlok.github.io/lazyload/demos/to_webp_some.html) and [source code](https://github.com/verlok/lazyload/blob/master/demos/to_webp_some.html). | ||
[DEMO](http://verlok.github.io/lazyload/demos/to_webp_all.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/to_webp_all.html) - [API](#-api) | ||
### Delay load | ||
> 💡 **Use case**: you want the images to stay inside the viewport for some time before to start loading them, e.g. to skip loading some images them if the user scrolled fast after them. | ||
HTML | ||
```html | ||
<img class="lazy" alt="..." | ||
data-src="../img/44721746JJ_15_a.jpg" | ||
width="220" height="280"> | ||
``` | ||
Javascript | ||
```js | ||
var myLazyLoad = new LazyLoad({ | ||
elements_selector: ".lazy", | ||
load_delay: 300 //adjust according to use case | ||
}); | ||
``` | ||
[DEMO](http://verlok.github.io/lazyload/demos/delay.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/delay.html) | [API](#-api) | ||
### Videos | ||
> **When to use**: you want to lazily load videos using the `video` tag. | ||
> 💡 **Use case**: you want to lazily load videos using the `video` tag. | ||
@@ -159,7 +370,7 @@ HTML | ||
[DEMO](http://verlok.github.io/lazyload/demos/video.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/video.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/video.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/video.html) - [API](#-api) | ||
### Iframes | ||
> **When to use**: you want to lazily load `iframe`s. | ||
> 💡 **Use case**: you want to lazily load `iframe`s. | ||
@@ -180,7 +391,7 @@ HTML | ||
[DEMO](http://verlok.github.io/lazyload/demos/iframes.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/iframes.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/iframes.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/iframes.html) - [API](#-api) | ||
### Async script + auto initialization | ||
> **When to use**: you want to use a non-blocking script (which is faster), and you don't need to have control on the exact moment when LazyLoad is created. | ||
> 💡 **Use case**: you want to use a non-blocking script (which is faster), and you don't need to have control on the exact moment when LazyLoad is created. | ||
@@ -218,9 +429,9 @@ Include the following scripts **at the end of** your HTML page, right before closing the `body` tag. | ||
Please note that if you put the script at the beginning of your HTML page, LazyLoad will sometimes be executed before the browser has loaded all the DOM. | ||
In that case, you need to store the instance in a variable and use the `update` method on it. This will make it check the DOM again. See [API](#api). | ||
In that case, you need to store the instance in a variable and use the `update` method on it. This will make it check the DOM again. See [API](#-api). | ||
[DEMO](http://verlok.github.io/lazyload/demos/async.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/async.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/async.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/async.html) - [API](#-api) | ||
#### Auto init + store the instance in a variable | ||
> **When to use**: you want to use a non-blocking script (which is faster), you don't need to have control on the exact moment when LazyLoad is created, but you need to assign the an auto-initialized instance to a variable, e.g. to use the [API](#api) on it. | ||
> 💡 **Use case**: you want to use a non-blocking script (which is faster), you don't need to have control on the exact moment when LazyLoad is created, but you need to assign the an auto-initialized instance to a variable, e.g. to use the [API](#-api) on it. | ||
@@ -249,3 +460,3 @@ HTML + Javascript | ||
[DEMO](http://verlok.github.io/lazyload/demos/async.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/async.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/async.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/async.html) - [API](#-api) | ||
@@ -274,83 +485,5 @@ **Note about Internet Explorer** | ||
### Scrolling panel | ||
> **When to use**: when your scrolling container is not the main browser window, but a scrolling container. | ||
HTML | ||
```html | ||
<div class="scrollingPanel"> | ||
<img alt="Image description" | ||
data-src="../img/44721746JJ_15_a.jpg" | ||
width="220" height="280"> | ||
<!-- More images --> | ||
</div> | ||
``` | ||
CSS | ||
```css | ||
.scrollingPanel { | ||
overflow-y: scroll; | ||
-webkit-overflow-scrolling: touch; | ||
} | ||
``` | ||
Javascript | ||
```js | ||
var myLazyLoad = new LazyLoad({ | ||
container: document.getElementById('scrollingPanel') | ||
}); | ||
``` | ||
[DEMO](http://verlok.github.io/lazyload/demos/single_container.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/single_container.html) | [API](#api) | ||
### Multiple scrolling panels | ||
> **When to use**: when your scrolling container is not the main browser window, and you have multiple scrolling containers. | ||
HTML | ||
```html | ||
<div id="scrollingPanel1" class="scrollingPanel"> | ||
<img alt="Image description" | ||
data-src="../img/44721746JJ_15_a.jpg" | ||
width="220" height="280"> | ||
<!-- More images --> | ||
</div> | ||
<div id="scrollingPanel2" class="scrollingPanel"> | ||
<img alt="Image description" | ||
data-src="../img/44721746JJ_15_a.jpg" | ||
width="220" height="280"> | ||
<!-- More images --> | ||
</div> | ||
``` | ||
CSS | ||
```css | ||
.scrollingPanel { | ||
overflow-y: scroll; | ||
-webkit-overflow-scrolling: touch; | ||
} | ||
``` | ||
Javascript | ||
```js | ||
var myLazyLoad1 = new LazyLoad({ | ||
container: document.getElementById('scrollingPanel1') | ||
}); | ||
var myLazyLoad2 = new LazyLoad({ | ||
container: document.getElementById('scrollingPanel2') | ||
}); | ||
``` | ||
[DEMO](http://verlok.github.io/lazyload/demos/multiple_container.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/multiple_container.html) | [API](#api) | ||
### Dynamic content | ||
> **When to use**: when you want to lazily load images, but the number of images change in the scrolling area changes, maybe because they are added asynchronously. | ||
> 💡 **Use case**: when you want to lazily load images, but the number of images change in the scrolling area changes, maybe because they are added asynchronously. | ||
@@ -369,7 +502,7 @@ HTML | ||
[DEMO](http://verlok.github.io/lazyload/demos/dynamic_content.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/dynamic_content.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/dynamic_content.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/dynamic_content.html) - [API](#-api) | ||
### Lazy iframes | ||
> **When to use**: you want to lazily load `iframe`s in your web page, maybe because you have many or just because you want to load only what your users actually want to see. | ||
> 💡 **Use case**: you want to lazily load `iframe`s in your web page, maybe because you have many or just because you want to load only what your users actually want to see. | ||
@@ -390,7 +523,7 @@ HTML | ||
[DEMO](http://verlok.github.io/lazyload/demos/iframes.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/iframes.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/iframes.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/iframes.html) - [API](#-api) | ||
### Lazy background images | ||
> **When to use**: your images are set as CSS background images instead of real `img`, but you still want to lazily load them. | ||
> 💡 **Use case**: your images are set as CSS background images instead of real `img`, but you still want to lazily load them. | ||
@@ -413,7 +546,7 @@ HTML | ||
[DEMO](http://verlok.github.io/lazyload/demos/background_images.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/background_images.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/background_images.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/background_images.html) - [API](#-api) | ||
### Lazy LazyLoad | ||
> **When to use**: when you have a lot of scrolling containers in the page and you want to instantiate a LazyLoad only on the ones that are in the viewport. | ||
> 💡 **Use case**: when you have a lot of scrolling containers in the page and you want to instantiate a LazyLoad only on the ones that are in the viewport. | ||
@@ -458,6 +591,8 @@ HTML | ||
[DEMO](http://verlok.github.io/lazyload/demos/lazily_load_lazyLoad.html) | [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/lazily_load_lazyLoad.html) | [API](#api) | ||
[DEMO](http://verlok.github.io/lazyload/demos/lazily_load_lazyLoad.html) - [SOURCE](https://github.com/verlok/lazyload/blob/master/demos/lazily_load_lazyLoad.html) - [API](#-api) | ||
## Demos | ||
--- | ||
## 📺 Demos | ||
Didn't find the [recipe](#recipes) that exactly matches your case? We have demos! | ||
@@ -467,4 +602,6 @@ | ||
## Tips & tricks | ||
--- | ||
## 😋 Tips & tricks | ||
### Occupy vertical space and maintain ratio | ||
@@ -528,3 +665,3 @@ | ||
Or instead of the above `:not()` selector do it using the **CSS classes** of `class_loading` and `class_loaded` set by LazyLoad when loading starts or is completed - see [API](#api). | ||
Or instead of the above `:not()` selector do it using the **CSS classes** of `class_loading` and `class_loaded` set by LazyLoad when loading starts or is completed - see [API](#-api). | ||
@@ -563,4 +700,6 @@ | ||
## API | ||
--- | ||
## 🔌 API | ||
### Constructor arguments | ||
@@ -606,2 +745,4 @@ | ||
| `class_error` | The class applied to the elements when the element causes an error | `"error"` | | ||
| `to_webp` | A boolean flag that activates the dynamic switch to WEBP feature. [More info](#switch-to-webp). | `false` | | ||
| `load_delay` | [**Available only in version 10.16.1-beta**] The time (in milliseconds) each image needs to stay inside the viewport before its loading begins. | `0` | | ||
| `callback_enter` | A function to be called when the DOM element enters the viewport. | `null` | | ||
@@ -623,4 +764,6 @@ | `callback_set` | A function to be called after the src of an image is set in the DOM. | `null` | | ||
## Notable features | ||
--- | ||
## 😯 Notable features | ||
### SEO friendly | ||
@@ -642,2 +785,6 @@ | ||
### Dynamic switch to WEBP | ||
[WebP](https://developers.google.com/speed/webp/) is a modern image format that provides superior lossless and lossy compression for images on the web. If you are providing your images in the WebP format too, LazyLoad can switch the filenames extension to `.webp` before the image is loaded, given that the user's browser supports it. See [WebP support table](https://caniuse.com/#feat=webp). | ||
### Intersection Observer API for optimized CPU usage | ||
@@ -644,0 +791,0 @@ |
@@ -59,2 +59,3 @@ import { getData } from "./lazyload.data"; | ||
setAttributeIfValue(element, "src", srcDataValue); | ||
element.load(); | ||
}; | ||
@@ -61,0 +62,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1098706
2272
777