airfranceklm / ng-lazy-image

Angular directive for loading responsive image when container (which is preventing reflow) is in viewport.
http://afklm.github.io/ng-lazy-image/
MIT License
350 stars 64 forks source link
proposed-to-be-archived

Build Status Coverage Status Sauce Test Status

NPM

Angular Lazy Image directive

Directive for loading responsive/ adaptive image when parent container (which is preventing reflow) is in viewport. Available as bower and npm component for your project. JavaScript only component.

Demo

Demonstration component

Usage

  1. npm install angular-lazy-image or bower install afkl-lazy-image -p and set this script as a dependency
  2. Add a dependency on afkl.lazyImage in your app module.
  3. See the style.css for some classes you can use (they prevent reflow and are optional)

Directive in html template

<div afkl-lazy-image="//placehold.it/480x480 480w, //placehold.it/768x768 768w, //placehold.it/936x936"
    class="afkl-lazy-wrapper afkl-img-ratio-1-1 own-classname"></div>

The attributes are using the srcset setup. Your window will determine which image fits best (so the rules are very dynamic using w/h/x). The image will only be set when the parent container scrolls in the viewport (lazy loading) or scrollable directive. It is also possible to set one image url only then you will not use the fully responsive option but always lazy load just one image. During loading a class 'afkl-lazy-image-loading' is set on the container so you can give loading state feedback to your visitor. Be sure to use block level for this directive since it checks for visibility as well (inline elements have no width on start, and we check for width to determine if element is visible).

Options

Todo

  1. Angular directives don't have any notion of style encapsulation, but Angular is expected to incorporate that functionality eventually. At the moment our optional css is delivered in an own stylesheet.
  2. Check for native browser support
  3. Look at sizes implementation (100vw)
  4. Rewrite pixeldensity to width (new spec), making it simple blog

Help

Grunt tasks

Source file of this bower module is placed at 'src/lazy-image.js'. Our bower content is also available at 'dist/'

Prerequisites

  1. Run npm run update-webdriver once to get latest chrome webdriver (e2e)
  2. Install InternetExplorerDriver

Now we can simply run:

History

This module was made while working on our Travel Inspiration Finder at KLM. Please do not use version 0.2.3 since it will generate an error when you change view. Try to upgrade to version 0.3.1 where the only change is that how you set a className and title on the image.

License

As AngularJS itself, this module is released under the permissive MIT license.

Your contributions are always welcome. Feel free to ask for new features. Contributions by: SquadraCorse, ledzep2, TimonVS, manifestinteractive, awalker, MichaelTSS, smnbbrv, burakkirkil.