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

https://github.com/bosens-china/img-lazyload

图片懒加载
https://github.com/bosens-china/img-lazyload

img lazy lazyload

Last synced: 3 months ago
JSON representation

图片懒加载

Awesome Lists containing this project

README

          

# img-lazyload

![size](https://img.shields.io/badge/size-2.4KB-blue) ![License](https://img.shields.io/badge/License-MLT-blue)

[中文文档](./README_ZH.md)

It does not rely on high-performance JavaScript library, with a size of `2.4kb`. It supports modification of `src` and other elements `backgroundimage` attributes under img tag by default.

Before using this library, you need to pay more attention to: **for the elements that need to be loaded lazily, you must set a `width height` or an initial image**.

```html


```

## install

```sh
npm i @boses/img-lazyload
# or
yarn add @boses/img-lazyload
```

## usage method

```html

img {
width: 800px;
height: 800px;
}




```

```js
import imgLazyload from "@boses/img-lazyload";
imgLazyload("img");
```

For the `img` tag, when the element is visible, the value of the `data-src` attribute of the element is assigned to the SRC attribute of the element;

For non `img` tags, `backgroundimage` is used by default. When the element is visible, the value of the element `data-src` attribute is assigned to the `backgroundimage` attribute of the element;

## api

```js
lazyload(el: el, option?: options): void
```

- el:`string | Element | NodeList`

##### option?

| name | type | default | required | describe |
| -------- | ----------------------- | -------- | -------- | ------------------------------------- |
| dataSrc | `string` | data-src | `false` | The actual URL address of the element |
| callback | `(el: Element) => void` | none | `false` | callback |

The following is an advanced example. Load the image and clear the `data-src` attribute of the element. The style element of HTML is the same as above.

```js
import imgLazyload from "@boses/img-lazyload";
imgLazyload("img", {
callback(el) {
const src = el.getAttribute("data-src");
el.src = src;
el.removeAttribute("data-src");
},
});
```

## License

MIT License