Giter VIP home page Giter VIP logo

progressively's Introduction

Progressively

Travis Coveralls npm (scoped) David Standard - JavaScript Style Guide

A JavaScript library to load images progressively

It’s written entirely in JavaScript so it doesn’t depend on 3rd-party libraries like jQuery. It's super small, < 1.2kB when minified & gzipped! It will load the full-size images only when the user browses to that part of the page, saving bandwidth & server requests. It is compatible with all modern browsers. See the Demo.

demo-image

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save-dev progressively

Alternatively you can use Bower.

$ bower install progressively

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import progressively from 'progressively'

// using CommonJS modules
var progressively = require('progressively')

The UMD build is also available on unpkg:

<script src="https://unpkg.com/progressively/dist/progressively.min.js"></script>

You can find the library on window.progressively.

You also need to embed the css file at your page.

<link rel="stylesheet" href="https://unpkg.com/progressively/dist/progressively.min.css">

Usage

Add a image to your HTML file setting the src attribute containing the lower quality image (< 20kb for ideal cases) and the data-progressive attribute holding the path/url to the high quality image.

<figure class="progressive">
  <img class="progressive__img progressive--not-loaded" data-progressive="img/highQualityImg.png" src="img/lowQualityImg.png">
</figure>

And initiate the script.

progressively.init()

See demo for examples.

API

progressively.init(options)

The init() API has a few options

throttle

Type: Number Default: 300

The throttle is managed by an internal function that prevents performance issues from continuous firing of window.onscroll events. Using a throttle will set a small timeout when the user scrolls and will keep throttling until the user stops. The default is 300 milliseconds.

delay

Type: Number Default: 100 value

The delay function sets the timout value for images to start load asynchronously. Ideally it's value should be low.

onloadComplete

Type: Function Arguments: None

The afterload function is callback function which executes when all images have loaded. It is fired when all the image elements have the *--is-loaded class.

onload

Type: Function Arguments: HTMLElement

The imgload function is invoked whenever an image elements finishes loading. It accepts HTMLElement as an argument which is the current element that is loaded.

progressively.init({
  delay: 50,
  throttle: 300,
  imgload: function(elem) {
    console.log(elem);
  },
  afterload: function() {
    console.log('All images have finished loading!');
  }
});

progressively.render()

Progressively has a render() method that can be used to make progressively poll your images when you're not scrolling. For instance in some case you want to render your images before/widthout scrolling down to the image, you can use render.

Contribute

See the contributing file for instructions.

License

MIT license Β© Ashish

progressively's People

Contributors

thinker3197 avatar thiamsantos avatar nahid avatar jezmck avatar

Watchers

sarawuthza avatar

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.