Repository: tuupola/lazyload
Branch: 2.x
Commit: d3ad81c12332
Files: 8
Total size: 13.3 KB
Directory structure:
gitextract_ris6vh6k/
├── .gitignore
├── .jshintrc
├── .travis.yml
├── LICENSE.md
├── Makefile
├── README.md
├── lazyload.js
└── package.json
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
yarn-error.log
yarn.lock
node_modules
================================================
FILE: .jshintrc
================================================
{
"esversion": 6,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"immed": true,
"noarg": true,
"quotmark": "double",
"trailing": true,
"undef": true,
"unused": "vars",
"node": true,
"jquery": true,
"browser": true,
"predef": [
"define",
"IntersectionObserver"
]
}
================================================
FILE: .travis.yml
================================================
sudo: false
language: node_js
node_js:
- "7"
install:
- npm install
script:
- make travis
================================================
FILE: LICENSE.md
================================================
The MIT License (MIT)
=====================
Copyright (c) 2007-2019 Mika Tuupola
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
================================================
FILE: Makefile
================================================
.DEFAULT_GOAL := help
help:
@echo ""
@echo "Available tasks:"
@echo " lint Run linter and code style checker"
@echo " unit Run unit tests and generate coverage"
@echo " test Run linter and unit tests"
@echo " watch Run linter and unit tests when any of the source files change"
@echo " deps Install dependencies"
@echo " build Build minified version"
@echo " all Install dependencies and run linter and unit tests"
@echo ""
deps:
yarn install
lint:
node_modules/.bin/jshint lazyload.js
unit:
@echo "No unit tests."
watch:
find . -name "*.js" -not -path "./node_modules/*" -o -name "*.json" -not -path "./node_modules/*" | entr -c make test
test: lint unit
travis: lint unit
build:
node_modules/.bin/uglifyjs lazyload.js --compress --mangle --source-map --output lazyload.min.js
sed -i "1s/^/\/*! Lazy Load 2.0.0-rc.2 - MIT license - Copyright 2007-2019 Mika Tuupola *\/\n/" lazyload.min.js
all: deps test build
.PHONY: help deps lint test watch build all
================================================
FILE: README.md
================================================
# Lazy Load Remastered
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.
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.
## Basic Usage
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.
```html
<script src="https://cdn.jsdelivr.net/npm/lazyload@2.0.0-rc.2/lazyload.js"></script>
<img class="lazyload" data-src="img/example.jpg" width="765" height="574" />
<img class="lazyload" src="img/example-thumb.jpg" data-src="img/example.jpg" width="765" height="574" />
```
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`.
```js
lazyload();
```
If you prefer you can explicitly pass the image elements to the factory. Use this for example if you use different class name.
```js
let images = document.querySelectorAll(".branwdo");
lazyload(images);
```
If you prefer you can also use the plain old constructor.
```js
let images = document.querySelectorAll(".branwdo");
new LazyLoad(images);
```
The core IntersectionObserver can be configured by passing an additional argument
```js
new LazyLoad(images, {
root: null,
rootMargin: "0px",
threshold: 0
});
```
## Additional API
To use the additional API you need to assign the plugin instance to a variable.
```js
let lazy = lazyload();
```
To force loading of all images use `loadimages()`.
```js
lazy->loadImages();
```
To destroy the plugin and stop lazyloading use `destroy()`.
```js
lazy->destroy();
```
Note that `destroy()` does not load the out of viewport images. If you also
want to load the images use `loadAndDestroy()`.
```js
lazy->loadAndDestroy();
```
Additional API is not avalaible with the jQuery wrapper.
## jQuery Wrapper
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.
```html
<img class="lazyload" data-original="img/example.jpg" width="765" height="574">
<img class="lazyload" src="img/example-thumb.jpg" data-original="img/example.jpg" width="765" height="574">
```
```js
$("img.lazyload").lazyload();
```
## Cookbook
### Blur Up Images
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.
```html
<img class="lazyload"
src="thumbnail.jpg"
data-src="original.jpg"
width="1024" height="768" />
```
```html
<div class="lazyload"
style="background-image: url('thumbnail.jpg')"
data-src="original.jpg" />
```
### Responsive Images
Lazyloaded [Responsive images](https://www.smashingmagazine.com/2014/05/responsive-images-done-right-guide-picture-srcset/) are supported via `data-srcset`. If browser does not support `srcset` and there is no polyfill the image from `data-src` will be shown.
```html
<img class="lazyload"
src="thumbnail.jpg"
data-src="large.jpg"
data-srcset="small.jpg 480w, medium.jpg 640w, large.jpg 1024w"
width="1024" height="768" />
```
```html
<img class="lazyload"
src="thumbnail.jpg"
data-src="normal.jpg"
data-srcset="normal.jpg 1x, retina.jpg 2x"
width="1024" height="768" />
```
### Inlined Placeholder Image
To reduce the amount of request you can use data uri images as the placeholder.
```html
<img src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs="
data-src="original.jpg"
width="1024" height="768" />
```
## Install
This is still work in progress. You can install beta version with yarn or npm.
```
$ yarn add lazyload
$ npm install lazyload
```
# License
All code licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php). All images licensed under [Creative Commons Attribution 3.0 Unported License](http://creativecommons.org/licenses/by/3.0/deed.en_US). In other words you are basically free to do whatever you want. Just don't remove my name from the source.
================================================
FILE: lazyload.js
================================================
/*!
* Lazy Load - JavaScript plugin for lazy loading images
*
* Copyright (c) 2007-2019 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* https://appelsiini.net/projects/lazyload
*
* Version: 2.0.0-rc.2
*
*/
(function (root, factory) {
if (typeof exports === "object") {
module.exports = factory(root);
} else if (typeof define === "function" && define.amd) {
define([], factory);
} else {
root.LazyLoad = factory(root);
}
}) (typeof global !== "undefined" ? global : this.window || this.global, function (root) {
"use strict";
if (typeof define === "function" && define.amd){
root = window;
}
const defaults = {
src: "data-src",
srcset: "data-srcset",
selector: ".lazyload",
root: null,
rootMargin: "0px",
threshold: 0
};
/**
* Merge two or more objects. Returns a new object.
* @private
* @param {Boolean} deep If true, do a deep (or recursive) merge [optional]
* @param {Object} objects The objects to merge together
* @returns {Object} Merged values of defaults and options
*/
const extend = function () {
let extended = {};
let deep = false;
let i = 0;
let length = arguments.length;
/* Check if a deep merge */
if (Object.prototype.toString.call(arguments[0]) === "[object Boolean]") {
deep = arguments[0];
i++;
}
/* Merge the object into the extended object */
let merge = function (obj) {
for (let prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
/* If deep merge and property is an object, merge properties */
if (deep && Object.prototype.toString.call(obj[prop]) === "[object Object]") {
extended[prop] = extend(true, extended[prop], obj[prop]);
} else {
extended[prop] = obj[prop];
}
}
}
};
/* Loop through each object and conduct a merge */
for (; i < length; i++) {
let obj = arguments[i];
merge(obj);
}
return extended;
};
function LazyLoad(images, options) {
this.settings = extend(defaults, options || {});
this.images = images || document.querySelectorAll(this.settings.selector);
this.observer = null;
this.init();
}
LazyLoad.prototype = {
init: function() {
/* Without observers load everything and bail out early. */
if (!root.IntersectionObserver) {
this.loadImages();
return;
}
let self = this;
let observerConfig = {
root: this.settings.root,
rootMargin: this.settings.rootMargin,
threshold: [this.settings.threshold]
};
this.observer = new IntersectionObserver(function(entries) {
Array.prototype.forEach.call(entries, function (entry) {
if (entry.isIntersecting) {
self.observer.unobserve(entry.target);
let src = entry.target.getAttribute(self.settings.src);
let srcset = entry.target.getAttribute(self.settings.srcset);
if ("img" === entry.target.tagName.toLowerCase()) {
if (src) {
entry.target.src = src;
}
if (srcset) {
entry.target.srcset = srcset;
}
} else {
entry.target.style.backgroundImage = "url(" + src + ")";
}
}
});
}, observerConfig);
Array.prototype.forEach.call(this.images, function (image) {
self.observer.observe(image);
});
},
loadAndDestroy: function () {
if (!this.settings) { return; }
this.loadImages();
this.destroy();
},
loadImages: function () {
if (!this.settings) { return; }
let self = this;
Array.prototype.forEach.call(this.images, function (image) {
let src = image.getAttribute(self.settings.src);
let srcset = image.getAttribute(self.settings.srcset);
if ("img" === image.tagName.toLowerCase()) {
if (src) {
image.src = src;
}
if (srcset) {
image.srcset = srcset;
}
} else {
image.style.backgroundImage = "url('" + src + "')";
}
});
},
destroy: function () {
if (!this.settings) { return; }
this.observer.disconnect();
this.settings = null;
}
};
root.lazyload = function(images, options) {
return new LazyLoad(images, options);
};
if (root.jQuery) {
const $ = root.jQuery;
$.fn.lazyload = function (options) {
options = options || {};
options.attribute = options.attribute || "data-src";
new LazyLoad($.makeArray(this), options);
return this;
};
}
return LazyLoad;
});
================================================
FILE: package.json
================================================
{
"name": "lazyload",
"version": "2.0.0-rc.2",
"description": "Vanilla JavaScript plugin for lazyloading images",
"main": "lazyload.js",
"repository": "https://github.com/tuupola/lazyload.git",
"author": "Mika Tuupola <tuupola@appelsiini.net>",
"license": "MIT",
"keywords": [
"lazyload"
],
"files": [
"lazyload.js",
"lazyload.min.js"
],
"devDependencies": {
"jshint": "^2.9.5",
"uglify-es": "^3.0.28"
}
}
gitextract_ris6vh6k/ ├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE.md ├── Makefile ├── README.md ├── lazyload.js └── package.json
SYMBOL INDEX (1 symbols across 1 files)
FILE: lazyload.js
function LazyLoad (line 84) | function LazyLoad(images, options) {
Condensed preview — 8 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (15K chars).
[
{
"path": ".gitignore",
"chars": 37,
"preview": "yarn-error.log\nyarn.lock\nnode_modules"
},
{
"path": ".jshintrc",
"chars": 341,
"preview": "{\n \"esversion\": 6,\n \"curly\": true,\n \"eqeqeq\": true,\n \"eqnull\": true,\n \"immed\": true,\n \"noarg\": true,\n "
},
{
"path": ".travis.yml",
"chars": 100,
"preview": "sudo: false\n\nlanguage: node_js\n\nnode_js:\n - \"7\"\n\ninstall:\n - npm install\n\nscript:\n - make travis\n"
},
{
"path": "LICENSE.md",
"chars": 1106,
"preview": "The MIT License (MIT)\n=====================\n\nCopyright (c) 2007-2019 Mika Tuupola\n\nPermission is hereby granted, free of"
},
{
"path": "Makefile",
"chars": 1015,
"preview": ".DEFAULT_GOAL := help\n\nhelp:\n\t@echo \"\"\n\t@echo \"Available tasks:\"\n\t@echo \" lint Run linter and code style checker\"\n\t"
},
{
"path": "README.md",
"chars": 4874,
"preview": "# Lazy Load Remastered\n\nLazy Load delays loading of images in long web pages. Images outside of viewport will not be loa"
},
{
"path": "lazyload.js",
"chars": 5674,
"preview": "/*!\n * Lazy Load - JavaScript plugin for lazy loading images\n *\n * Copyright (c) 2007-2019 Mika Tuupola\n *\n * Licensed u"
},
{
"path": "package.json",
"chars": 452,
"preview": "{\n \"name\": \"lazyload\",\n \"version\": \"2.0.0-rc.2\",\n \"description\": \"Vanilla JavaScript plugin for lazyloading images\",\n"
}
]
About this extraction
This page contains the full source code of the tuupola/lazyload GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 8 files (13.3 KB), approximately 3.4k tokens, and a symbol index with 1 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.