|
1 |
| -# <custom-element> element |
| 1 | +# Native lazy load boilerplate |
2 | 2 |
|
3 |
| -Boilerplate for creating a custom element. |
| 3 | +* 🧱 Pollyfill for modern browsers not supporting it yet |
| 4 | +* 👵 Gracefully degrades for old browsers |
| 5 | +* 🤖 Future proof markup |
| 6 | +* 🚉 Using the platform |
| 7 | + |
| 8 | +The whole idea is to create a copy-past boilerplate that works today. If by a miracle all browser vendors agree to ship native lazy load, you would **only have to change one place**. |
| 9 | + |
| 10 | +## Boilerplate |
| 11 | +```html |
| 12 | +<!-- 1. Markup --> |
| 13 | +<img |
| 14 | + is="lazy-loadable" |
| 15 | + loading="lazy" |
| 16 | + lazyload="1" |
| 17 | + importance="low" |
| 18 | + srcset="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAEElEQVR42gEFAPr/AP///wAI/AL+Sr4t6gAAAABJRU5ErkJggg==" |
| 19 | + src="https://placekitten.com/400/400" |
| 20 | + width="400" |
| 21 | + height="400" |
| 22 | + alt="" |
| 23 | +> |
| 24 | +<!-- 2. Safari - Custom elements pollyful --> |
| 25 | +<script> |
| 26 | + if(this.customElements) |
| 27 | + try{customElements.define('built-in',document.createElement('p').constructor,{'extends':'p'})} |
| 28 | + catch(s){document.write('<script src="//unpkg.com/@ungap/custom-elements-builtin"><\x2fscript>')} |
| 29 | + else |
| 30 | + document.write('<script src="//unpkg.com/document-register-element"><\x2fscript>'); |
| 31 | +</script> |
| 32 | +<!-- 3. Img HTML extension --> |
| 33 | +<script type="module"> |
| 34 | + import LazyLoadable from '../index.js'; |
| 35 | + customElements.define('lazy-loadable', LazyLoadable, { extends: "img" }); |
| 36 | +</script> |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +## Custom elements |
| 41 | +custom elements instead of querySelectorAll cuz no forEach needed |
| 42 | +better for inifinity scroll when images are not present when the for each loads |
| 43 | +## src-set instead of data-src use the platform, because removing it will fallback to browser default behaviour |
| 44 | +## In future all we would |
| 45 | +## data-src swap techinique is not exactly compatible with picture source |
| 46 | +## |
| 47 | +lazyload="1" importance="low" |
| 48 | + |
| 49 | + |
| 50 | + |
4 | 51 |
|
5 | 52 | ## Installation
|
6 | 53 |
|
|
0 commit comments