• Stars
    star
    942
  • Rank 48,525 (Top 1.0 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 13 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

🚅 Lazyload images, iframes, widgets with a standalone JavaScript lazyloader

NOTE FROM MAINTAINERS This library is still working but not actively maintained, we recommend you use https://github.com/ApoorvSaxena/lozad.js first rather than this one. /NOTE FROM MAINTAINERS

lazyload Dependency Status devDependency Status

Lazyload images, iframes or any src* element until they are visible in the viewport.

Selenium Test Status

Install

npm install lazyloadjs --save

Also available for <script> users:

Simple example

See more examples.

<!DOCTYPE html>
<html>
  <head>
    <title>lazyload</title>
  </head>
  <body>
    <script src="lazyload.min.js"></script>

    <!-- A lot of content -->
    <!-- A lot of content -->

    <img
      data-src="real/image/src.jpg"
      src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
      onload="lzld(this)">
  </body>
</html>

Make sure your webpage is in standards mode.

Viewport computing is badly handled by browsers when in quirksmode.

If you do not want to use a data-uri as your src, you can also use the provided b.gif which is the tiniest gif ever.

On most websites, you better let the first top images not bound to lzld method. So that they shows really fast.

Advanced example

lazyloadjs is an npm module and is compatible with browserify.

global.myLazyload = require('lazyloadjs')();
<img
  data-src="real/image/src.jpg"
  src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
  onload="myLazyload(this)">

Per default, lazyloadjs exposes the lzld instance on the global object. So that in most cases, you just need to require it in your webpage.

API

var lzld = lazyload([opts])

opts is an object with these defaults:

{
  container: document.body,
  offset: 333,
  src: 'data-src' // or function(elt) { return customSrc }
}

opts.container is the referencing container, it's the viewport, defaults to document.body

opts.offset is a length in pixels used to compute when an element will soon be visible. So that you load it just before it becomes visible.

src is the attribute name storing the real src of the element to load.

src can also be a function, so that you can have your custom src computing algorithm. You can use it to lazyload High DPI/retina images.

Launching the examples

npm run examples

Developing

Launch the dev server:

npm run dev

Browse to http://localhost:8080/__zuul.

Tests are written with mocha.

Building

We provide a pre-built version of lazyloadjs in build/lazyload.min.js.

But you can build your own:

npm run build

You get the build in build/lazyload.min.js.

Please consider using browserify.

Sites using lazyload

Tens of millions of pageviews are served each month using this project:

.. And many unlisted websites, add yours!

Licence

Also see LICENCE.fasterize

(The MIT Licence)

Copyright (c) Vincent Voyer

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.

More Repositories

1

gifify

😻 Convert any video file to an optimized animated GIF.
JavaScript
6,088
star
2

iron-session

🛠 Secure, stateless, and cookie-based session library for JavaScript
TypeScript
3,619
star
3

tzdb

🕰 Simplified, grouped and always up to date list of time zones, with major cities
JavaScript
629
star
4

in-viewport

Get a callback when any element becomes visible in a viewport (body or custom viewport)
JavaScript
280
star
5

sourcekarma

Discover how people react to you on GitHub 👍
JavaScript
101
star
6

javascript-library-template

JavaScript library template to focus on ⌨️ coding, 🙌 collaborating and 🚀 shipping
JavaScript
80
star
7

npm-pkgr

Cache `npm install` results by hashing dependencies
JavaScript
73
star
8

nextjs-vercel-aws-cdk-example

Example Next.js project deployed on Vercel with AWS services for database, cron jobs and asynchronous jobs
JavaScript
70
star
9

docker-selenium-firefox-chrome-beta

A Dockerfile starting a selenium standalone server with Chrome and Firefox beta
Shell
67
star
10

iron-store

🧿 in-memory, signed and encrypted JavaScript store
JavaScript
65
star
11

aws-lambda-nodejs-webpack

λ CDK Construct to build Node.js AWS lambdas using webpack
TypeScript
31
star
12

analytics.js-loader

Asynchronously load segment.com analytics.js with an npm module
JavaScript
29
star
13

concat-files

[DEPRECATED] Concatenate files asynchronously with node.js
JavaScript
26
star
14

zorgs

🦑 Command line tool displaying GitHub organizations stats
JavaScript
18
star
15

ansible-archee

dotfiles on steroids
Python
17
star
16

localenvify

localenv and envify combined as a browserify transform
JavaScript
14
star
17

chainit

Turn an asynchronous JavaScript api into an asynchronous chainable JavaScript api
JavaScript
12
star
18

selenium-runner

Run a [url/JSTest, ..] combo in selenium grid, in parallel
JavaScript
10
star
19

mocha-browse

Launch headless browsers on any mocha tests webpage, get results in console
JavaScript
9
star
20

node-whereis

Simply get the first path to a bin on any system
JavaScript
7
star
21

typescript-library-template

TypeScript library template to focus on ⌨️ coding, 🙌 collaborating and 🚀 shipping
TypeScript
7
star
22

project-name

description
JavaScript
5
star
23

tokenify

🎰 Interactive command line tool to easily get refresh tokens for Spotify API
JavaScript
5
star
24

coronamaison

🎨 All the drawings from the #coronamaison hashtag on Twitter
JavaScript
5
star
25

almanak

WIP Google Calendar like component for React
TypeScript
5
star
26

azure-sb-queue-watcher

Job worker around Azure Service Bus Queues
JavaScript
4
star
27

forkie

Forkie is a graceful process manager for Node.js: start/stop/working?
JavaScript
4
star
28

knex-typescript-migrations-esm

TypeScript
3
star
29

offline-docs

Always get some documentation on the modules you use, even offline
CSS
3
star
30

b64-bench

JavaScript
2
star
31

kcnb1-france.org

Repository for Association KCNB1 France website
PHP
2
star
32

www.berlisco.com

CTRL+W
JavaScript
2
star
33

cron-example

CSS
2
star
34

aws-lambda-nodejs-performance

Example of performance issue of aws-lambda-nodejs
JavaScript
2
star
35

vercel-test-workspaces

JavaScript
1
star
36

test-post-body

JavaScript
1
star
37

test-favicon

Vue
1
star
38

example-empty-project

1
star
39

vvo

1
star
40

npm-test-pre-push

Install a git pre-push hook launching `npm test`
JavaScript
1
star
41

test-babel-node-configuration-file

JavaScript
1
star
42

go-ghoauth

Two steps process for github access_token creation
Go
1
star
43

test-next-broken-packagelock

JavaScript
1
star
44

deploy-summary-test

CSS
1
star
45

emoshort

Emoji to short name converter
JavaScript
1
star
46

nextjs-jobs

JavaScript
1
star
47

express-minimal-app

basic express app, production ready
JavaScript
1
star
48

knex-pool-require-cache-issue

Demo of knex pool issue when combined with delete require.cache
JavaScript
1
star
49

swc-maximum-call-stack-error

TypeScript
1
star
50

test-nextjs-routes-bug-locales

JavaScript
1
star