-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
123 lines (105 loc) · 4.57 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><lazyload-image>: HTMLImageElement extension for lazy loading</title>
<link rel="stylesheet" href="https://1000ch.github.io/gh-pages.css/gh-pages.css">
<style>
.image-container {
display: flex;
flex-direction: column;
margin: auto;
max-width: 960px;
padding: 16px;
}
.image-container img {
width: 100%;
}
</style>
<!-- Place this tag in your head or just before your close body tag. -->
<script defer src="https://buttons.github.io/buttons.js"></script>
<script defer src="https://unpkg.com/github-corner"></script>
<script type="module">
import { windowReady } from 'https://unpkg.com/html-ready';
import XMarked from 'https://unpkg.com/x-marked';
import LazyloadImage from 'https://unpkg.com/lazyload-image';
customElements.define('x-marked', XMarked);
customElements.define('lazyload-image', LazyloadImage, {
extends: 'img'
});
windowReady.then(() => {
const xmarked = document.querySelector('x-marked');
const div = document.querySelector('div');
const h2 = xmarked.shadowRoot.querySelector('h2');
xmarked.shadowRoot.querySelector('div').insertBefore(div, h2);
});
</script>
</head>
<body>
<div>
<!-- Place this tag where you want the button to render. -->
<a class="github-button" href="https://github.com/1000ch/lazyload-image" data-icon="octicon-star" data-size="large" data-show-count="true" aria-label="Star 1000ch/lazyload-image on GitHub">Star</a>
<!-- Place this tag where you want the button to render. -->
<a class="github-button" href="https://github.com/1000ch" data-size="large" data-show-count="true" aria-label="Follow @1000ch on GitHub">Follow @1000ch</a>
</div>
<github-corner>
<a href="https://github.com/1000ch/lazyload-image">GitHub</a>
</github-corner>
<x-marked highlight class="container">
🚩 [Native lazy-loading for the web](https://web.dev/native-lazy-loading/) is introduced finally, so I recommend you to use [`loading` attribute](https://html.spec.whatwg.org/#lazy-loading-attributes) instead.
# lazyload-image
[HTMLImageElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement) extension for lazy loading. Images will be loaded when they are shown.
## Install
Install `lazyload-image` via npm.
```bash
$ npm install lazyload-image
```
## Usage
Import `LazyloadImage` and register it.
```javascript
import LazyloadImage from 'https://unpkg.com/lazyload-image';
customElements.define('lazyload-image', LazyloadImage, {
extends: 'img'
});
```
Modify your `<img>` elements such as following.
```html
<img
is="lazyload-image"
src="path/to/your/image.jpg"
offset="200px"
width="100"
height="100"
>
```
## Fallback
If a browser does not support `customElements.define()`, images will be loaded as usual.
## License
[MIT](https://1000ch.mit-license.org) © [Shogo Sensui](https://github.com/1000ch)
</x-marked>
<div class="image-container">
<h1>Demo</h1>
<img is="lazyload-image" offset="200px" src="assets/img/1.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/2.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/3.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/4.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/5.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/6.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/7.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/8.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/9.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/10.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/11.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/12.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/13.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/14.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/15.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/16.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/17.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/18.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/19.jpg">
<img is="lazyload-image" offset="200px" src="assets/img/20.jpg">
</div>
</body>
</html>