{"id":13580880,"url":"https://github.com/jesusbotella/ngx-lazy-load-images","last_synced_at":"2025-04-06T06:32:08.586Z","repository":{"id":19358473,"uuid":"86870207","full_name":"jesusbotella/ngx-lazy-load-images","owner":"jesusbotella","description":"Image lazy load library for Angular 2+","archived":false,"fork":false,"pushed_at":"2023-01-06T01:32:37.000Z","size":4104,"stargazers_count":80,"open_issues_count":22,"forks_count":19,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-24T12:05:02.732Z","etag":null,"topics":["angular","angular10","angular11","angular2","angular4","angular9","angularuniversal","images","intersection-observer","intersectionobserver","lazy-load","lazy-loading","mutationobserver"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/jesusbotella.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-01T00:23:19.000Z","updated_at":"2024-02-18T10:06:40.000Z","dependencies_parsed_at":"2022-08-19T13:50:26.236Z","dependency_job_id":null,"html_url":"https://github.com/jesusbotella/ngx-lazy-load-images","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jesusbotella%2Fngx-lazy-load-images","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jesusbotella%2Fngx-lazy-load-images/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jesusbotella%2Fngx-lazy-load-images/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jesusbotella%2Fngx-lazy-load-images/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jesusbotella","download_url":"https://codeload.github.com/jesusbotella/ngx-lazy-load-images/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445649,"owners_count":20939952,"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":["angular","angular10","angular11","angular2","angular4","angular9","angularuniversal","images","intersection-observer","intersectionobserver","lazy-load","lazy-loading","mutationobserver"],"created_at":"2024-08-01T15:01:55.918Z","updated_at":"2025-04-06T06:32:08.315Z","avatar_url":"https://github.com/jesusbotella.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# ngx-lazy-load-images\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fjesusbotella%2Fngx-lazy-load-images.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fjesusbotella%2Fngx-lazy-load-images?ref=badge_shield)\n\n\nngx-lazy-load-images is a image lazy load library for Angular 9+.\n\nThe library allows to lazy load images from your web application using the [MutationObserver](https://developer.mozilla.org/en/docs/Web/API/MutationObserver) and the [IntersectionObserver](https://developer.mozilla.org/en/docs/Web/API/IntersectionObserver). Images will be loaded as soon as they enter the viewport in a non-blocking way.\n\nIt supports `\u003cimg\u003e` tags as well as background images.\n\nPlease check v1.x.x releases for support to older versions of Angular. ngx-lazy-load-images v2+ just supports Angular 9+.\n\n## Installation\nYou can install the library via npm with this command:\n```\nnpm install ngx-lazy-load-images --save\n```\n\n\n## Usage\n\n### 1. Import the `LazyLoadImagesModule`\n\nImport `LazyLoadImagesModule` from the `ngx-lazy-load-images` package, and add it to the `NgModule` imports array of your component.\n\n```typescript\nimport { NgModule } from '@angular/core';\nimport { LazyLoadImagesModule } from 'ngx-lazy-load-images';\n\n@NgModule({\n  imports: [\n    LazyLoadImagesModule\n  ]\n})\nexport class AppComponent {}\n```\n\n### 2. Use the `lazy-load-images` directive\n\nAdd the `lazy-load-images` directive to the tag containing all the DOM image nodes to lazy load:\n\n```html\n\u003c!-- Image tags --\u003e\n\u003cdiv class=\"image-list\" lazy-load-images\u003e\n  \u003cimg *ngFor=\"let imageUrl in images\" [attr.data-src]=\"imageUrl\"\u003e\n\u003c/div\u003e\n\n\u003c!-- Background images --\u003e\n\u003cdiv class=\"image-list\" lazy-load-images\u003e\n  \u003cdiv *ngFor=\"let imageUrl in images\" [attr.data-background-src]=\"imageUrl\"\u003e\u003c/div\u003e\n\u003c/div\u003e\n```\n\nThe container can have any HTML or components inside along with the images. But the less nodes inside the directive, the faster it will be.\n\n**Intersection Observer Configuration**\n\nIf you want to make some configuration changes to the Intersection Observer, you can do it by passing an object to the `lazy-load-images` directive. The object takes the [Intersection Observer configuration parameters](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_options) as keys.\n\n  ```html\n  \u003cdiv class=\"image-list\" [lazy-load-images]=\"{ rootMargin: '50px' }\"\u003e\u003c/div\u003e\n  ```\n\n### 3. Set the `data-src` or `data-background-src` attribute\n\nSet the `data-src` or `data-background-src` attribute in the images or tags that you want to lazy load.\n\nThe `data-src` attribute allows to lazy load images inside `\u003cimg\u003e` tags. You can define it by passing a variable to the attribute, interpolating a variable, or inserting the URL directly:\n\n```html\n\u003cimg [attr.data-src]=\"imageUrlVariable\"\u003e\n\u003cimg attr.data-src=\"{{ imageUrlVariable }}\"\u003e\n\u003cimg data-src=\"https://example.com/cute_kitten.jpg\"\u003e\n```\n\nThe `data-background-src` attribute lazy loads background images inside any HTML tag. It works the same way as the `data-src` attribute:\n\n```html\n\u003cdiv [attr.data-background-src]=\"imageUrlVariable\"\u003e\u003c/div\u003e\n\u003cdiv attr.data-background-src=\"{{ imageUrlVariable }}\"\u003e\u003c/div\u003e\n\u003cdiv data-background-src=\"https://example.com/cute_kitten.jpg\"\u003e\u003c/div\u003e\n```\n\n### 4. Have fun  🎉\n\n## Performance\n\n- Will this module make my web application worse in terms of performance?\n  - This module will make your app better in terms of overall and render performance, especially if it has a lot of images to show when the page is loaded. The images will be downloaded as soon as they appear in the viewport instead of all together. That means that your users will save network transfer data and the scroll will be less laggy.\n\n- What happens if I have lots of images, will it consume too much memory?\n  - The lifespan of the image observers is too short. It will only last until [the URL is set to the proper attribute, and destroyed right after that](https://github.com/jesusbotella/ngx-lazy-load-images/blob/master/src/lib/src/ngx-lazy-load-images.directive.ts#L82). And if the directive is destroyed, [all the remaining image observers and the intersection observer instance will be disconnected](https://github.com/jesusbotella/ngx-lazy-load-images/blob/master/src/lib/src/ngx-lazy-load-images.directive.ts#L38).\n\n\n## Browser Compatibility\n\nThis library has been tested and known to work in the latest version of all major browsers.\n\nThe [WICG Intersection Observer Polyfill](https://github.com/WICG/IntersectionObserver/tree/gh-pages/polyfill) is bundled within the module to use the Intersection Observer API in the non-compatible browsers. Avoid importing it in your application too, as it may cause unexpected issues.\n\nHowever, the compatibility with older browsers is limited by the [`Mutation Observer` support](https://caniuse.com/#feat=mutationobserver), which is known to work in:\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003cimg src=\"https://raw.github.com/alrra/browser-logos/39.2.2/src/chrome/chrome_48x48.png\" alt=\"Chrome\"\u003e\u003cbr\u003e\n      26+\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003cimg src=\"https://raw.github.com/alrra/browser-logos/39.2.2/src/firefox/firefox_48x48.png\" alt=\"Firefox\"\u003e\u003cbr\u003e\n      14+\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003cimg src=\"https://raw.github.com/alrra/browser-logos/39.2.2/src/safari/safari_48x48.png\" alt=\"Safari\"\u003e\u003cbr\u003e\n      6+\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003cimg src=\"https://raw.github.com/alrra/browser-logos/39.2.2/src/edge/edge_48x48.png\" alt=\"Edge\"\u003e\u003cbr\u003e\n      ✔\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003cimg src=\"https://raw.github.com/alrra/browser-logos/39.2.2/src/archive/internet-explorer_7-8/internet-explorer_7-8_48x48.png\" alt=\"Internet Explorer\"\u003e\u003cbr\u003e\n    11+\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003cimg src=\"https://raw.github.com/alrra/browser-logos/39.2.2/src/opera/opera_48x48.png\" alt=\"Opera\"\u003e\u003cbr\u003e\n      15+\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003cimg src=\"https://raw.github.com/alrra/browser-logos/39.2.2/src/android/android_48x48.png\" alt=\"Android\"\u003e\u003cbr\u003e\n      4.4+\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n## License\n\nMIT\n\n\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fjesusbotella%2Fngx-lazy-load-images.svg?type=large)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fjesusbotella%2Fngx-lazy-load-images?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjesusbotella%2Fngx-lazy-load-images","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjesusbotella%2Fngx-lazy-load-images","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjesusbotella%2Fngx-lazy-load-images/lists"}