Skip to content

Commit 677adb9

Browse files
committed
docs: updated readme
1 parent 272a1e1 commit 677adb9

File tree

1 file changed

+53
-67
lines changed

1 file changed

+53
-67
lines changed

README.md

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ Because embedded content takes time to load.
1212
- **Google maps** – 52 requests ≈ 580kb
1313
- **Vimeo** – 8 requests ≈ 145kb
1414

15-
Lazyframe creates a responsive placeholder for embedded content and requests it when the user interacts with it. This decreases the page load and idle time.
15+
Lazyframe creates a responsive placeholder for embedded content and requests it only when the user interacts with it. This decreases page load and idle time.
1616

1717
Lazyframe comes with brand-like themes for Youtube and Vimeo.
1818

1919
1. [Install](#install)
2020
2. [Import](#import)
21-
3. [Initialize](#Initialize)
21+
3. [Initialize](#initialize)
2222
4. [Options](#options)
2323

2424
### Install
2525

2626
NPM
2727

2828
```bash
29-
$ npm install lazyframe --save
29+
$ npm install @justia/lazyframe --save
3030
```
3131

3232
Bower
@@ -40,7 +40,7 @@ $ bower install lazyframe
4040
JavaScript ES6 imports
4141

4242
```js
43-
import lazyframe from "lazyframe";
43+
import lazyframe from "@justia/lazyframe";
4444
```
4545

4646
Include JavaScript in html
@@ -51,7 +51,7 @@ Include JavaScript in html
5151

5252
Sass import
5353

54-
```sass
54+
```scss
5555
@import 'src/scss/lazyframe'
5656
```
5757

@@ -63,30 +63,36 @@ Include css in html
6363

6464
### Initialize
6565

66+
The `lazyframe` function accepts a CSS selector, a single DOM element, or a collection of elements (like a `NodeList` or jQuery object).
67+
6668
```js
67-
// Passing a selector
69+
// Passing a selector string
6870
lazyframe(".lazyframe");
6971

70-
// Passing a nodelist
71-
let elements = document.querySelectorAll(".lazyframe");
72+
// Passing a NodeList
73+
const elements = document.querySelectorAll(".lazyframe");
7274
lazyframe(elements);
7375

74-
// Passing a jQuery object
75-
let elements = $(".lazyframe");
76-
lazyframe(elements);
76+
// Passing a single element
77+
const element = document.querySelector(".lazyframe");
78+
lazyframe(element);
7779
```
7880

7981
## Options
8082

81-
You can pass general options to lazyframe on initialization. Element-specific options (most options) are set on data attributes on the element itself.
83+
Configuration can be applied globally during initialization or on a per-element basis using `data-*` attributes. Element-specific attributes will always override global options.
84+
85+
### General Options
8286

83-
General options and corresponding defaults
87+
These options are passed as an object during initialization and apply to all instances unless overridden by a `data-*` attribute.
8488

8589
```js
8690
lazyframe(elements, {
87-
debounce: 250,
8891
lazyload: true,
8992
autoplay: true,
93+
initinview: false,
94+
showPlayButton: true,
95+
loadThumbnail: true,
9096

9197
// Callbacks
9298
onLoad: (lazyframe) => console.log(lazyframe),
@@ -95,69 +101,49 @@ lazyframe(elements, {
95101
});
96102
```
97103

98-
### `debounce`
99-
100-
Value (in milliseconds) for when the update function should run after the user has scrolled. [More here](https://css-tricks.com/the-difference-between-throttling-and-debouncing/)
101-
102-
### `lazyload`
103-
104-
Set this to `false` if you want all API calls and local images to be loaded on page load (instead of when the element is in view).
105-
106-
### `autoplay`
107-
108-
Set this to `false` to remove autoplay from the `allow` attribute on the iframe tag i.e if set this to `false` if you want don't want your Youtube video to automatically start playing once the user clicks on the play icon.
109-
110-
### `onLoad`
111-
112-
Callback function for when a element is initialized.
113-
114-
### `onAppend`
115-
116-
Callback function for when the iframe is appended to DOM.
104+
| Option | Type | Default | Description |
105+
| :--- | :--- | :--- | :--- |
106+
| `lazyload` | `(boolean)` | `true` | If `true`, uses `IntersectionObserver` to initialize only when the element is in the viewport. If `false`, initializes all elements on page load. |
107+
| `autoplay` | `(boolean)` | `true` | If `true`, the embedded content will attempt to play automatically after the user clicks the placeholder. Sets `autoplay=1` in the iframe `src` and adds `autoplay` to the `allow` attribute. |
108+
| `initinview` | `(boolean)` | `false` | If `true`, the iframe is immediately initialized (as if clicked) when the element enters the viewport. Requires `lazyload: true`. |
109+
| `showPlayButton` | `(boolean)` | `true` | If `false`, the play button graphic will not be added to the placeholder. |
110+
| `loadThumbnail` | `(boolean)` | `true` | If `false`, the fetched thumbnail image will not be applied as a background to the placeholder. |
111+
| `onLoad` | `(function)` | ` ` | Callback function fired when an element is initialized (i.e., enters the viewport or on page load if `lazyload` is `false`). Receives the lazyframe instance object. |
112+
| `onAppend` | `(function)` | ` ` | Callback function fired after the `iframe` is appended to the DOM upon user interaction. Receives the `iframe` element. |
113+
| `onThumbnailLoad` | `(function)` | ` ` | Callback function fired after a thumbnail URL is successfully fetched from the `noembed.com` API. Receives the image URL string. |
117114

118-
### `onThumbnailLoad`
115+
### Element-Specific Options (Data Attributes)
119116

120-
Callback function with the thumbnail URL
121-
122-
## Element-specific options
117+
These options are set as `data-*` attributes directly on the HTML element.
123118

124119
```html
125120
<div
126121
class="lazyframe"
127-
data-vendor=""
128-
data-title=""
129-
data-thumbnail=""
130-
data-src=""
131-
data-ratio="1:1"
132-
data-initinview="false"
122+
data-src="https://www.youtube.com/embed/ara1uUvajoU"
123+
data-vendor="youtube"
124+
data-title="Custom Title"
125+
data-thumbnail="https://example.com/custom-thumb.jpg"
126+
data-ratio="16:9"
133127
data-autoplay="false"
128+
data-initinview="true"
129+
data-show-play-button="false"
130+
data-load-thumbnail="false"
134131
></div>
135132
```
136133

137-
### `data-vendor`
138-
139-
Attribute for theming lazyframe. Currently supported values are `youtube`, `youtube_nocookie` and `vimeo`.
140-
141-
### `data-title`
142-
143-
Attribute for custom title. Leave empty to get value from noembed.com.
144-
145-
### `data-thumbnail`
146-
147-
Attribute for custom thumbnail. Leave empty to get value from noembed.com.
148-
149-
### `data-src`
150-
151-
The source of what you want to lazyload.
152-
153-
### `data-ratio`
154-
155-
The ratio of the lazyframe. Possible values: 16:9, 4:3, 1:1
156-
157-
### `data-initinview`
158-
159-
Set this to true if you want the resource to execute (for example video to play) when the element is in view.
134+
| Attribute | Description |
135+
| :--- | :--- |
136+
| `data-src` | **Required.** The source URL for the content you want to lazy-load. Any query parameters on this URL will be preserved. |
137+
| `data-vendor` | Specifies the vendor for theming the placeholder. Supported values: `youtube`, `youtube_nocookie`, `vimeo`. |
138+
| `data-title` | Sets a custom title. If omitted for a known vendor, the title is fetched from the `noembed.com` API. |
139+
| `data-thumbnail` | Sets a custom thumbnail URL. If omitted for a known vendor, the thumbnail is fetched from the API. |
140+
| `data-ratio` | Controls the aspect ratio of the placeholder. Possible values: `16:9`, `4:3`, `1:1`. |
141+
| `data-lazyload` | Overrides the global `lazyload` setting. Set to `"false"` to force this instance to initialize on page load. |
142+
| `data-autoplay` | Overrides the global `autoplay` setting. Set to `"false"` to prevent autoplay for this instance. |
143+
| `data-initinview` | Overrides the global `initinview` setting. Set to `"true"` to initialize the iframe when it enters the viewport. |
144+
| `data-show-play-button` | Overrides the global `showPlayButton` setting. Set to `"false"` to prevent the play button from being added. |
145+
| `data-load-thumbnail` | Overrides the global `loadThumbnail` setting. Set to `"false"` to prevent the thumbnail background from being applied. |
160146

161147
## License
162148

163-
[MIT](https://opensource.org/licenses/MIT). © 2016 Viktor Bergehall
149+
[MIT](https://opensource.org/licenses/MIT). © 2016 Viktor Bergehall

0 commit comments

Comments
 (0)