├── .gitignore
├── .jshintrc
├── .travis.yml
├── LICENSE.md
├── Makefile
├── README.md
├── lazyload.js
├── lazyload.min.js
├── lazyload.min.js.map
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | yarn-error.log
2 | yarn.lock
3 | node_modules
--------------------------------------------------------------------------------
/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | "esversion": 6,
3 | "curly": true,
4 | "eqeqeq": true,
5 | "eqnull": true,
6 | "immed": true,
7 | "noarg": true,
8 | "quotmark": "double",
9 | "trailing": true,
10 | "undef": true,
11 | "unused": "vars",
12 |
13 | "node": true,
14 | "jquery": true,
15 | "browser": true,
16 |
17 | "predef": [
18 | "define",
19 | "IntersectionObserver"
20 | ]
21 | }
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 |
3 | language: node_js
4 |
5 | node_js:
6 | - "7"
7 |
8 | install:
9 | - npm install
10 |
11 | script:
12 | - make travis
13 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 | =====================
3 |
4 | Copyright (c) 2007-2019 Mika Tuupola
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .DEFAULT_GOAL := help
2 |
3 | help:
4 | @echo ""
5 | @echo "Available tasks:"
6 | @echo " lint Run linter and code style checker"
7 | @echo " unit Run unit tests and generate coverage"
8 | @echo " test Run linter and unit tests"
9 | @echo " watch Run linter and unit tests when any of the source files change"
10 | @echo " deps Install dependencies"
11 | @echo " build Build minified version"
12 | @echo " all Install dependencies and run linter and unit tests"
13 | @echo ""
14 |
15 | deps:
16 | yarn install
17 |
18 | lint:
19 | node_modules/.bin/jshint lazyload.js
20 |
21 | unit:
22 | @echo "No unit tests."
23 |
24 | watch:
25 | find . -name "*.js" -not -path "./node_modules/*" -o -name "*.json" -not -path "./node_modules/*" | entr -c make test
26 |
27 | test: lint unit
28 |
29 | travis: lint unit
30 |
31 | build:
32 | node_modules/.bin/uglifyjs lazyload.js --compress --mangle --source-map --output lazyload.min.js
33 | sed -i "1s/^/\/*! Lazy Load 2.0.0-rc.2 - MIT license - Copyright 2007-2019 Mika Tuupola *\/\n/" lazyload.min.js
34 |
35 | all: deps test build
36 |
37 | .PHONY: help deps lint test watch build all
38 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Lazy Load Remastered
2 |
3 | Lazy Load delays loading of images in long web pages. Images outside of viewport will not be loaded before user scrolls to them. This is opposite of image preloading.
4 |
5 | This is a modern vanilla JavaScript version of the original [Lazy Load](https://github.com/tuupola/jquery_lazyload) plugin. It uses [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) to observe when the image enters the browsers viewport. Original code was inspired by [YUI ImageLoader](https://yuilibrary.com/yui/docs/imageloader/) utility by Matt Mlinac. New version loans heavily from a [blog post](https://deanhume.com/Home/BlogPost/lazy-loading-images-using-intersection-observer/10163) by Dean Hume.
6 |
7 | ## Basic Usage
8 |
9 | By default Lazy Load assumes the URL of the original high resolution image can be found in `data-src` attribute. You can also include an optional low resolution placeholder in the `src` attribute.
10 |
11 | ```html
12 |
13 |
14 |
15 |
16 | ```
17 |
18 | With the HTML in place you can then initialize the plugin using the factory method. If you do not pass any settings or image elements it will lazyload all images with class `lazyload`.
19 |
20 | ```js
21 | lazyload();
22 | ```
23 |
24 | If you prefer you can explicitly pass the image elements to the factory. Use this for example if you use different class name.
25 |
26 | ```js
27 | let images = document.querySelectorAll(".branwdo");
28 | lazyload(images);
29 | ```
30 |
31 | If you prefer you can also use the plain old constructor.
32 |
33 | ```js
34 | let images = document.querySelectorAll(".branwdo");
35 | new LazyLoad(images);
36 | ```
37 |
38 | The core IntersectionObserver can be configured by passing an additional argument
39 |
40 | ```js
41 | new LazyLoad(images, {
42 | root: null,
43 | rootMargin: "0px",
44 | threshold: 0
45 | });
46 | ```
47 |
48 | ## Additional API
49 |
50 | To use the additional API you need to assign the plugin instance to a variable.
51 |
52 | ```js
53 | let lazy = lazyload();
54 | ```
55 |
56 | To force loading of all images use `loadimages()`.
57 |
58 | ```js
59 | lazy->loadImages();
60 | ```
61 |
62 | To destroy the plugin and stop lazyloading use `destroy()`.
63 |
64 | ```js
65 | lazy->destroy();
66 | ```
67 |
68 | Note that `destroy()` does not load the out of viewport images. If you also
69 | want to load the images use `loadAndDestroy()`.
70 |
71 | ```js
72 | lazy->loadAndDestroy();
73 | ```
74 |
75 | Additional API is not avalaible with the jQuery wrapper.
76 |
77 | ## jQuery Wrapper
78 |
79 | If you use jQuery there is a wrapper which you can use with the familiar old syntax. Note that to provide BC it uses `data-original` by default. This should be a drop in replacement for the previous version of the plugin.
80 |
81 | ```html
82 |
83 |
84 | ```
85 |
86 | ```js
87 | $("img.lazyload").lazyload();
88 | ```
89 |
90 | ## Cookbook
91 |
92 | ### Blur Up Images
93 |
94 | Low resolution placeholder ie. the "blur up" technique. You can see this in action [in this blog entry](https://appelsiini.net/2017/trilateration-with-n-points/). Scroll down to see blur up images.
95 |
96 | ```html
97 |
101 | ```
102 |
103 | ```html
104 |