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

lazy-video-loader

v1.0.3

Published

A custom video element tag that enables loading of videos only when viewport renders near the element.

Downloads

216

Readme

Lazy Video Loader

LazyVideoLoader is a custom video element that enables loading of videos only when they are in view. This helps to save bandwidth and improve the performance of your web applications by deferring the loading of videos until they are needed.

Description

The LazyVideoLoader custom HTML component is a lightweight solution (~1.8 KB) for lazy loading videos in your web applications. It leverages the Intersection Observer API to detect when the video element enters the viewport and then starts loading the video. The component supports a variety of attributes to customize its behavior.

Benefits

Websites with heavy video content often struggle with slow load times and high data usage, leading to poor user experiences and frustration. This library defers the loading of videos until they enter the viewport, ensuring faster initial page loads and saving bandwidth. This is particularly beneficial for users on mobile devices or with slower internet connections, as it reduces unnecessary data consumption. It also contributes with better SEO by minimizing memory usage even on devices with limited resources, as search engines favor quicker websites.

Installation

You can install LazyVideoLoader via npm:

npm install lazy-video-loader

Or include it directly in your HTML via unpkg:

<script src="https://unpkg.com/lazy-video-loader/lazy-video-loader-minified.js"></script>

Attributes

  • width : The width of the video element.
  • height : The height of the video element.
  • controls : Whether to display video controls. (true/false)
  • loop: Whether to loop the video. (true/false)
  • playsinline : Whether to play the video inline. (true/false)
  • margin : A margin percentage to trigger the video load before it enters the viewport (See below for detailed explanation).
  • manual : If set to true, the video will not automatically load when the viewport is in the margin. (true/false)

Explanation of margin

A value of 0 means that the video will start loading exactly when the viewport passes the bounds of the container. Position values will start loading the video sooner, based on the percentage of the container's height. For example, a value of 0.5 means the video will start loading when the viewport is within half the height of the container above or below it

  • 0: The video will start loading exactly when the viewport reaches the bounds of the container.
  • 0.1 : The video will start loading when the viewport is within 10% of the container's height from the top or bottom.
  • 0.5 : The video will start loading when the viewport is within 50% of the container's height from the top or bottom.

By adjusting the margin attribute, you can control how early or late the video starts loading relative to its position in the viewport, providing flexibility for optimizing user experience and performance.

Usage

Example for implementing Lazy Loading

<lazy-video-loader width="640" height="360" controls loop playsinline>
    <source src="path/to/your/video.mp4" type="video/mp4">
    <source src="path/to/your/video.webm" type="video/webm">
</lazy-video-loader>

Example for implementing Manual Loading

<lazy-video-loader width="640" height="360" controls loop playsinline manual margin="0.5">
    <source src="path/to/your/video.mp4" type="video/mp4">
</lazy-video-loader>

<script>
    const videoLoader = document.querySelector('lazy-video-loader');
    videoLoader.load();
</script>

Example for implementing Custom Poster Image

This example sets a custom poster image for the video. The poster image will also be loaded based on the lazy loading logic, meaning it will only be fetched when the video element enters the viewport.

<lazy-video-loader width="640" height="360" poster="path/to/poster.jpg">
    <source src="path/to/your/video.mp4" type="video/mp4">
</lazy-video-loader>

License

This project is licensed under the Apache 2.0 license

Author

Ben Lim