GithubHelp home page GithubHelp logo

lukeed / onloaded Goto Github PK

View Code? Open in Web Editor NEW
39.0 3.0 6.0 7 KB

A tiny (350B) library to detect when images have loaded.

Home Page: https://jsfiddle.net/lukeed/8njejbjz/

License: MIT License

JavaScript 100.00%
images ready is-loading imagesloaded lightweight javascript

onloaded's Introduction

onloaded

A tiny (350B) library to detect when images have loaded

Demo

This module exposes three module definitions:

  • ES Module: dist/onloaded.es.js
  • CommonJS: dist/onloaded.js
  • UMD: dist/onloaded.min.js

If using the UMD bundle, the library is exposed as onloaded globally.

Install

$ npm install --save onloaded

Usage

const onloaded = require('onloaded');

// passing a selector to `elem`
onloaded('#container > img', {
  onLoad(img) {
    img.className += ' loaded';
  },
  onError(img) {
    img.className += ' failed';
  },
  onProgress(val) {
    console.log(`I am ${ val * 100 }% complete!`);
  },
  onComplete(val, stats) {
    // val is always 1 ~~> 100%
    console.log('This callback always runs!');
    console.log(`  ${stats.loaded} loaded`);
    console.log(`  ${stats.failed} failed`);
    console.log(`  ${stats.total} total`);
  }
});

Note: Visit elem for other possibilities!

API

onloaded(elem, options)

elem

Type: String|Node|NodeList

You have several options here:

  1. Pass a selector string to img element(s);

    onloaded('.container img', { ... });
  2. Pass a reference to a specific <img /> DOM Node;

    var img = document.querySelector('.container img');
    onloaded(img, { ... });
  3. Pass a reference to a multiple <img /> DOM Nodes;

    var imgs = document.querySelectorAll('.container img');
    onloaded(imgs, { ... });
  4. Pass a reference to a container DOM Node that contains <img /> elements;

    var parent = document.querySelector('.container');
    onloaded(parent, { ... });

options.onError(node)

Type: Function

Callback whenever an image source failed to load. Receives the <img /> DOM node;

options.onLoad(node)

Type: Function

Callback whenever an image source sucessfully loads. Receives the <img /> DOM node;

options.onProgress(val, stats)

Type: Function

Callback whenever an image's network request has completed, regardless of success or failure.

Receives the current "progress" of completed requests as a decimal.

Also receives a stats object with loaded, failed, and total keys.

options.onComplete(val, stats)

Type: Function

Callback when all network requests have completed, regardless of success or failure.

Receives the "progress" as its first parameter. This will always equal 1.

Also receives a stats object with loaded, failed, and total keys.

License

MIT ยฉ Luke Edwards

onloaded's People

Contributors

lukeed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

onloaded's Issues

[RFC] Listen to CSS Background Images

If this lands, it will be a 2.0 release & thus will be the distinguishing feature between v1 and v2.

Most of the time when I'm trying to do a fair bit of animating, it's never on a <img/> directly. It's nearly always on a <div> with a background-image style property.

In those cases, I'd (presumably) like to know when the image has completely downloaded before I begin the non-critical animation. IMO, it's far worse to have something fly/scale in, only to be partially loaded and/or lag the browser while it tries to pain the image and animate at the same time.

Here's how it would look, I think:

// load all <div>s' background-images
onloaded('.slide > div', { background:true });

// load the background-images on .container's .slide nodes
onloaded('.container', { background:'.slide' });

I'm only guessing, but doing so may add roughly ~100-200 bytes, which is a significant increase since 1.0 is only 350 bytes.

Please cast your vote below ๐Ÿ˜†



Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.