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

rehab-rip

v1.0.0

Published

Gathers up image elements in the DOM denoted with a particular class and hides them until their source has been determined and preloaded.

Downloads

4

Readme

Travis npm

Responsive Image Preloader (RIP)

Introduction

Responsive Image Preloader gathers up image elements in the DOM denoted with a particular class, determines the relevant source for the current breakpoint then preloads it and applies a class to the image. This library will also work on standard non-responsive images.

The library has been built upon the rehabstudio FE Skeleton so any specifics about installation, task documentation or setup can be read from the documentation on that repository.

Installation

Install this package via the usual npm commmands or you could download this repository as a zip and take the relevant library bundle file from the dist/ folder.

npm install responsive-image-preloader --save

Example

An example page with multiple use cases (standard image, responsive image) can be found within the examples folder of the repository.

Usage

Imagery requiring hidden and preloaded should be denoted with a particular JS hook (which is configurable) and another selector that hides the image until it has been loaded:

<img class="rip js-rip-preload" src="/abc.jpg" alt="test image" />

<picture>
    <source srcset="/large.jpg" media="(min-width: 800px)" />
    <source srcset="/medium.jpg" media="(min-width: 640px)" />
    <img class="rip js-rip-preload" src="/small.jpg" alt="test image" />
</picture>

Once the image markup has been implemented correctly, add the stylesheet and script to your HTML document:

<link href="rip.css" rel="stylesheet" />
<script src="rip.js"></script>

With the images and library files in place the only thing left to do is create an instance of the library and trigger its preloading functionality:

<script>
    var imagePreloader = new Rip();
    imagePreloader.triggerPreloading();
</script>

The relevant image sources will now be preloaded in the background (responsive images will be polled until their currentSrc attribute has been set by the browser to denote it has made a decision on what source is best for the current breakpoint) and after preloading has completed, a CSS selector will be added to the image element to make it transition back into view.

Documentation

The constructor method permits a multitude of settings to be specified in an options object whenever it is called:

| Setting | Description | Type | Example Value | |---------|-------------|------|---------------| | scanInterval | The frequency (ms) with which image elements should be tested for their currentSrc value being set. | Number | 150 | | scanTimeout | The amount of time (ms) that can pass before the interval doing the scanning gets forcibly cleared. | Number | 6000 | | imageSelector | A valid CSS selector to select image elements needing preloaded. | String | 'js-rip-preload' | | imageLoadedSelector | A CSS selector applied after an image element finishes preloading. | String | 'rip--loaded' | | onLoad | Logic to run whenever an image has been successfully preloaded. | Function | function(imageElement) { imageElement.classList.add('rip--loaded'); } |

Example of overriding the defaults above:

var imagePreloader = new Rip({
    scanInterval: 300,
    scanTimeout: 5000,
    imageSelector: 'js-preload-me',
    imageLoadedSelector: 'preloaded',
    onLoad: function(img) { img.classList.add('preloaded'); }
});

imagePreloader.triggerPreloading();

Compiling

If you wish to manually compile the latest library files then clone the repository and install npm packages:

npm install

Once the build tools have been installed you can use an npm script to bundle the source code into the library files:

npm run-script build

This command will generate both unminified and minified versions of the script along with source maps for both.

Testing

The library comes with a test suite to ensure it operates as functionally expected. If you wish to run this test suite (which also includes source file linting) then run the following npm command:

npm test