GithubHelp home page GithubHelp logo

unveil's Introduction

#unveil.js ###A very lightweight plugin to lazy load images

Most of us are familiar with the Lazy Load plugin by Mika Tuupola. This plugin is very useful and it boosts performance delaying loading of images in long web pages because images outside of viewport (visible part of web page) won't be loaded until the user scrolls to them. Lazy Load has some cool options such as custom effects, container, events or data attribute. If you're not gonna use any of them you can reduce the file size by leaving just the essential code to show the images. That's what I did and this is my lightweight version of Lazy Load with support for serving high-resolution images to devices with retina displays - less than 1k.

Visit unveil's project page to read the documentation and see the demo.

Usage

Use a placeholder image in the src attribute - something to be displayed while the original image loads - and include the actual image source in a "data-src" attribute. If you want to serve high-resolution images to devices with retina displays, you just have to include the source for those images in a "data-src-retina" attribute. You don't need to include a "data-src-retina" attribute in all the image tags, unveil is smart enough to fallback to "data-src" or even do nothing in case there isn't any "data-src" specified.

<img src="bg.png" data-src="img1.jpg" />
<img src="bg.png" data-src="img2.jpg" data-src-retina="img2-retina.jpg" />

If you care about users without javascript enabled, you can include the original image inside a noscript tag:

<noscript>
  <img src="bg.png" src="img1.jpg" />
</noscript>

Run the script on document ready:

$(document).ready(function() {
  $("img").unveil();
});

###Option By default, images are only loaded and "unveiled" when user scrolls to them and they became visible on the screen. If you want your images to load earlier than that, lets say 200px before they appear on the screen, you just have to:

$("img").unveil( 200 );

###Trigger You can still trigger image loading whenever you need. All you have to do is select the images you want to "unveil" and trigger the event:

$("img").trigger( "unveil" );

###Demo Visit project page to see it working.

###License Unveil is licensed under the MIT license.

unveil's People

Contributors

luis-almeida avatar

Watchers

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