Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alexstaroselsky/svelte-lazy-image

Svelte action and component to lazy load an image
https://github.com/alexstaroselsky/svelte-lazy-image

lazy-loading svelte sveltekit

Last synced: about 2 months ago
JSON representation

Svelte action and component to lazy load an image

Awesome Lists containing this project

README

        

# svelte-lazy-image

Svelte component to lazy load images using [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).

## Usage

```bash
npm install svelte-lazy-image
```

### Svelte:

#### use:action

Set `data-src` and/or `data-srcset` on an `img` element:

```html

import { useLazyImage as lazyImage } from 'svelte-lazy-image';

foobar
```

Specify IntersectionObserver [options](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver) via object passed to `use`:

```html

import { LazyImage, useLazyImage as lazyImage } from 'svelte-lazy-image';

foobar
```

#### Component

```html

import { LazyImage } from 'svelte-lazy-image';

```

The component uses `$$restProps` to pass props other than `placeholder`, `src`, or `alt` to the underlying `img` element. An example using `img` attributes `srcset` and `sizes`:

```html

import { LazyImage } from 'svelte-lazy-image';

```

Specify IntersectionObserver [options](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver):

```html

import { LazyImage } from 'svelte-lazy-image';

```