{"id":18657195,"url":"https://github.com/reecelucas/lazily","last_synced_at":"2025-11-05T21:30:30.154Z","repository":{"id":32555958,"uuid":"136915848","full_name":"reecelucas/lazily","owner":"reecelucas","description":"Lightweight image lazy-loader using the intersection observer API","archived":false,"fork":false,"pushed_at":"2022-12-07T19:33:38.000Z","size":562,"stargazers_count":1,"open_issues_count":10,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-28T04:35:05.326Z","etag":null,"topics":["es2015","intersection","intersection-observer","javascript-library","lazy-loading","lazyload","observer","performance"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/reecelucas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-11T11:17:11.000Z","updated_at":"2020-06-04T02:58:37.000Z","dependencies_parsed_at":"2023-01-14T21:34:57.247Z","dependency_job_id":null,"html_url":"https://github.com/reecelucas/lazily","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reecelucas%2Flazily","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reecelucas%2Flazily/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reecelucas%2Flazily/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reecelucas%2Flazily/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reecelucas","download_url":"https://codeload.github.com/reecelucas/lazily/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239474404,"owners_count":19644915,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["es2015","intersection","intersection-observer","javascript-library","lazy-loading","lazyload","observer","performance"],"created_at":"2024-11-07T07:27:03.543Z","updated_at":"2025-11-05T21:30:30.106Z","avatar_url":"https://github.com/reecelucas.png","language":"JavaScript","readme":"# Lazily.js\n\nA lightweight (1.8kB min, 950b min + gzip) image lazy-loading utility using the [Intersection Observer API](https://developer.mozillaorg/en-US/docs/Web/API/Intersection_Observer_API).\n\n## Browser Support\n\nFor Intersection Observer API support please refer to [canIuse](https://caniuse.com/#feat=intersectionobserver). In unsupporting browsers it's necessary to load a [polyfill](https://github.com/w3c/IntersectionObserver/tree/master/polyfill) in order to observe images on scroll. In the absence of native or polyfilled support, all images are loaded immediately.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/lang/en/):\n\nnpm:\n```Bash\nnpm install lazily --save\n```\n\nyarn:\n```Bash\nyarn add lazily\n```\n\n## Example Usage\n\n```html\n\u003c!-- Simple image --\u003e\n\u003cimg\n    class=\"js-lazily-image\"\n    src=\"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\"\n    data-src=\"my-image.jpg\"\n    alt=\"Example alt text\"\n\u003e\n\n\u003c!-- Responsive image --\u003e\n\u003cimg\n    class=\"js-lazily-image\"\n    alt=\"Example alt text\"\n    src=\"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\"\n    sizes=\"(max-width: 460px) 100vw,\n           (max-width: 1024px) 100vw,\n           (min-width: 1025px) 1200px\"\n    data-src=\"my-image.png\"\n    data-srcset=\"my-image--mobile.jpg 460w,\n                 my-image--tablet.jpg 768w,\n                 my-image--desktop.jpg 1600w\"\n\u003e\n```\n\n```JavaScript\nimport lazily from 'lazily.js';\n\n/**\n * Call the `lazily` factory function to return a `lazily`\n * object exposing three methods:\n *\n * 1. init\n * 2. destroy\n * 3. update\n */\nconst lazyLoad = lazily();\n\n// 1. Begin watching images on scroll.\nlazyLoad.init();\n\n/**\n * 2. Stop watching images on scroll, and destroy the Intersection Observer instance\n * created by calling the `init` method.\n */\nlazyLoad.destroy();\n\n/**\n * 3. Update the array of images that will be observed.\n * This method is useful if you need to respond to DOM insertions after fetching\n * data asynchronously.\n */\nfetchImagesAndUpdateDOM().then(() =\u003e {\n    lazyLoad.update();\n});\n\n```\n\n## Configuration\n\n```JavaScript\nconst lazyLoad = lazily({\n    selector: '.my-image-class',\n    loadClass: 'my-load-class',\n    errorClass: 'my-error-class',\n    loadCallback: () =\u003e console.log('image has been loaded!'),\n    errorCallback: () =\u003e console.log('something went wrong!'),\n    rootId: 'my-root-container-id',\n    rootMargin: '100px 100px 100px 100px',\n    threshold: 0.5\n});\n```\n\n### Options:\n\n\n| Option  | Type  | Default  | Description\n| ------- | ----- | -------- | ------------\n| `selector`      | String   |`'.js-lazily-image'`| Selector used to retrieve images. Any valid selector that can be passed to `querySelectorAll` can be used.\n| `loadClass`     | String   | `'has-loaded'` | Class to be applied to each image once loaded.\n| `errorClass`    | String   | `'has-error'` | Class to be applied to each image on error.\n| `loadCallback`  | Function | `null` | Callback function to be executed on each image's `load` event.\n| `errorCallback` | Function | `null` | Callback function to be executed on each image's `error` event.\n| `rootId`      | String   | `null` | `Id` of the `root` container. Defaults to the browser viewport if not specified. (see Intersection Observer API [docs](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) for details).\n| `rootMargin`    | String   | `'0px 0px 0px 0px'` | Margin around the root container. Can have values similar to the CSS margin property. E.g. `\"10px 20px 30px 40px\"`. If the `rootId` is specified the values can be percentages. (see Intersection Observer API [docs](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) for details).\n| `threshold`     | Number   | `0` | A single number which indicates at what percentage of the images's visibility the observer's callback should be executed. E.g. If you want to detect when visibility passes the 50% mark (half of the image is visible within the root element), you can use a value of `0.5`. (see Intersection Observer API [docs](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) for details).\n\n## License\n\nLicensed under the MIT License, Copyright © 2019 Reece Lucas.\n\nSee [LICENSE](./LICENSE) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freecelucas%2Flazily","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freecelucas%2Flazily","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freecelucas%2Flazily/lists"}