|
7 | 7 |
|
8 | 8 | --- |
9 | 9 |
|
10 | | -### _**Status WIP**_ |
11 | | - |
12 | | ---- |
| 10 | + |
13 | 11 |
|
14 | 12 | > _**Project inspired by [lozad.js](https://github.com/ApoorvSaxena/lozad.js)**_ |
15 | 13 | > _**Big thx to [ApoorvSaxena](https://github.com/ApoorvSaxena) and community for their work on `lozad.js`**_ |
|
36 | 34 | | `triggerLoad()` method return Promise | _No_ | _**Yes**_ | |
37 | 35 |
|
38 | 36 |
|
39 | | -## Live preview |
40 | | - |
41 | | -[https://WezomAgency.github.io/zz-load/examples/](https://wezomagency.github.io/zz-load/examples/index.html) |
42 | | - |
43 | | ---- |
44 | | - |
45 | | - |
46 | | -# Install |
47 | | - |
48 | | -#### NPM |
49 | | - |
50 | | -```bash |
51 | | -npm i zz-load |
52 | | -``` |
53 | | - |
54 | | -#### CDN (unpkg.com) |
55 | | - |
56 | | -```html |
57 | | -<script src="https://unpkg.com/zz-load@latest/dist/zz-load.js"></script> |
58 | | -<!-- or minimized version --> |
59 | | -<script src="https://unpkg.com/zz-load@latest/dist/zz-load.min.js"></script> |
60 | | -``` |
61 | | - |
62 | | -#### Download |
63 | | - |
64 | | -- [zz-load.es.js](https://unpkg.com/zz-load@latest/zz-load.es.js) |
65 | | -- [dist/zz-load.js](https://unpkg.com/zz-load@latest/dist/zz-load.js) |
66 | | -- [dist/zz-load.min.js](https://unpkg.com/zz-load@latest/dist/zz-load.min.js) |
67 | | - |
68 | | ---- |
69 | | - |
70 | | -# Simple usage |
71 | | - |
72 | | -Prepare markup |
73 | | - |
74 | | -```html |
75 | | -<img class="zzload custom-image" |
76 | | - width="150" height="150" |
77 | | - src="./some-low-quality-placeholder.jpg" |
78 | | - data-zzload-source-img="./path/to/image.jpg"> |
79 | | -``` |
80 | | - |
81 | | -CSS for transition |
82 | | - |
83 | | -```css |
84 | | -.custom-image { |
85 | | - opacity: 0; |
86 | | -} |
87 | | - |
88 | | -/* zz-load will add `data-zzload-is-loaded` attribute |
89 | | - after fully resource loaded */ |
90 | | -.custom-image[data-zzload-is-loaded] { |
91 | | - opacity: 1; |
92 | | - transition: opacity .3s ease; |
93 | | -} |
94 | | -``` |
95 | | - |
96 | | - |
97 | | -import js module |
98 | | - |
99 | | -```js |
100 | | -import zzLoad from 'zz-load'; |
101 | | -``` |
102 | | - |
103 | | -or include as external file |
104 | | - |
105 | | -```html |
106 | | -<script src="your/path/to/zz-load.min.js"></script> |
107 | | -<!-- will be added to Window, as global object zzLoad --> |
108 | | -``` |
109 | | - |
110 | | -then fire up |
111 | | - |
112 | | -```js |
113 | | -const observer = zzLoad(); |
114 | | -observer.observe(); |
115 | | -``` |
116 | | - |
117 | | ---- |
118 | | - |
119 | | -## More Examples |
120 | | - |
121 | | -_documentation in progress_ |
122 | | - |
123 | | ---- |
124 | | - |
125 | | -# API |
126 | | - |
127 | | -### `zzLoad([elements][, options]): observer` |
128 | | - |
129 | | -#### `elements` |
130 | | - |
131 | | -_type_: `string | Element | NodeList | jQuery<Element>` |
132 | | -_default value_: `'.zzload'` |
133 | | - |
134 | | -Can be elements or string selector for find elements. |
135 | | - |
136 | | - |
137 | 37 |
|
| 38 | +## Docs and examples |
138 | 39 |
|
139 | | -#### `options` |
140 | | - |
141 | | -_type:_ `Object` |
142 | | - |
143 | | -##### `options.rootMargin` |
144 | | - |
145 | | -_default value:_ `'0px'` |
146 | | -read more: [IntersectionObserver.rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin) |
147 | | - |
148 | | - |
149 | | - |
150 | | - |
151 | | -##### `options.threshold` |
152 | | - |
153 | | -_default value:_ `0` |
154 | | -read more: [IntersectionObserver.thresholds](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/thresholds) |
155 | | - |
156 | | - |
157 | | - |
158 | | - |
159 | | - |
160 | | -##### `options.onLoad(element, source): void` |
161 | | - |
162 | | -_default value:_ `empty function` |
163 | | -Callback executed on fully success loaded source. |
164 | | -Has two arguments: |
165 | | - |
166 | | -- `element: Element` - current element |
167 | | -- `source: string` - loaded source url |
168 | | - |
169 | | -example: |
170 | | - |
171 | | -```js |
172 | | -import zzLoad from 'zz-load'; |
173 | | -const observer = zzLoad('.zzload', { |
174 | | - onLoad (element, source) { |
175 | | - element.classList.add('is-fully-loaded'); |
176 | | - console.log('Source successfully loaded:\n' + source); |
177 | | - } |
178 | | -}) |
179 | | -``` |
180 | | - |
181 | | - |
182 | | -> _Note! zzLoad automatically will add own attribute to elements,_ |
183 | | -> _for marking them as loaded, even without setting custom `onLoad()` method._ |
184 | | -> _So you can use this approach for custom styling._ |
185 | | -> _Read more in [Auto marking elements](#auto-marking-elements) section_ |
186 | | -
|
187 | | - |
188 | | - |
189 | | - |
190 | | - |
191 | | - |
192 | | -##### `options.onError(element, source): void` |
193 | | - |
194 | | -_default value:_ `empty function` |
195 | | -Callback executed on error loading source. |
196 | | -Has two arguments: |
197 | | - |
198 | | -- `element: Element` - current element |
199 | | -- `source: string` - failed source url |
200 | | - |
201 | | -example: |
202 | | - |
203 | | -```js |
204 | | -import zzLoad from 'zz-load'; |
205 | | -const observer = zzLoad('.zzload', { |
206 | | - onError (element, source) { |
207 | | - element.classList.add('is-damaged'); |
208 | | - console.warn('Something went wrong with source:\n' + source); |
209 | | - } |
210 | | -}) |
211 | | -``` |
212 | | - |
213 | | - |
214 | | -> _Note! zzLoad automatically will add own attribute to elements,_ |
215 | | -> _for marking them as failed, even without setting custom `onLoad()` method._ |
216 | | -> _So you can use this approach for custom styling._ |
217 | | -> _Read more in [Auto marking elements](#auto-marking-elements) section_ |
218 | | -
|
219 | | -### `observer` |
220 | | - |
221 | | -_documentation in progress_ |
222 | | - |
223 | | -#### `observer.observe()` |
224 | | - |
225 | | -_documentation in progress_ |
226 | | - |
227 | | -#### `observer.triggerLoad(): Promise` |
228 | | - |
229 | | -_documentation in progress_ |
230 | | - |
231 | | ---- |
| 40 | +https://WezomAgency.github.io/zz-load/ |
232 | 41 |
|
233 | | -# Auto marking elements |
| 42 | +## License |
234 | 43 |
|
235 | | -_documentation in progress_ |
| 44 | +[MIT](https://github.com/WezomAgency/zz-load/blob/master/LICENSE) |
236 | 45 |
|
237 | 46 |
|
0 commit comments