GithubHelp home page GithubHelp logo

kkpalanisamy / lazy-image Goto Github PK

View Code? Open in Web Editor NEW

This project forked from notwaldorf/lazy-image

0.0 2.0 0.0 761 KB

A custom image element that lets you load resources on demand

Home Page: https://meowni.ca/lazy-image/

License: MIT License

CSS 7.10% Shell 14.60% HTML 56.24% JavaScript 22.07%

lazy-image's Introduction

lazy-image

<lazy-image> is a custom element that contains an image that has the option to be loaded only on-demand, for performance reasons. If a <lazy-image> is inactive, then its source is not loaded (the XHR won't be made until the active attribute is set on the element).

<lazy-image> has 3 attributes:

  • src the image source (like you would use with a <img>
  • alt, the alternate text for the image the image source (like you would use with a <img>)
  • active, whether the image should be loaded or not

How to use

You can install the element with bower (npm support coming soon, hold tight)

bower install notwaldorf/lazy-image

Then you can drop a <script src="bower_components/lazy-image.js"></script> in the page where you want to use it. Custom elements/Shadow DOM are not yet supported in all browsers, so we recommend the webcomponentsjs polyfills. In my demo I use the webcomponents-sd-ce.js bundle (because I am not using any HTML Imports and I didn't bother with IE11), but for a fancier way to load the polyfills, check the webcomponents-loader and their docs.

On-demand loading

For a <lazy-image> to load, it must have the active property set to true. In the example below, the images will only load when clicked (you can check the network tab in your favourite developer tools to see that there's no initial request for these files). This happens because they each start off with the active property set to false, and have a click event listener, that sets the <lazy-image>'s active attribute to true:

<lazy-image src="..." alt="..." id="i"></lazy-image>

<script>
  i.addEventListener('click', function() {
    if (!this.active)
      this.active = true;
  });
</script>

If you want to have some global setting that controls all <lazy-images> on the page (i.e. activates or deactivates all of them), you can set the window.LazyImageSiteDefaultActive global before loading the lazy-image.js script.

Intersection Observer

Intersection observers let you figure out when an element enters into view. Combined with a <lazy-image>, this lets you only load images that are scrolled into view, while leaving images at the bottom of the page that haven't been seen inactive.

// Create an observer.
var observer = new IntersectionObserver(onChange, {
  threshold: [0.5]  // rootMargin: '50% 0%'
});

// That observes all the random images we've created.
els.forEach(el => observer.observe(el));

// Whenever we scroll...
function onChange(changes) {
  changes.forEach(change => {
    var el = change.target;
    if (!el.active)
      el.active = true;
    observer.unobserve(el);  // Don't care anymore.
  });
}

Intersection observers make me sad because they're awesome but only work in Chrome ๐Ÿ’”.

๐Ÿ˜˜, monica

lazy-image's People

Contributors

notwaldorf avatar

Watchers

 avatar  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.