
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
@smartimpact-it/lazyload
Advanced tools
A set of lazyload utilities, for images, scripts, styles and iframes
This library contains some JavaScript plugins to allow lazy-loading images, scripts, stylesheets, iframes and more. The lazy-load functionality also works with pictures tags.
To initialize the library, you need to create a new instance of the lazyLoadAssets
class:
new lazyLoadAssets(document.body, {
plugins: [...],
imagesLoaded: imagesLoaded, // optional
})
For example:
import {
activateOnScroll,
lazyVideo,
lazyIframe,
lazyScripts,
lazyStyles,
lazyCssBg,
lazyLoadAssets,
animate,
onInteractive,
} from '@smartimpact-it/lazyload';
import imagesLoaded from 'imagesloaded';
onInteractive(() => {
new lazyLoadAssets(document.body, {
plugins: [
activateOnScroll,
lazyVideo,
lazyIframe,
lazyCssBg,
lazyScripts,
lazyStyles,
animate,
],
imagesLoaded,
});
});
Starting with version 2.0, if you want to use ImagesLoaded, you must pass it to the lazyLoadAssets constructor.
v2.0
activateOnScroll
The activateOnScroll
plugins is the base plugin that triggers actions when elements come into the viewport. It also is the plugin that implements lazy-load for images/picture tags.
You can also use the plugin as standalone, whenever you need to trigger some event when a specific DOM element comes into the viewport:
new activateOnScroll(element, options);
The options object can have the following properties:
callback
- the callback function to be run when the element comes into view (it receives the element that is being activated as a parameter);activatedClass
- the class to be added when the element has been activated (default: animation-started
);activatedClassTargetSelector
- a selector for the element that should receive the "activatedClass" class, when it is a child of the actual element (default: false
);initAttribute
= the dataset attribute to set when activated (it is used to prevent duplicate activations. Default: activateOnScrollInit
),options
- the settings for the IntersectionObserver (default: { rootMargin: '100px 0px' }
).Image elements that will be activated by this plugin should have their structure like this:
<img data-lazy-img data-src="https://test.image" alt="" />
lazyCssBg
This plugin is used for elements which have their background image set by CSS.
<style>
#my-element[data-css-bg-loaded] {
background-image: url(https://test.image);
}
</style>
<div id="my-element" data-lazy-css-bg>....</div>
The element will receive the "data-css-bg-loaded" attribute when it gets into view.
lazyIframe
Used to lazy-load iframes.
<iframe data-lazysrc="https://my.iframe"></iframe>
lazyScripts
Used to load script tags only when a specific element is visible. For example, to lazy-load magnific-popup only when the newsletter section in the footer is visible. Or to lazy-load Mailchimp/Sendinblue scripts until the newsletter section is visible.
Use the data-lazy-script
attribute, which accepts a single value or an array of script URLs.
<section class="newsletter-section" data-lazy-script="[https://mailchimp.some.script]">
Mailchimp content
</section>
lazyStyles
Used to load stylesheets only when a specific element is visible. Similar to lazy-loaded scripts.
Use the data-lazy-stylesheet
attribute, which accepts a single value or an array of stylesheet URLs.
Warning! Do not use data-lazy-script
and data-lazy-stylesheet
on the same element. Use them in separate elements (perhaps a nested element structure).
<section class="newsletter-section" data-lazy-script="[https://mailchimp.some.script]">
<div class="" data-lazy-stylesheet="[https://mailchimp.some.styles]">
Mailchimp content
</div>
</section>
animate
This plugin will add a specified class on the elements when they come into view. It's similar to AOS, but much simpler/more limited.
<style>
[data-animate='load-from-left'] {
transform: translateX(-100px);
transition: transform 0.3s;
}
[data-animate='load-from-left'].load-from-left {
transform: translateX(0);
}
// or
[data-animate='load-from-left'][data-animate-init] {
transform: translateX(0);
}
</style>
<div data-animate="load-from-left">Text loaded from left...</div>
When the element comes into view, it will receive the load-from-left
class which can be used to animate the element.
You can also set the rootMargin
for the IntersectionObserver by using the data-root-margin
attribute.
<div data-animate="load-from-left" data-root-margin="500px 200px">
Text loaded from left...
</div>
lazyVideo
This will allow you to create video elements that are loaded only when they come into view.
Use the data-lazy-video
attribute on the video element, and replace src
with data-src
on the source element tags:
<video data-lazy-video autoplay muted loop playsinline>
<source data-src="one-does-not-simply.webm" type="video/webm" />
<source data-src="one-does-not-simply.mp4" type="video/mp4" />
</video>
When new content is inserted into the page (either by AJAX, or by sliders that duplicate content etc.), the new content is automatically parsed and lazy-loaded. So you don't need to call the initializer again. It will just work.
When initialized, if jQuery is present on the page, the plugins will also make themselves available as jQuery plugins:
$('.my-element').activateOnScroll({
callback: function (element) {
console.log(element);
},
});
FAQs
A set of lazyload utilities, for images, scripts, styles and iframes
We found that @smartimpact-it/lazyload demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.