# @kilohaty/fog

> lazy load library for images

Latest version **0.1.1** (published 2018-10-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @kilohaty/fog
pnpm add @kilohaty/fog
yarn add @kilohaty/fog
bun add @kilohaty/fog
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2018-10-12 |
| First published | 2018-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 2.6 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | axi |
| Maintainers | kilohaty_axi |
| Keywords | images, lazy, load, lazyload, fog |

## Links

- npm: https://www.npmjs.com/package/@kilohaty/fog
- Repository: https://github.com/kilohaty/fog
- Homepage: https://github.com/kilohaty/fog#readme
- Issues: https://github.com/kilohaty/fog/issues
- npm.io page: https://npm.io/package/@kilohaty/fog

## Alternatives

- [exif-parser](https://npm.io/package/exif-parser.md) — 3.8M weekly downloads
- [vite-plugin-compression](https://npm.io/package/vite-plugin-compression.md) — 569.5K weekly downloads
- [pica](https://npm.io/package/pica.md) — 442.4K weekly downloads
- [@reportportal/client-javascript](https://npm.io/package/@reportportal/client-javascript.md) — 408.8K weekly downloads
- [@tldraw/state](https://npm.io/package/@tldraw/state.md) — 316.0K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2018-10-12
- 0.1.0 — 2018-10-11

## README

# fog
> A lazy load effect library for images 

<br/>

# Preview
_Note: the format of the preview is webp, Safari browser or other browsers may not support viewing._

![example](example_1.webp)

<br/>

# Getting Started

## Install
- via npm

  ```
  npm install @kilohaty/fog --save
  ```
  ```js
  // using ES6 modules
  import fog from '@kilohaty/fog';

  // using CommonJS modules
  var fog = require('@kilohaty/fog')
  ```  

- via script
  ```html
  <!-- you can find the library on window.fog -->
  <script src="/dist/bundle/fog.js"></script>
  ```
    
<br/>

## Setup Images

In HTML, add the following attributes to your images. ( **note: both images must have the same aspect ratio**)

| attribute name | required  | description |
|----------------|-----------|-------------|
|`data-src`      |  ✓        | source image url |
|`data-src-mini` |           | mini image url |
|`data-width`    |           | image width |
|`data-height`   |           | image height |

```html
<img src=""
     data-width="400"
     data-height="200"
     data-src="https://example.com/example_source.jpg" 
     data-src-mini="https://example.com/example_source.jpg.mini.jpg"
     alt="example" />
```

<br/>

## Set global config (Optional)

| config name | value type | default value | description |
|-------------|------------|---------------|-------------|
|`display`    | `string`   | inline-block  | display style of wrapped div|
|`width`      | `number`   | 1             | image width |
|`height`     | `number`   | 1             | image height |
|`backgroundColor` | `string`   |          | skeleton image color |
|`transitionDuration` | `number` |    1    | opacity transition duration (second) |
|`retryTimes` | `number`   |        2      | the number of retry when image load failed |
|`retryDuration` | `number`   |    3000    | retry interval time (ms) |
|`miniImgRule` | `Function`   |            | rule function for mini image |
|`errorImage` | `string`   |               | set replacement image when source image load failed |

```js
fog.setConfig({
  width: 400,
  height: 200,
  backgroundColor: '#ffff00',
  transitionDuration: 0.5,
  retryTimes: 1,
  retryDuration: 2000,
  miniImgRule: function (sourceImageUrl) {
    return sourceImageUrl + '.mini.jpg';
  },
  errorImage: 'https://example.com/error.jpg',
});
```
  
  
<br/>

## Init

```js
var el = document.querySelector('img');

// simple init
fog.init(el);

// or with options
fog.init({
  el: el,
  onSuccess() {
    // source image load success
  },
  onFail() {
    // source image load fail
  },
  onComplete() {
    // source image load complete (success or fail)
  }
});
```

---
_Source: https://npm.io/package/@kilohaty/fog · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
