npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ldlazy

v2.0.0

Published

Vanilla JS Lazy Show Library

Downloads

8

Readme

ldlazy

Vanilla JS Lazy Show Library

Usage

install via npm:

npm install ldlazy

include the required files:

<link rel="stylesheet" type="text/css" href="path-to-dist/index.min.css">
<script type="text/javascript" src="path-to-dist/index.min.js"></script>

construct a ldlazy object:

ldlz = new ldlazy({root: document.body,  toggle: function(n) { ... } });
ldlz.add( ... );

Constructor Options

  • root - root element for monitoring scroll.
  • toggle - when visibility changes, toggle will be called with the target node as param. default null.
  • debounce - millisecond delay before handling events. require debounce.js, otherwise will be ignored.

API

  • ldlz.add(n) - start monitoring node n for visibility change.
  • ldlz.remove(n) - stop monitoring node n for visibility change.

Pug Mixin

To make it easier to add an ldlazy image tag, we offer a pug mixin with a minimized gif placeholder, which can be used as:

include path-to-dist/index.pug
+ldlz("your-file")( ...attributes... )

An example with @plotdb/srcbuild:

include @/ldlazy/dist/index.pug
+ldlz("thumbnail.png").custom-cls(width="800px",height="600px",alt="thumbnail")

which generates following html:

<img class="ldlz clustom-cls" src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" data-src="thumbnail.png" width="800px" height="600px" alt="thumbnail">

How It Works

ldlazy controls the visibility of an element based on the event from IntersectionObserver. Two styles will be touched:

  • visibility
  • opacity

Alternatively, you can specify a source url for showing / lazy loading an image when the element becomes visible. Use src or data-src (for lazy loading ) attributes to indicate image url:

<div src=""></div>
<img data-src="...">

ldlazy fill the corresponding field ( style.backgroundImage for div, src for img ) when the element is visible.

Additionally, specify following css class over an element for additional effect:

  • nocache: always load this image with a randomized querystring, which forcily disable the cache.
    • useful for transitional animated SVG
  • placeholder: insert a invisible placeholder image with given URL inside this node.
    • the image may be loaded before visible, so it becomes lazy show instead of lazy load.
    • only applicable for container type node ( such as div ).

Class API

  • init(): ask ldlazy to scan and init all elements with ldlz classes.

Best Practice

When using with img tag, you may want to fill following attributes to prevent CLS - Cumulative Layout Shift:

  • width and height: set to the expected size of your image.

    • this will be overwritten if you also specify values in stylesheet, but it helps in keeping aspect ratio.
  • src: you can still fill src attribute with a 66 bytes long, single pixel, transprent gif:

    • data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAIA
    • this is available as a mixin named ldlz in Pug after including dist/index.pug, with src as only parameter:

    +ldlz("path-to-your-image")(additional-attribute="here", ...)

Placeholder Image

Placeholder image prefilled in src attribute can prevent a broken image or alt attribute to be shown. While we may prefer transparent placeholder image, ldlazy actually make elements transparent before revealing them so we actually don't need a real transpareny pixels.

Here are some possible smallest images, with their corresponding sources:

smallest 1px gif (26bytes/58bytes, currently used), may be shown as black in some browsers (e.g., IE11):

data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=

smallest transparent 1px gif (33bytes/66bytes):

data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAIA

alternative with SVG (41bytes/60bytes)

data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"/>

check following pages for more resources about a minimal image:

  • 26 bytes one:
    • https://github.com/mathiasbynens/small
    • https://stackoverflow.com/questions/6018611/smallest-data-uri-image-possible-for-a-transparent-image
    • http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever
  • 33 bytes one:
    • https://stackoverflow.com/questions/2570633/smallest-filesize-for-transparent-single-pixel-image

License

MIT