• Stars
    star
    3,723
  • Rank 11,854 (Top 0.3 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lazy-loading images with data-* attributes

Echo.js Build Status

Echo is a standalone JavaScript lazy-loading image micro-library. Echo is fast, 2KB, and uses HTML5 data-* attributes for simple. Check out a demo. Echo works in IE8+.

bower install echojs
npm install echo-js

Using Echo.js is simple, to add an image directly into the page simply add a data-echo attribute to the img tag. Alternatively if you want to use Echo to lazy load background images simply add a `data-echo-background' attribute to the element with the image URL.

<body>

  <img src="img/blank.gif" alt="Photo" data-echo="img/photo.jpg">

  <script src="dist/echo.js"></script>
  <script>
  echo.init({
    offset: 100,
    throttle: 250,
    unload: false,
    callback: function (element, op) {
      console.log(element, 'has been', op + 'ed')
    }
  });

  // echo.render(); is also available for non-scroll callbacks
  </script>
</body>

.init() (options)

The init() API takes a few options

offset

Type: Number|String Default: 0

The offset option allows you to specify how far below, above, to the left, and to the right of the viewport you want Echo to begin loading your images. If you specify 0, Echo will load your image as soon as it is visible in the viewport, if you want to load 1000px below or above the viewport, use 1000.

offsetVertical

Type: Number|String Default: offset's value

The offsetVertical option allows you to specify how far above and below the viewport you want Echo to begin loading your images.

offsetHorizontal

Type: Number|String Default: offset's value

The offsetHorizontal option allows you to specify how far to the left and right of the viewport you want Echo to begin loading your images.

offsetTop

Type: Number|String Default: offsetVertical's value

The offsetTop option allows you to specify how far above the viewport you want Echo to begin loading your images.

offsetBottom

Type: Number|String Default: offsetVertical's value

The offsetBottom option allows you to specify how far below the viewport you want Echo to begin loading your images.

offsetLeft

Type: Number|String Default: offsetVertical's value

The offsetLeft option allows you to specify how far to left of the viewport you want Echo to begin loading your images.

offsetRight

Type: Number|String Default: offsetVertical's value

The offsetRight option allows you to specify how far to the right of the viewport you want Echo to begin loading your images.

throttle

Type: Number|String Default: 250

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 250 milliseconds.

debounce

Type: Boolean Default: true

By default the throttling function is actually a debounce function so that the checking function is only triggered after a user stops scrolling. To use traditional throttling where it will only check the images every throttle milliseconds, set debounce to false.

unload

Type: Boolean Default: false

This option will tell echo to unload loaded images once they have scrolled beyond the viewport (including the offset area).

callback

Type: Function

The callback will be passed the element that has been updated and what the update operation was (ie load or unload). This can be useful if you want to add a class like loaded to the element. Or do some logging.

echo.init({
  callback: function(element, op) {
    if(op === 'load') {
      element.classList.add('loaded');
    } else {
      element.classList.remove('loaded');
    }
  }
});

.render()

Echo's callback render() can be used to make Echo poll your images when you're not scrolling, for instance if you've got a filter layout that swaps images but does not scroll, you need to call the internal functions without scrolling. Use render() for this:

echo.render();

Using render() is also throttled, which means you can bind it to an onresize event and it will be optimised for performance in the same way onscroll is.

Manual installation

Drop your files into your required folders, make sure you're using the file(s) from the dist folder, which is the compiled production-ready code. Ensure you place the script before the closing </body> tag so the DOM tree is populated when the script runs.

Configuring Echo

Add the image that needs to load when it's visible inside the viewport in a data-echo attribute:

<img src="img/blank.gif" alt="Photo" data-echo="img/photo.jpg">

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Gulp.

License

MIT license

More Repositories

1

angularjs-styleguide

AngularJS styleguide for teams
5,965
star
2

fireshell

Fiercely quick front-end boilerplate and workflows, HTML5, Grunt, Sass.
JavaScript
1,519
star
3

angular-1-5-components-app

A Contacts Manager application built on Angular 1.5 components, ui-router 1.0.0, Firebase.
JavaScript
573
star
4

flare

Unobtrusive event emitter API for Google Universal Analytics event tracking
JavaScript
505
star
5

angular-pizza-creator

Source code for Angular advanced Form APIs pizza builder
TypeScript
450
star
6

fluidvids

Fluid width (responsive) videos module, 1KB, custom players, dynamic elements/XHR support.
JavaScript
415
star
7

angular-dynamic-forms

Configurable Reactive Forms in Angular with dynamic components
TypeScript
317
star
8

angular-architecture

Angular architecture guide for teams.
262
star
9

apollo

Standalone cross-browser DOM class manipulation module
JavaScript
250
star
10

angular-component

Fully featured .component() polyfill for Angular v1.3+
JavaScript
191
star
11

reactive-pizza

Angular v4.1.1 - Reactive Forms, Observables, Component Architecture. Pizza. πŸ•
TypeScript
180
star
12

axis

Simple JavaScript type checking module. Returns a Boolean for each type check.
JavaScript
179
star
13

superbox

Superbox, the lightbox, reimagined
JavaScript
125
star
14

flaunt-js

Flaunt JS, stylish responsive navigations with nested click to reveal
CSS
89
star
15

linkjuice

Automatically wraps and creates anchor links for <h1-h6> headings
JavaScript
88
star
16

foreach

forEach implementation for Objects/NodeLists/Arrays, automatic type loops and context options
JavaScript
81
star
17

lunar

SVG class module for has/add/remove/toggleClass
JavaScript
78
star
18

stratos

Object manipulation module, simplified extends, keys, add/remove methods and more.
JavaScript
72
star
19

interpolate

Micro templating engine, maps Object property values to a String template
JavaScript
56
star
20

console.loge

Doge-driven debugging. Such console, many logs. Wow.
JavaScript
47
star
21

jbar

jBar, the lightweight jQuery notification banner.
CSS
32
star
22

superfast-angular-2

A super simple setup for Angular 2 to prototype or play around. Components, TypeScript and Routing.
JavaScript
32
star
23

gulp-oss

My Gulp.js boilerplate for creating new JavaScript projects
JavaScript
28
star
24

NoFi

No WiFi, detecting offline states with HTML5
JavaScript
25
star
25

switch-messages

switchMessages for Angular 1.2+, partial functionality from Angular 1.3+ "ngMessages"
JavaScript
24
star
26

fluidvids-react

React.js implementation of fluidvids.js
JavaScript
23
star
27

track-digests

A simple Directive to track $digest counts
JavaScript
21
star
28

psswrd

Show/hide toggling for password inputs.
JavaScript
21
star
29

ac-store

Simple Redux Store
TypeScript
19
star
30

angular-connect-2016

Final code for AngularConnect Angular 1 & 2 demos.
HTML
18
star
31

suave

Re-engineering the HTML5 <video> tag for semantics and modularity.
JavaScript
16
star
32

ac-boilerplate

TypeScript
15
star
33

mapify

Map iteration for Arrays and Objects
JavaScript
14
star
34

fluidvids-polymer

Web Component for fluidvids.js built with Polymer
14
star
35

ngrx-workshop

JavaScript
12
star
36

data-tabs

Simple HTML5 data-* jQuery tabs. Markup free and relative to their container for ultimate re-use.
12
star
37

workspaces-grunt

Using Grunt and Chrome Workspaces for in-browser development
JavaScript
10
star
38

fowa2014

FOWA 2014 workshop
CSS
7
star
39

ngWorkshop2015

[this is really old, wouldn't bother using it] Falsy Values AngularJS Workshop 2015
JavaScript
5
star
40

babel-private-properties-methods-classes

πŸ”₯ Babel + ESLint + Private Properties and Methods in Classes
5
star
41

imgShufflr

jQuery random image shufflr, inline img and background CSS
5
star
42

ngbe-2016

Source code moved here: https://github.com/toddmotto/angular-pizza-creator
5
star
43

selectnav

JavaScript select navigation for responsive projects
JavaScript
4
star
44

fitdevcomments

3
star
45

aot-loader

[BETA/Work In Progress] Ahead-of-Time Compiler for Webpack.
JavaScript
3
star
46

web-platform-angular-2

HTML
3
star
47

required-fallback

A robust HTML5 'required' attribute fallback, client-side jQuery script.
3
star
48

bounceThis

jQuery plugin that mimics CSS3 keyframes animations of bouncing headers
JavaScript
2
star
49

kendo-html-editor

HTML
2
star
50

fowd

CSS
1
star