Giter Club home page Giter Club logo

lazy.js's Introduction

lazy.js Build Status devDependency Status

A tiny and dependency-free JavaScript library for lazy loading resources.

What kind of resources can I load?

  • Images
  • Scripts
  • Styles
  • Iframes
  • Videos
  • Audios
  • Background images.

How-to

You should add the library (inline) into your HTML file:

<script>
    /*! lazy.js v0.0.1 Released under the MIT license. */!function(a){"use strict";function b(a){a=a||document.querySelectorAll("[data-lazy]");var b,c,d,e=0;for(void 0===a.length&&(a=[a]),b=a.length,e;b>e;e+=1)c=a[e],d=c.getAttribute("data-lazy"),""!==d&&(c["LINK"!==c.tagName?"src":"href"]=d),c.removeAttribute("data-lazy")}a.lazy=b}(this);
</script>

Images

<img data-lazy="http://foo.bar.com/foobar.png" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" width="400" height="300">
window.onload = function () {
    lazy();
};

Script

<script data-lazy="http://foobar.com/foo.js"></script>
window.onload = function () {
    lazy();
};

Style

<link data-lazy="http://foobar.com/foo.js" rel="stylesheet">
window.onload = function () {
    lazy();
};

Iframe

<iframe data-lazy="http://foobar.com" src="javascript:false"></iframe>
window.onload = function () {
    lazy();
};

Video

<video data-lazy="http://foobar.com/bar.mp4" controls></video>
window.onload = function () {
    lazy();
};

Audio

<audio data-lazy="http://foobar.com/foo.mp3" controls></audio>
window.onload = function () {
    lazy();
};

Background Images

/* You must add the following CSS snippet */
[data-lazy] {
    background-image: none !important;
}

/* Your own CSS */
.box {
    width: 400px;
    height: 300px;
    background: url('http://foobar.com/foobar.png') no-repeat center center;
}
<div data-lazy class="box"></div>
window.onload = function () {
    lazy();
};

Progressive Enhancement is still important

For example, to show images when JavaScript is not enabled you should include the images inside <noscript>.

<img data-lazy="foo.jpg" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" width="400" height="300">
<noscript>
    <img src="foo.jpg" width="400" height="300">
</noscript>

API

lazy([nodes])

Loads resources asynchronously on-demand.

  • nodes (optional): [NodeList] - An node element or a collection of node elements. Default: the result of querySelectorAll('[data-lazy]').
<img data-lazy="http://foo.bar.com/foo.png" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" width="400" height="300">
<img data-lazy="http://foo.bar.com/bar.png" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" width="400" height="300">

<img data-lazy="http://foo.bar.com/foobar.png" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" width="400" height="300" id="onscroll">
window.onload = function () {
    lazy();
};

window.onscroll = function () {
    var ondemand = document.getElementById('onscroll');

    lazy(ondemand);
};

Development setup

  1. Install Git and NodeJS.

  2. Open your terminal and clone mercadolibre/lazy.js by running:

     $ git clone [email protected]:mercadolibre/lazy.js.git
    
  3. Now go to the project's folder:

     $ cd lazy.js
    
  4. Install its dependencies:

     $ npm install
    
  5. Install grunt-cli:

     $ npm install grunt-cli -g
    
  6. Develop! :)

## Grunt tasks

  • grunt dev: Builds a development version.
  • grunt test: Runs Jasmine tests.
  • grunt dist: Creates a distrubution version of lazy.js. You should find two files: .dist/lazy.js and .dist/lazy.min.js.

Contribute

Please read through our code style guide:

## Maintained by

## Credits

MercadoLibre

## License Licensed under the MIT license.

Copyright (c) 2013 MercadoLibre.

lazy.js's People

Contributors

pazguille avatar

Watchers

James Cloos avatar Dogeek 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.