├── b.gif ├── test ├── ajax │ ├── lazyload.html │ └── images.html ├── imgs │ ├── 1.jpg │ ├── 10.jpg │ ├── 11.jpg │ ├── 12.jpg │ ├── 13.jpg │ ├── 14.jpg │ ├── 15.jpg │ ├── 16.jpg │ ├── 17.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ ├── 5.jpg │ ├── 6.jpg │ ├── 7.jpg │ ├── 8.jpg │ ├── 9.jpg │ ├── test-image.jpg │ └── google-mail.jpg ├── 14.html ├── ajax-loaded-images-ondemand.html ├── detached-below-the-fold.html ├── slow-domready.html ├── fast-domready.html ├── detached-in-viewport.html ├── onload.html ├── ajax-loaded-images.html ├── getattribute.html ├── test.html ├── test_minified.html └── double-script.html ├── index.js ├── package.json ├── TODO.md ├── lazyload.min.js ├── README.md ├── .jshintrc └── lazyload.js /b.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/b.gif -------------------------------------------------------------------------------- /test/ajax/lazyload.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /test/imgs/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/1.jpg -------------------------------------------------------------------------------- /test/imgs/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/10.jpg -------------------------------------------------------------------------------- /test/imgs/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/11.jpg -------------------------------------------------------------------------------- /test/imgs/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/12.jpg -------------------------------------------------------------------------------- /test/imgs/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/13.jpg -------------------------------------------------------------------------------- /test/imgs/14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/14.jpg -------------------------------------------------------------------------------- /test/imgs/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/15.jpg -------------------------------------------------------------------------------- /test/imgs/16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/16.jpg -------------------------------------------------------------------------------- /test/imgs/17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/17.jpg -------------------------------------------------------------------------------- /test/imgs/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/2.jpg -------------------------------------------------------------------------------- /test/imgs/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/3.jpg -------------------------------------------------------------------------------- /test/imgs/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/4.jpg -------------------------------------------------------------------------------- /test/imgs/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/5.jpg -------------------------------------------------------------------------------- /test/imgs/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/6.jpg -------------------------------------------------------------------------------- /test/imgs/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/7.jpg -------------------------------------------------------------------------------- /test/imgs/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/8.jpg -------------------------------------------------------------------------------- /test/imgs/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/9.jpg -------------------------------------------------------------------------------- /test/imgs/test-image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/test-image.jpg -------------------------------------------------------------------------------- /test/imgs/google-mail.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fasterize/lazyload/HEAD/test/imgs/google-mail.jpg -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports.lazyload = require('fs').readFileSync(__dirname + '/lazyload.min.js', 'utf8'); 2 | module.exports.lazyloadDebug = require('fs').readFileSync(__dirname + '/lazyload.js', 'utf8'); 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lazyload", 3 | "description": "Image lazy loading", 4 | "version": "0.8.5", 5 | "repository": { 6 | "type": "git", 7 | "url": "http://github.com/fasterize/lazyload.git" 8 | }, 9 | "private": true 10 | } 11 | -------------------------------------------------------------------------------- /test/14.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Test 5 | 6 | 7 | 8 | 9 | a 10 |
11 | b 12 |
13 | 14 | 15 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | - We should handle the case where an hidden lazy image trigger onload after we unsubscribed 2 | Right now we do not restart listeners so that image will never be shown 3 | - We could use this handy function for h/v offset checking 4 | ```js 5 | function elementInViewport(el) { 6 | var rect = el.getBoundingClientRect() 7 | return ( 8 | rect.top + rect.height >= 0 && 9 | rect.left + rect.width >= 0 && 10 | rect.bottom - rect.height <= window.innerHeight && 11 | rect.right - rect.width <= window.innerWidth 12 | ) 13 | } 14 | ``` 15 | - Add automated tests using casperjs 16 | - Check for resize event 17 | - Add IE6/IE7 support ? + Test 18 | - Add "used by" section in README 19 | - Provide fallback for users w/o JS ? 20 | -------------------------------------------------------------------------------- /test/ajax-loaded-images-ondemand.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ajax ondemand loaded images 5 | 6 | 7 | 8 | 9 | 10 | 11 |

This test checks that after-onload ajax inserted images can still be lazyloaded

12 | 13 | c 14 | 15 | 16 | 21 |
22 | 23 | 24 | -------------------------------------------------------------------------------- /test/detached-below-the-fold.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | detached element test 4 | 5 | 6 | 7 | 8 | 9 |

When

10 |

An image is below the fold

11 |

And it gets registered into the lazyloader

12 |

But is not shown because it is far from the fold

13 |

And it gets removed by some JavaScript

14 |

When we scroll to the bottom, we do not fail in I.E (no error)

15 | 16 |

17 |
18 | k 19 | 20 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /test/slow-domready.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | slow domready 5 | 6 | 7 | 11 | 12 | 13 | 14 |

Document Not Ready ...

15 |

Image should be shown before domready

16 | 17 | a 18 | 19 |

This test check that even if domready event is very slow, we still manage to show a lazyloaded image before the script.

20 | 21 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /test/fast-domready.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | fast domready 5 | 6 | 7 | 8 | 14 | 15 | 16 | 17 |

Document Not Ready ...

18 |

Found src should be imgs/2.jpg

19 | 20 |

Found src:

21 | 22 | a 23 | 24 |

This test check that when the domready event is really fast, we still manage to show the image and switch data-frz-src/src attributes.

25 | 26 |

This will still not work for scripts trying to get the src of lazyloaded images below the fold as their src will still be a blank data-uri image.

27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /test/detached-in-viewport.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | detached images dom nodes 5 | 6 | 7 | 23 | 24 | 25 | 26 | 27 |

This test checks that detached dom nodes can be lazyloaded (fails)

28 | 29 |
30 | 31 |

And now ajax images

32 |
33 | 34 | 35 | -------------------------------------------------------------------------------- /test/onload.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | onload 5 | 6 | 7 | 11 | 12 | 13 | 14 |

Hide images onload test

15 | 16 |

This test checks that if we remove an image that trigger some other images to be shown, we will show them

17 | 18 |

Fixed with asking to show image at onload

19 | 20 | 21 |
22 |
23 |
24 |
25 | BAMMMMMMMMMMM! not shown 26 | a 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /test/ajax-loaded-images.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ajax loaded images 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 |

This test checks that ajax inserted images can be lazyloaded

17 | 18 |
19 | b 20 |
21 | c 22 |
23 | d 24 |
25 | e 26 |
27 | f 28 | 29 |

And now ajax images

30 |
31 | 32 | 33 | -------------------------------------------------------------------------------- /lazyload.min.js: -------------------------------------------------------------------------------- 1 | window.lzld||function(e,d){function m(){n=!0;f();setTimeout(f,25)}function p(a){var b=0;return function(){var c=+new Date;20>c-b||(b=c,a.apply(this,arguments))}}function h(a,b,c){a.attachEvent?a.attachEvent&&a.attachEvent("on"+b,c):a.addEventListener(b,c,!1)}function k(a,b,c){a.detachEvent?a.detachEvent&&a.detachEvent("on"+b,c):a.removeEventListener(b,c,!1)}function q(a,b){return y(d.documentElement,a)&&a.getBoundingClientRect().topc?Math.max(0,d+c):c:0;c 2 | 3 | 4 | getAttribute test 5 | 6 | 7 | 8 | 9 | 10 | 40 | 41 |

We checks that in any situation when a script requests an img src, we give real src, not 42 | the fake blank image.

43 | 44 |

A lot of slideshows are trying to get src attributes. Attention, we cannot handle img['src']

45 | 46 |

It passes if it says `success`:

47 | 48 | 49 | 50 | 51 | 52 |
53 | 54 | 55 | 56 | 57 | 58 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /test/ajax/images.html: -------------------------------------------------------------------------------- 1 | a 2 |
3 | b 4 |
5 | c 6 |
7 | d 8 |
9 | e 10 |
11 | f 12 |
13 | g 14 |
15 | h 16 |
17 | i 18 |
19 | j 20 |
21 | k 22 |
23 | l 24 |
25 | m 26 |
27 | n 28 |
29 | o 30 | 31 | -------------------------------------------------------------------------------- /test/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | standard test 5 | 6 | 7 | 8 | 9 | 10 | a 11 |
12 | b 13 |
14 | c 15 |
16 | d 17 |
18 | e 19 |
20 | f 21 |
22 | g 23 |
24 | h 25 |
26 | i 27 |
28 | j 29 |
30 | k 31 |
32 | l 33 |
34 | m 35 |
36 | n 37 |
38 | o 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /test/test_minified.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | minified test 5 | 6 | 7 | 8 | 9 | a 10 |
11 | b 12 |
13 | c 14 |
15 | d 16 |
17 | e 18 |
19 | f 20 |
21 | g 22 |
23 | h 24 |
25 | i 26 |
27 | j 28 |
29 | k 30 |
31 | l 32 |
33 | m 34 |
35 | n 36 |
37 | o 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /test/double-script.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | double script insertion 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 |

This test checks that when lazyload scripts is inserted more than once

17 |

We do not fail

18 |

Like calling an ajax html page that has the script in it

19 | 20 | a 21 |
22 | b 23 |
24 | c 25 |
26 | d 27 |
28 | e 29 |
30 | f 31 |
32 | g 33 |
34 | h 35 |
36 | i 37 |
38 | j 39 |
40 | k 41 |
42 | l 43 |
44 | m 45 |
46 | n 47 |
48 | o 49 |
50 | 51 | 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lazyload 2 | 3 | This image lazyloader is designed to help you save http requests on images. 4 | 5 | Most of the time, when you have 100 images on a page, your user doesn't need them all. 6 | 7 | This lazyloader will only load what is necessary. 8 | 9 | It's a standalone script that weights ~1KB minified gzipped. 10 | 11 | ## Using it in your project, website or any application 12 | 13 | You should use the `lazyload.min.js` file from this repo. It has a line with [licence](#Licence) information in it that is mandatory. 14 | 15 | It helps us to be rewarded for our work and you to always have a link to this project. 16 | 17 | ## How to use 18 | 19 | 1. Add lazyload.min.js to your page before any `