Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vanilla-lazyload

Package Overview
Dependencies
Maintainers
1
Versions
148
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vanilla-lazyload - npm Package Compare versions

Comparing version 8.7.1 to 8.8.0

demos/video.html

2

.eslintrc.json

@@ -45,3 +45,3 @@ {

"no-loop-func": 1,
"no-magic-numbers": [2, { "ignore": [0, 1] }],
"no-magic-numbers": [2, { "ignore": [0, 1, -1] }],
"no-multi-spaces": 1,

@@ -48,0 +48,0 @@ "no-multi-str": 1,

@@ -115,13 +115,8 @@ (function (global, factory) {

const setSourcesForPicture = function (element, srcsetDataAttribute) {
const parent = element.parentNode;
if (parent && parent.tagName !== "PICTURE") {
return;
}
for (let i = 0; i < parent.children.length; i++) {
let pictureChild = parent.children[i];
if (pictureChild.tagName === "SOURCE") {
let sourceSrcset = getData(pictureChild, srcsetDataAttribute);
if (sourceSrcset) {
pictureChild.setAttribute("srcset", sourceSrcset);
const setSourcesInChildren = function(parentTag, attrName, dataAttrName) {
for (let i = 0, childTag; childTag = parentTag.children[i]; i += 1) {
if (childTag.tagName === "SOURCE") {
let attributeValue = getData(childTag, dataAttrName);
if (attributeValue) {
childTag.setAttribute(attrName, attributeValue);
}

@@ -132,26 +127,35 @@ }

var setSources = function (element, srcsetDataAttribute, srcDataAttribute) {
const setAttributeIfNotNullOrEmpty = function(element, attrName, value) {
if (!value) {return;}
element.setAttribute(attrName, value);
};
function setSources(element, settings) {
const dataAttrSrcName = settings.data_src;
const elementSrc = getData(element, dataAttrSrcName);
const tagName = element.tagName;
const elementSrc = getData(element, srcDataAttribute);
if (tagName === "IMG") {
setSourcesForPicture(element, srcsetDataAttribute);
const imgSrcset = getData(element, srcsetDataAttribute);
if (imgSrcset) {
element.setAttribute("srcset", imgSrcset);
const dataAttrSrcSetName = settings.data_srcset;
const elementSrcSet = getData(element, dataAttrSrcSetName);
const parent = element.parentNode;
if (parent && parent.tagName === "PICTURE") {
setSourcesInChildren(parent, "srcset", dataAttrSrcSetName);
}
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
setAttributeIfNotNullOrEmpty(element, "srcset", elementSrcSet);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "IFRAME") {
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "VIDEO") {
setSourcesInChildren(element, "src", dataAttrSrcName);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (elementSrc) {
element.style.backgroundImage = `url("${elementSrc}")`;
}
};
}

@@ -225,3 +229,3 @@ const runningOnBrowser = (typeof window !== "undefined");

callCallback(settings.callback_enter, element);
if (element.tagName === "IMG" || element.tagName === "IFRAME") {
if (["IMG", "IFRAME", "VIDEO"].indexOf(element.tagName) > -1) {
element.addEventListener("load", loadCallback);

@@ -231,3 +235,3 @@ element.addEventListener("error", errorCallback);

}
setSources(element, settings.data_srcset, settings.data_src);
setSources(element, settings);
callCallback(settings.callback_set, element);

@@ -234,0 +238,0 @@ },

@@ -113,13 +113,8 @@ 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; };

var setSourcesForPicture = function setSourcesForPicture(element, srcsetDataAttribute) {
var parent = element.parentNode;
if (parent && parent.tagName !== "PICTURE") {
return;
}
for (var i = 0; i < parent.children.length; i++) {
var pictureChild = parent.children[i];
if (pictureChild.tagName === "SOURCE") {
var sourceSrcset = getData(pictureChild, srcsetDataAttribute);
if (sourceSrcset) {
pictureChild.setAttribute("srcset", sourceSrcset);
var setSourcesInChildren = function setSourcesInChildren(parentTag, attrName, dataAttrName) {
for (var i = 0, childTag; childTag = parentTag.children[i]; i += 1) {
if (childTag.tagName === "SOURCE") {
var attributeValue = getData(childTag, dataAttrName);
if (attributeValue) {
childTag.setAttribute(attrName, attributeValue);
}

@@ -130,26 +125,37 @@ }

var setSources = function setSources(element, srcsetDataAttribute, srcDataAttribute) {
var setAttributeIfNotNullOrEmpty = function setAttributeIfNotNullOrEmpty(element, attrName, value) {
if (!value) {
return;
}
element.setAttribute(attrName, value);
};
function setSources(element, settings) {
var dataAttrSrcName = settings.data_src;
var elementSrc = getData(element, dataAttrSrcName);
var tagName = element.tagName;
var elementSrc = getData(element, srcDataAttribute);
if (tagName === "IMG") {
setSourcesForPicture(element, srcsetDataAttribute);
var imgSrcset = getData(element, srcsetDataAttribute);
if (imgSrcset) {
element.setAttribute("srcset", imgSrcset);
var dataAttrSrcSetName = settings.data_srcset;
var elementSrcSet = getData(element, dataAttrSrcSetName);
var parent = element.parentNode;
if (parent && parent.tagName === "PICTURE") {
setSourcesInChildren(parent, "srcset", dataAttrSrcSetName);
}
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
setAttributeIfNotNullOrEmpty(element, "srcset", elementSrcSet);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "IFRAME") {
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "VIDEO") {
setSourcesInChildren(element, "src", dataAttrSrcName);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (elementSrc) {
element.style.backgroundImage = 'url("' + elementSrc + '")';
}
};
}

@@ -227,3 +233,3 @@ var runningOnBrowser = typeof window !== "undefined";

callCallback(settings.callback_enter, element);
if (element.tagName === "IMG" || element.tagName === "IFRAME") {
if (["IMG", "IFRAME", "VIDEO"].indexOf(element.tagName) > -1) {
element.addEventListener("load", loadCallback);

@@ -233,3 +239,3 @@ element.addEventListener("error", errorCallback);

}
setSources(element, settings.data_srcset, settings.data_src);
setSources(element, settings);
callCallback(settings.callback_set, element);

@@ -236,0 +242,0 @@ },

@@ -1,1 +0,1 @@

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";var e=function(){return{elements_selector:"img",container:window,threshold:300,throttle:150,data_src:"src",data_srcset:"srcset",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}},t=!("onscroll"in window)||/glebot/.test(navigator.userAgent),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,n){return!(i(e,t,n)||l(e,t,n)||r(e,t,n)||a(e,t,n))},u=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)},d=function(e,t){return e.getAttribute("data-"+t)},h=function(e,t,n){return e.setAttribute("data-"+t,n)},f=function(e,t){var n=e.parentNode;if(!n||"PICTURE"===n.tagName)for(var o=0;o<n.children.length;o++){var i=n.children[o];if("SOURCE"===i.tagName){var s=d(i,t);s&&i.setAttribute("srcset",s)}}},_=function(e,t,n){var o=e.tagName,i=d(e,n);if("IMG"===o){f(e,t);var s=d(e,t);return s&&e.setAttribute("srcset",s),void(i&&e.setAttribute("src",i))}"IFRAME"!==o?i&&(e.style.backgroundImage='url("'+i+'")'):i&&e.setAttribute("src",i)},p="undefined"!=typeof window,m=p&&"classList"in document.createElement("p"),g=function(e,t){m?e.classList.add(t):e.className+=(e.className?" ":"")+t},v=function(e,t){m?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s+)"+t+"(\\s+|$)")," ").replace(/^\s+/,"").replace(/\s+$/,"")},w=function(t){this._settings=_extends({},e(),t),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()};w.prototype={_reveal:function(e){var t=this._settings,o=function o(){t&&(e.removeEventListener("load",i),e.removeEventListener("error",o),v(e,t.class_loading),g(e,t.class_error),n(t.callback_error,e))},i=function i(){t&&(v(e,t.class_loading),g(e,t.class_loaded),e.removeEventListener("load",i),e.removeEventListener("error",o),n(t.callback_load,e))};n(t.callback_enter,e),"IMG"!==e.tagName&&"IFRAME"!==e.tagName||(e.addEventListener("load",i),e.addEventListener("error",o),g(e,t.class_loading)),_(e,t.data_srcset,t.data_src),n(t.callback_set,e)},_loopThroughElements:function(){var e=this._settings,o=this._elements,i=o?o.length:0,s=void 0,r=[],l=this._isFirstLoop;for(s=0;s<i;s++){var a=o[s];e.skip_invisible&&null===a.offsetParent||(t||c(a,e.container,e.threshold))&&(l&&g(a,e.class_initial),this._reveal(a),r.push(s),h(a,"was-processed",!0))}for(;r.length;)o.splice(r.pop(),1),n(e.callback_processed,o.length);0===i&&this._stopScrollHandler(),l&&(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];d(i,"was-processed")&&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()},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}};var b=window.lazyLoadOptions;return p&&b&&function(e,t){var n=t.length;if(n)for(var o=0;o<n;o++)u(e,t[o]);else u(e,t)}(w,b),w});
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){var n=t.data_src,o=h(e,n),i=e.tagName;if("IMG"===i){var s=t.data_srcset,r=h(e,s),l=e.parentNode;return l&&"PICTURE"===l.tagName&&_(l,"srcset",s),p(e,"srcset",r),void p(e,"src",o)}if("IFRAME"!==i)return"VIDEO"===i?(_(e,"src",n),void p(e,"src",o)):void(o&&(e.style.backgroundImage='url("'+o+'")'));p(e,"src",o)}var t=function(){return{elements_selector:"img",container:window,threshold:300,throttle:150,data_src:"src",data_srcset:"srcset",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}},n=!("onscroll"in window)||/glebot/.test(navigator.userAgent),o=function(e,t){e&&e(t)},i=function(e){return e.getBoundingClientRect().top+window.pageYOffset-e.ownerDocument.documentElement.clientTop},s=function(e,t,n){return(t===window?window.innerHeight+window.pageYOffset:i(t)+t.offsetHeight)<=i(e)-n},r=function(e){return e.getBoundingClientRect().left+window.pageXOffset-e.ownerDocument.documentElement.clientLeft},l=function(e,t,n){var o=window.innerWidth;return(t===window?o+window.pageXOffset:r(t)+o)<=r(e)-n},a=function(e,t,n){return(t===window?window.pageYOffset:i(t))>=i(e)+n+e.offsetHeight},c=function(e,t,n){return(t===window?window.pageXOffset:r(t))>=r(e)+n+e.offsetWidth},u=function(e,t,n){return!(s(e,t,n)||a(e,t,n)||l(e,t,n)||c(e,t,n))},d=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)},h=function(e,t){return e.getAttribute("data-"+t)},f=function(e,t,n){return e.setAttribute("data-"+t,n)},_=function(e,t,n){for(var o,i=0;o=e.children[i];i+=1)if("SOURCE"===o.tagName){var s=h(o,n);s&&o.setAttribute(t,s)}},p=function(e,t,n){n&&e.setAttribute(t,n)},m="undefined"!=typeof window,g=m&&"classList"in document.createElement("p"),v=function(e,t){g?e.classList.add(t):e.className+=(e.className?" ":"")+t},w=function(e,t){g?e.classList.remove(t):e.className=e.className.replace(new RegExp("(^|\\s+)"+t+"(\\s+|$)")," ").replace(/^\s+/,"").replace(/\s+$/,"")},b=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()};b.prototype={_reveal:function(t){var n=this._settings,i=function e(){n&&(t.removeEventListener("load",s),t.removeEventListener("error",e),w(t,n.class_loading),v(t,n.class_error),o(n.callback_error,t))},s=function e(){n&&(w(t,n.class_loading),v(t,n.class_loaded),t.removeEventListener("load",e),t.removeEventListener("error",i),o(n.callback_load,t))};o(n.callback_enter,t),["IMG","IFRAME","VIDEO"].indexOf(t.tagName)>-1&&(t.addEventListener("load",s),t.addEventListener("error",i),v(t,n.class_loading)),e(t,n),o(n.callback_set,t)},_loopThroughElements:function(){var e=this._settings,t=this._elements,i=t?t.length:0,s=void 0,r=[],l=this._isFirstLoop;for(s=0;s<i;s++){var a=t[s];e.skip_invisible&&null===a.offsetParent||(n||u(a,e.container,e.threshold))&&(l&&v(a,e.class_initial),this._reveal(a),r.push(s),f(a,"was-processed",!0))}for(;r.length;)t.splice(r.pop(),1),o(e.callback_processed,t.length);0===i&&this._stopScrollHandler(),l&&(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];h(i,"was-processed")&&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()},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}};var y=window.lazyLoadOptions;return m&&y&&function(e,t){var n=t.length;if(n)for(var o=0;o<n;o++)d(e,t[o]);else d(e,t)}(b,y),b});
{
"name": "vanilla-lazyload",
"version": "8.7.1",
"version": "8.8.0",
"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",

@@ -57,3 +57,3 @@ import getDefaultSettings from "./lazyload.defaults";

callCallback(settings.callback_enter, element);
if (element.tagName === "IMG" || element.tagName === "IFRAME") {
if (["IMG", "IFRAME", "VIDEO"].indexOf(element.tagName) > -1) {
element.addEventListener("load", loadCallback);

@@ -63,3 +63,3 @@ element.addEventListener("error", errorCallback);

}
setSources(element, settings.data_srcset, settings.data_src);
setSources(element, settings);
callCallback(settings.callback_set, element);

@@ -66,0 +66,0 @@ },

import {getData} from "./lazyload.data";
const setSourcesForPicture = function (element, srcsetDataAttribute) {
const parent = element.parentNode;
if (parent && parent.tagName !== "PICTURE") {
return;
}
for (let i = 0; i < parent.children.length; i++) {
let pictureChild = parent.children[i];
if (pictureChild.tagName === "SOURCE") {
let sourceSrcset = getData(pictureChild, srcsetDataAttribute);
if (sourceSrcset) {
pictureChild.setAttribute("srcset", sourceSrcset);
const setSourcesInChildren = function(parentTag, attrName, dataAttrName) {
for (let i = 0, childTag; childTag = parentTag.children[i]; i += 1) {
if (childTag.tagName === "SOURCE") {
let attributeValue = getData(childTag, dataAttrName);
if (attributeValue) {
childTag.setAttribute(attrName, attributeValue);
}

@@ -19,22 +14,31 @@ }

export default function (element, srcsetDataAttribute, srcDataAttribute) {
const setAttributeIfNotNullOrEmpty = function(element, attrName, value) {
if (!value) {return;}
element.setAttribute(attrName, value);
};
export default function setSources(element, settings) {
const dataAttrSrcName = settings.data_src;
const elementSrc = getData(element, dataAttrSrcName);
const tagName = element.tagName;
const elementSrc = getData(element, srcDataAttribute);
if (tagName === "IMG") {
setSourcesForPicture(element, srcsetDataAttribute);
const imgSrcset = getData(element, srcsetDataAttribute);
if (imgSrcset) {
element.setAttribute("srcset", imgSrcset);
const dataAttrSrcSetName = settings.data_srcset;
const elementSrcSet = getData(element, dataAttrSrcSetName);
const parent = element.parentNode;
if (parent && parent.tagName === "PICTURE") {
setSourcesInChildren(parent, "srcset", dataAttrSrcSetName);
}
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
setAttributeIfNotNullOrEmpty(element, "srcset", elementSrcSet);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "IFRAME") {
if (elementSrc) {
element.setAttribute("src", elementSrc);
}
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (tagName === "VIDEO") {
setSourcesInChildren(element, "src", dataAttrSrcName);
setAttributeIfNotNullOrEmpty(element, "src", elementSrc);
return;
}
if (elementSrc) {

@@ -41,0 +45,0 @@ element.style.backgroundImage = `url("${elementSrc}")`;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc