{"id":13481733,"url":"https://github.com/tjoskar/ng-lazyload-image","last_synced_at":"2025-05-15T04:08:18.886Z","repository":{"id":4169338,"uuid":"52146170","full_name":"tjoskar/ng-lazyload-image","owner":"tjoskar","description":"🖼 A small library for lazy loading images for Angular apps with zero dependencies ","archived":false,"fork":false,"pushed_at":"2023-03-04T02:27:35.000Z","size":4526,"stargazers_count":762,"open_issues_count":35,"forks_count":142,"subscribers_count":26,"default_branch":"main","last_synced_at":"2025-05-15T04:08:12.575Z","etag":null,"topics":["angular","lazy-loading"],"latest_commit_sha":null,"homepage":"https://naughty-bose-ec1cfc.netlify.com","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/tjoskar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-02-20T09:30:48.000Z","updated_at":"2025-04-04T05:56:25.000Z","dependencies_parsed_at":"2023-07-05T17:00:54.130Z","dependency_job_id":null,"html_url":"https://github.com/tjoskar/ng-lazyload-image","commit_stats":{"total_commits":445,"total_committers":29,"mean_commits":"15.344827586206897","dds":0.3101123595505618,"last_synced_commit":"af6a423a5bdf0a9bf4e25646be477987101fde4b"},"previous_names":["tjoskar/ng2-lazyload-image"],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjoskar%2Fng-lazyload-image","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjoskar%2Fng-lazyload-image/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjoskar%2Fng-lazyload-image/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjoskar%2Fng-lazyload-image/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tjoskar","download_url":"https://codeload.github.com/tjoskar/ng-lazyload-image/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254270656,"owners_count":22042860,"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","lazy-loading"],"created_at":"2024-07-31T17:00:54.895Z","updated_at":"2025-05-15T04:08:13.856Z","avatar_url":"https://github.com/tjoskar.png","language":"TypeScript","funding_links":[],"categories":["Uncategorized","TypeScript","UI"],"sub_categories":["Uncategorized","Images"],"readme":"\u003ch3 align=\"center\"\u003eng-lazyload-image\u003c/h3\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n[![Status](https://img.shields.io/badge/status-active-success.svg)]()\n![npm](https://img.shields.io/npm/dw/ng-lazyload-image.svg)\n[![npm version](https://badge.fury.io/js/ng-lazyload-image.svg)](https://badge.fury.io/js/ng-lazyload-image)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](/LICENSE)\n![Build Status](https://github.com/tjoskar/ng-lazyload-image/workflows/Node%20CI/badge.svg)\n\n\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\n  A super small libary for lazy loading images for Angular apps with zero dependencies\n\u003c/p\u003e\n\n## 📝 Table of Contents\n\n- [Demo](#demo)\n- [Prerequisites](#prerequisites)\n- [Install](#install)\n- [Setup](#libsetup)\n- [Usages](#usages)\n- [Debug](#debug)\n- [CSS](#css)\n- [API](#api)\n- [Hooks](#hooks)\n- [Search Engine Optimization (SEO)](#seo)\n- [FAQ](#faq)\n- [Contributing](#contributing)\n\n## 🤯 Demo \u003ca name = \"demo\"\u003e\u003c/a\u003e\n\nVisit this site: https://naughty-bose-ec1cfc.netlify.com\n\n## ✅ Prerequisites \u003ca name = \"prerequisites\"\u003e\u003c/a\u003e\n\nThe browser you are targeting need to have support of `WeakMap` and `String.prototype.includes`. If you need to support an older browser (like IE) you will need to include polyfill for `WeakMap` and `String.prototype.includes` (see https://github.com/zloirock/core-js for example).\n\nMake also sure to inclue a pollyfill for [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) if you need to target IE and want to use IntersectionObserver: https://github.com/w3c/IntersectionObserver/tree/master/polyfill\n\n## ⬇️ Install \u003ca name = \"install\"\u003e\u003c/a\u003e\n\nTo install the package, just run:\n\n```\n$ npm install ng-lazyload-image\n```\n\nor the following if you are using yarn\n\n```\n$ yarn add ng-lazyload-image\n```\n\n## 🛠 Setup \u003ca name = \"libsetup\"\u003e\u003c/a\u003e\n\nInclude the library in your module (see [app.module.ts](https://github.com/tjoskar/ng-lazyload-image/blob/master/example/src/app/app.module.ts)):\n\n```javascript\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { LazyLoadImageModule } from 'ng-lazyload-image'; // \u003c-- import it\nimport { AppComponent } from './app.component';\n\n@NgModule({\n  declarations: [AppComponent],\n  imports: [BrowserModule, LazyLoadImageModule], // \u003c-- and include it\n  bootstrap: [AppComponent],\n})\nexport class MyAppModule {}\n```\n\n### IntersectionObserver\n\n`ng-lazyload-image` is using a intersection observer by default so you don't need to do anything if you want to continue using the intersection observer as event emitter.\n\n### Scroll listener\n\nYou can easily swtich from IntersectionObserver to scroll listener by using the `scrollHooks`:\n\n```javascript\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { LazyLoadImageModule, LAZYLOAD_IMAGE_HOOKS, ScrollHooks } from 'ng-lazyload-image'; // \u003c-- include ScrollHooks\nimport { AppComponent } from './app.component';\n\n@NgModule({\n  declarations: [AppComponent],\n  imports: [BrowserModule, LazyLoadImageModule],\n  providers: [{ provide: LAZYLOAD_IMAGE_HOOKS, useClass: ScrollHooks }], // \u003c-- Declare that you want to use ScrollHooks\n  bootstrap: [AppComponent],\n})\nexport class MyAppModule {}\n```\n\nSee [hooks](#hooks) below for more information.\n\n## 🖼 Usages \u003ca name = \"usages\"\u003e\u003c/a\u003e\n\nA simple usecase is to use a `img`-tag and give it the image to lazyload to `[lazyLoad]` and an optional default image to `[defaultImage]`. The default image will be shown while the \"real\" image is getting loaded.\n\nExample:\n\n```javascript\nimport { Component } from '@angular/core';\n\n@Component({\n  selector: 'image',\n  template: ` \u003cimg [defaultImage]=\"defaultImage\" [lazyLoad]=\"image\" /\u003e `,\n})\nclass ImageComponent {\n  defaultImage = 'https://www.placecage.com/1000/1000';\n  image = 'https://images.unsplash.com/photo-1443890923422-7819ed4101c0?fm=jpg';\n}\n```\n\n### Background images\n\nIt also supports background images:\n\n```javascript\n@Component({\n  selector: 'image',\n  template: ` \u003cdiv [defaultImage]=\"defaultImage\" [lazyLoad]=\"image\"\u003e\u003c/div\u003e `,\n})\nclass ImageComponent {\n  defaultImage = 'https://www.placecage.com/1000/1000';\n  image = 'https://images.unsplash.com/photo-1443890923422-7819ed4101c0?fm=jpg';\n}\n```\n\n### Responsive images\n\nIf using responsive images in a plain `\u003cimg\u003e` tag, you'll need to set the `useSrcset` attribute to `true`:\n\n```javascript\n@Component({\n  selector: 'image',\n  template: ` \u003cimg [defaultImage]=\"defaultImage\" [lazyLoad]=\"images\" [useSrcset]=\"true\" /\u003e `,\n})\nclass ImageComponent {\n  defaultImage = 'https://www.placecage.com/1000/1000';\n  images = `https://images.unsplash.com/photo-1434725039720-aaad6dd32dfe?fm=jpg 700w,\n            https://images.unsplash.com/photo-1437818628339-19ded67ade8e?fm=jpg 1100w`;\n}\n```\n\nIf using responsive images in a `\u003cpicture\u003e` tag, set the default `\u003cimg\u003e` tag as usual with `lazyLoad` etc. attributes.  \nYou can use `attr.lazyLoad`, `attr.defaultImage` and `attr.errorImage` attributes for `\u003csource\u003e` elements.  \nThere's no need to set `useSrcset` for `\u003csource\u003e` elements, as `srcset` is used by default.  \nA simple example for a `\u003cpicture\u003e` tag:\n\n```javascript\n@Component({\n  selector: 'image',\n  template: `\n    \u003cpicture\u003e\n      \u003csource media=\"(min-width: {{ screen_lg }})\" [attr.defaultImage]=\"defaultImage\" [attr.lazyLoad]=\"image2\" /\u003e\n      \u003csource media=\"(min-width: {{ screen_md }})\" [attr.defaultImage]=\"defaultImage\" [attr.lazyLoad]=\"image3\" /\u003e\n      \u003cimg [defaultImage]=\"defaultImage\" [lazyLoad]=\"image1\" /\u003e\n    \u003c/picture\u003e\n  `,\n})\nclass ImageComponent {\n  screen_lg = '1200px';\n  screen_md = '992px';\n  defaultImage = 'https://www.placecage.com/1000/1000';\n  image1 = 'https://images.unsplash.com/photo-1422004707501-e8dad229e17a?fm=jpg';\n  image2 = 'https://images.unsplash.com/photo-1439931444800-9bcc83f804a6?fm=jpg';\n  image3 = 'https://images.unsplash.com/photo-1417128281290-30a42da46277?fm=jpg';\n}\n```\n\n### Loading image path async\n\nYou can load image async or change the url on the fly, eg.\n\n```html\n\u003cimg [lazyLoad]=\"image$ | async\" /\u003e\n```\n\n### Custom observable\n\nSometimes you want to get more controll over when the we should check if the image is in viewport. `customObservable` let's you create your own observable.\n\nThis will change the functionality of *when* we chek if the image is in the viewport. It does not change the functionality of *how* to detect if an image is in the viewport or not. Meaning: if you are using IntersectionObserver (default), it is important that the obserer that you pass to `customObservable` will emit objects that looks like: `{ isIntersecting: boolean }`. You can change this behavior by implementing your own `isVisible` (see [hooks](#hooks) below for more information).\n\nIf you are using the ScrollHooks-preset, you can just pass `customObservable` and the reset will be handle automatically.\n\n```ts\nimport { merge, fromEvent } from 'rxjs'\n\n...\n\nconstructor() {\n  this.scroll$ = merge(\n    fromEvent(window, 'scroll'),\n    fromEvent(someDivRef, 'scroll')\n  );\n}\n```\n\n```html\n\u003cimg [customObservable]=\"scroll$\" ... /\u003e\n```\n\n### Ionic\n\nIf you are using Ionic and **don't** want to use IntersectionObserver, you may need to include your own scroll observable or change the scroll target. For instans if you want to have multiple scroll targets:\n\n```javascript\n@Component({\n  selector: 'page-image',\n  template: `\n    \u003cion-content #container padding\u003e\n      \u003cimg [defaultImage]=\"https://www.placecage.com/1000/1000\" [lazyLoad]=\"lazyLoadImage\" [customObservable]=\"container.ionScroll\" /\u003e\n    \u003c/ion-content\u003e\n  `,\n})\nexport class AboutPage {\n  lazyLoadImage = 'https://hd.unsplash.com/photo-1431400445088-1750c997c6b5';\n}\n```\n\nIn case of using ion-slides in Ionic 2+, you can include your own scroll observable as below.\n\n```javascript\n@Component({\n  selector: 'page-image',\n  template: `\n    \u003cion-content #container padding\u003e\n      \u003cimg [defaultImage]=\"https:  //www.placecage.com/1000/1000\" [lazyLoad]=\"lazyLoadImage\" [customObservable]=\"container.ionSlideWillChange\" /\u003e\n    \u003c/ion-content\u003e\n  `,\n})\nexport class AboutPage {\n  lazyLoadImage = 'https://hd.unsplash.com/photo-1431400445088-1750c997c6b5';\n}\n```\n\n## 🐛 Debug \u003ca name = \"debug\"\u003e\u003c/a\u003e\n\nIn order to get a better understanding of what is happening you can pass `[debug]=\"true\"` which will output some debug information in the web console.\n\nSee [onStateChange](#onStateChange) for more information about the diffrent output messages.\n\n## 💅 CSS \u003ca name = \"css\"\u003e\u003c/a\u003e\n\nThe css class name `ng-lazyloading` will automatically be added before the image is loaded and will be removed when the image has been loaded or if the image couldn't be loaded.\n\nThe css class name `ng-lazyloaded` will be added when the image is loaded (regardless of if the image could be loaded or not).\n\nThe css class name `ng-failed-lazyloaded` will be added when the image couldn't be loaded.\n\n## 🔄 API \u003ca name = \"api\"\u003e\u003c/a\u003e\n\n##### lazyLoad\n\nType: `string`\n\nExample: `https://images.unsplash.com/photo-1443890923422-7819ed4101c0?fm=jpg`\n\nThe image to be lazy loaded. This image will replace the default image (`defaultImage`).\n\n```html\n\u003cimg [defaultImage]=\"'https://www.placecage.com/1000/1000'\" [lazyLoad]=\"'https://hd.unsplash.com/photo-1431400445088-1750c997c6b5'\" /\u003e\n```\n\n##### defaultImage (optional)\n\nType: `string`\n\nExample: `https://www.placecage.com/1000/1000`\n\nPath to default image. This image will be loaded right away.\n\nYou can also use `src` attribute for img tag to define default image:  \n`\u003cimg src=\"https://www.placecage.com/1000/1000\" [lazyLoad]=\"lazyLoadImage\" /\u003e`\n\nor `background-image` property for non-image tags:  \n`\u003cdiv style=\"background-image: url('https://www.placecage.com/1000/1000');\" [lazyLoad]=\"lazyLoadImage\"\u003e\u003c/div\u003e`\n\n##### errorImage (optional)\n\nType: `string`\n\nExample: `https://i.imgur.com/XkU4Ajf.png`\n\nAn image to be loaded if failing to load `lazyLoad`. Will load the default image (`defaultImage`) if absent.\n\n```html\n\u003cimg [defaultImage]=\"someDefaultImage\" [lazyLoad]=\"imageToLazyLoad\" [errorImage]=\"imageToShowOnError\" /\u003e\n```\n\n##### offset (optional)\n\nType: `number`\n\nExample: `100`\n\nDefault: `0`\n\nNumber of px a image should be loaded before it is in view port\n\n```html\n\u003cimg [defaultImage]=\"someDefaultImage\" [lazyLoad]=\"imageToLazyLoad\" [offset]=\"100\" /\u003e\n```\n\n##### scrollTarget (optional)\n\nType: `Element`\n\nExample: `document.getElementById('my-scroll-container')`\n\nDefault: `window`\n\nYou will need to set this property if you are using a scroll container and do not propagate the scroll event to window.\n\n##### customObservable (optional)\n\nType: `Observable`\n\nExample: `Observable.fromEvent(myScrollContainer, 'scroll')`\n\nYou can pass your own observable if you need more control over the flow. Can be useful if integrating with other frameworks like ionic. See [Custom Observable](#custom-observable) for more information.\n\n##### useSrcset (optional)\n\nType: `boolean`\n\nExample: `true`\n\nYou can set this to `true` if you need to lazy load images with `srcset` attribute, instead of `src`.  \n`\u003csource\u003e` tags are set to use `srcset` by default, so you don't need to set this option additionaly.\n\n##### decode (optional)\n\nType: `boolean`\n\nExample: `true`\n\nYou can set this to `true`, the image will be [decoded](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/decode) before inserted into the DOM. This can be useful for large images.\n\n##### debug (optional)\n\ntype: `boolean`\n\nExaple: `true`\n\nSee [debug](#debug) for more information.\n\n### Events\n\n##### onStateChange (optional) \u003ca name = \"onStateChange\"\u003e\u003c/a\u003e\n\nType: `Function: (event: StateChange) =\u003e void`\n\nExample: `\u003cimg [lazyLoad]=\"lazyLoadImage\" (onStateChange)=\"myCallbackFunction($event)\"\u003e`\n\nYou can pass a callback function, which will be called when the image is getting into different state.\n\n```ts\nmyCallbackFunction(event: StateChange) {\n  switch (event.reason) {\n    case 'setup':\n      // The lib has been instantiated but we have not done anything yet.\n      break;\n    case 'observer-emit':\n      // The image observer (intersection/scroll/custom observer) has emit a value so we\n      // should check if the image is in the viewport.\n      // `event.data` is the event in this case.\n      break;\n    case 'start-loading':\n      // The image is in the viewport so the image will start loading\n      break;\n    case 'mount-image':\n      // The image has been loaded successfully so lets put it into the DOM\n      break;\n    case 'loading-succeeded':\n      // The image has successfully been loaded and placed into the DOM\n      break;\n    case 'loading-failed':\n      // The image could not be loaded for some reason.\n      // `event.data` is the error in this case\n      break;\n    case 'finally':\n      // The last event before cleaning up\n      break;\n  }\n}\n```\n\n## 🎣 Hooks \u003ca name = \"hooks\"\u003e\u003c/a\u003e\n\nIt is possible to hook into the loading process by create your own functions.\n\nFor example, let's say you want to fetch an image with some custom headers. If so, you can create a custom hook to fetch the image:\n\n```ts\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { LazyLoadImageModule, IntersectionObserverHooks, Attributes, LAZYLOAD_IMAGE_HOOKS } from 'ng-lazyload-image';\nimport { AppComponent } from './app.component';\n\nexport class LazyLoadImageHooks extends IntersectionObserverHooks {\n  loadImage({ imagePath }: Attributes): Promise\u003cstring\u003e {\n    return fetch(imagePath, {\n      headers: {\n        Authorization: 'Bearer ...',\n      },\n    })\n      .then((res) =\u003e res.blob())\n      .then((blob) =\u003e URL.createObjectURL(blob));\n  }\n}\n\n@NgModule({\n  declarations: [AppComponent],\n  imports: [BrowserModule, LazyLoadImageModule],\n  providers: [{ provide: LAZYLOAD_IMAGE_HOOKS, useClass: LazyLoadImageHooks }],\n  bootstrap: [AppComponent],\n})\nexport class MyAppModule {}\n```\n\nYou can even use other services by inject them. Lets say you want to use Angulars https class instead of `window.fetch`:\n\n```ts\nimport { NgModule, Injectable } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { HttpClientModule, HttpClient } from '@angular/common/http';\nimport { LazyLoadImageModule, IntersectionObserverHooks, Attributes, LAZYLOAD_IMAGE_HOOKS } from 'ng-lazyload-image';\nimport { Observable } from 'rxjs';\nimport { map } from 'rxjs/operators';\nimport { AppComponent } from './app.component';\n\n@Injectable()\nexport class LazyLoadImageHooks extends IntersectionObserverHooks {\n  private http: HttpClient;\n\n  constructor(http: HttpClient) {\n    super();\n    this.http = http;\n  }\n\n  loadImage({ imagePath }: Attributes): Observable\u003cstring\u003e {\n    // Load the image through `HttpClient` and cancel the request if the user change page or the image gets removed\n    return this.http.get(imagePath, { responseType: 'blob' }).pipe(map(blob =\u003e URL.createObjectURL(blob)));\n  }\n}\n\n@NgModule({\n  declarations: [AppComponent],\n  imports: [BrowserModule, HttpClientModule, LazyLoadImageModule],\n  providers: [{ provide: LAZYLOAD_IMAGE_HOOKS, useClass: LazyLoadImageHooks }],\n  bootstrap: [AppComponent],\n})\nexport class MyAppModule {}\n```\n\nThe following hooks are supported:\n\n### getObservable\n\nShould return an observable that emits a new value every time `ng-lazyload-image` should check if the image is in viewport. It is important that the emited event is of the same type `isVisible` expects.\n\nEg.\n\n```ts\nimport { Attributes, IntersectionObserverHooks } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  getObservable(attributes: Attributes) {\n    // Will emit `{ isIntersecting: true }` every second.\n    // You will have to overwride `isVisible` if you want to pass another event\n    return interval(1000).pipe(mapTo({ isIntersecting: true })));\n  }\n}\n```\n\nA more usefull example could be to add a debounce time so we only start loading the image if it has been in the view port for some time:\n\n```ts\nimport { Attributes, IntersectionObserverHooks } from 'ng-lazyload-image';\nimport { debounceTime } from 'rxjs/operators';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  getObservable(attributes: Attributes) {\n    // Only load the image if it has been in the viewport for one second\n    return super.getObservable(attributes).pipe(debounceTime(1000))\n  }\n}\n```\n\nSee [intersection-listener.ts](https://github.com/tjoskar/ng-lazyload-image/blob/master/src/intersection-observer-hooks/hooks.ts) for example.\n\nSee `isVisible` if you want to use your own event.\n\n### isVisible\n\nFunction to check if the element is vissible.\n\nEg.\n\n```ts\nimport { Attributes, ScrollHooks } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends ScrollHooks {\n  isVisible(event: Event | string, { element, scrollContainer, offset }: Attributes) {\n    // `event` is form `getObservable`\n    return isElementInViewport(element, scrollContainer, offset);\n  }\n}\n```\n\nIf you want to create your own event, you can create both `getObservable` and `isVisible` by extend `SharedHooks`:\n\n```ts\nimport { Attributes, SharedHooks } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends SharedHooks\u003cnumber\u003e {\n\n  getObservable(attributes: Attributes) {\n    return interval(1000);\n  }\n\n  isVisible(event: number, attributes: Attributes) {\n    // `event` is 0,1,2,3,4,5,...\n    return isElementInViewport(element, scrollContainer, offset);\n  }\n}\n```\n\n### loadImage\n\nFunction to load the image. It must return a path to the image (it can however be async, like the example below and/or return a observable).\n\n```ts\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  loadImage({ imagePath }: Attributes): Promise\u003cstring\u003e {\n    return await fetch(imagePath)\n      .then((res) =\u003e res.blob())\n      .then((blob) =\u003e URL.createObjectURL(blob));\n  }\n}\n```\n\nIf you don't want to load the image but instead let the browser load it for you, then you can just return the imagePath (We will however not know if the image can't be loaded and the error image will not be used):\n\n```ts\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  loadImage({ imagePath }: Attributes) {\n    return [imagePath];\n  }\n}\n```\n\n### setLoadedImage\n\nA function to set the image url to the DOM.\n\nEg.\n\n```ts\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  setLoadedImage({ element, imagePath }: Attributes) {\n    // `imagePath` comes from `loadImage`\n    element.src = imagePath;\n  }\n}\n```\n\n### setErrorImage\n\nThis function will be called if the lazy image cant be loaded.\n\nEg.\n\n```ts\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  setErrorImage({ element, errorImagePath }: Attributes) {\n    element.src = errorImagePath;\n  }\n}\n```\n\n### setup\n\nThis function will be called on setup. Can be useful for (re)setting css-classes and setting the default image.\n\nThis function will be called every time an attrebute is changing.\n\nEg.\n\n```ts\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  setup(attributes: Attributes) {\n    // Overwride the path to the default image for all lazyloaded images\n    attributes.defaultImagePath = 'some/path.jpg';\n    // You can always call the base `setup` if you want to keep the default behavior\n    super.setup(attributes);\n  }\n}\n```\n\n### finally\n\nThis function will be called on teardown. Can be useful for setting css-classes.\n\nEg.\n\n```ts\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  finally(attributes: Attributes) {\n    // So something\n  }\n}\n```\n\n### isBot\n\nA function to check if the current user is a bot or not. Can be useful for SSR and SEO.\n\nDefault function:\n\n```ts\nimport { isPlatformServer } from '@angular/common';\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  isBot(attributes?: Attributes) {\n    // Check if the user is a bot or not.\n    this.navigator; // Is the same as `window.navigator` if window is defined otherwise undefined.\n    isPlatformServer(this.platformId); // True if the code is running on the server\n  }\n}\n```\n\nBoth `IntersectionObserverHooks` and `ScrollHooks` will load the image as soon as possble if `isBot` returns `true`.\n\n### isDisabled\n\nA function to decided if the module should be disabled, meaning: it should not do anything, just exit right away, without loading any image. The default behavior is to disable it on the server if the user is not a bot:\n\n```ts\nimport { isPlatformServer } from '@angular/common';\nimport { IntersectionObserverHooks } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  isDisabled() {\n    // This is the same as: `return super.isDisabled();`\n    return isPlatformServer(this.platformId) \u0026\u0026 !this.isBot();\n  }\n}\n```\n\n### skipLazyLoading\n\nA function to decided if we should load the image right away. This can be useful if you want to skip to lazyload the image when SSR:\n\n```ts\nimport { isPlatformServer } from '@angular/common';\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  skipLazyLoading(attributes: Attributes) {\n    return isPlatformServer(this.platformId); \n  }\n}\n```\n\nOr maybe you want to load the image ASAP from specific domains\n\n```ts\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  skipLazyLoading(attributes: Attributes) {\n    const imageUrl = new URL(attributes.imagePath);\n    return imageUrl.hostname === 'example.org';\n  }\n}\n```\n\nThe default behavior for `skipLazyLoading` is to call `isBot`. Meaning: if the user is a bot, load the image right away, otherwise, lazyload the image.\n\n### onAttributeChange\n\nThis function is called when some of the atrebute of the image is changed.\n\n```ts\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  onAttributeChange(newAttributes: Attributes) {\n    console.log(`New attributes: ${newAttributes}`);\n  }\n}\n```\n\n### onDestroy\n\nThis function is called when a image is loaded and the directive will unmount, aka. when `ngOnDestroy` is called in the directive. This can be useful if you want to do some cleanup.\n\n```ts\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  imageToBeLoaded = new Map\u003cstring, Attributes\u003e();\n\n  onAttributeChange(newAttributes: Attributes) {\n    console.log(`New attributes: ${newAttributes}`);\n    imageToBeLoaded.set(newAttributes.id, newAttributes);\n  }\n\n  onDestroy(attributes: Attributes) {\n    imageToBeLoaded.delete(attributes.id);\n  }\n}\n```\n\n## 🔎 Search Engine Optimization (SEO) \u003ca name = \"seo\"\u003e\u003c/a\u003e\n\n`ng-lazyload-image` are using the following strategy by default:\n\n### Server side rendering (SSR)\n\nIf the incoming request is from a bot; it will set `[lazyLoad]` to `src` on the image (letting the browser loading the image right away). Useful if the bot don't understand javascript.\n\nIf the request is not from a bot (or if we can't decide), don't do anything and let the client fix the images (see below).\n\nYou can chang this behavior by implementing your own `skipLazyLoading` function (see `skipLazyLoading` above). Let's say you always want to show the image ASAP for SSR, regardles of if the user is a bot or not:\n\n```ts\nimport { isPlatformServer } from '@angular/common';\nimport { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';\n\nclass LazyLoadImageHooks extends IntersectionObserverHooks {\n  skipLazyLoading(attributes: Attributes) {\n    // Skipping lazyloading the image for SSR\n    return isPlatformServer(this.platformId); \n  }\n}\n```\n\n### Client side\n\n- If the user is a bot (see `isBot` hook above), render all the images right away. (useful if the bot understand javascript)\n- If the user is not a bot (or if we can't decide), lazy load the images\n\n## 🤔 FAQ \u003ca name = \"faq\"\u003e\u003c/a\u003e\n\n**Q** How can I manually trigger the loading of images?\n\n**A** You can either use the `getObservable` hook if you can trigger the loading outside the dom or you can use the `customObservable` input, see [Custom Observable](#custom-observable)\n\n**Q** Does this library work with ionic or some other wrapper for Angular?\n\n**A** Yes, but ionic and some other library wraps the whole document inside an other div so you might need to create your own scroll listener.\n\n**Q** How can I add a transition effect between the default image and the lazy loaded image?\n\n**A** See: https://github.com/tjoskar/ng-lazyload-image/issues/300\n\n**Q** It doesn't work with `BrowserAnimationsModule`\n\n**A** Are you using the scroll preset? If so, take a look at this [issue](https://github.com/tjoskar/ng-lazyload-image/issues/438).\n\n**Q** Can I add a debounce time before loading the image?\n\n**A** Yes, take a look at this [issue](https://github.com/tjoskar/ng-lazyload-image/issues/456).\n\n**Q** Can I cancel image loading when the user change page?\n\n**A** Yes, take a look at this [issue](https://github.com/tjoskar/ng-lazyload-image/issues/458).\n\n**Q** I can't get it to work. Can you help me?\n\n**A** Sure, create an issue and describe your issue in as much detail as possible.\n\n## 🙇‍ Contributing \u003ca name = \"contributing\"\u003e\u003c/a\u003e\n\nSee the [contributing guide](CONTRIBUTING.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftjoskar%2Fng-lazyload-image","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftjoskar%2Fng-lazyload-image","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftjoskar%2Fng-lazyload-image/lists"}