-
Notifications
You must be signed in to change notification settings - Fork 12
Add lazyLoad module #540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add lazyLoad module #540
Changes from all commits
96a01fc
69050c1
11aa301
3fb16c9
041b456
8a347a8
c46ef3e
78fd4e1
213c583
18b3f99
e5c5e97
d4725a4
4160e30
f9d27b5
71e6543
911e51b
9b47562
9c365ca
c93951d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| .lazy-load { | ||
| background: #f7fafb; | ||
| border-radius: 3px; | ||
| width: 150px; | ||
|
|
||
| @media (--mobile) { | ||
| width: 75px; | ||
| } | ||
|
|
||
| &__media { | ||
| opacity: 0; | ||
| transition: opacity 0.6s ease; | ||
| } | ||
|
|
||
| &--loaded { | ||
| width: auto; | ||
| background: transparent; | ||
|
|
||
| > ^&__media { | ||
| opacity: 1; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /** | ||
| * Module for lazy-loading images & videos | ||
| */ | ||
| export default class LazyLoad { | ||
|
|
||
| /** | ||
| * Classes used in module | ||
| */ | ||
| static get CSS() { | ||
|
|
||
| return { | ||
| wrapper: 'js_lazy-load', | ||
| loaded: 'lazy-load--loaded', | ||
| itemAttribute: 'data-lazy-src' | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Initialize module | ||
| */ | ||
| init() { | ||
|
|
||
| this.loadMedia(); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Find media targets on page and apply to them various lazy-loading technique depending on type: image or video | ||
| */ | ||
| loadMedia() { | ||
|
|
||
| const items = document.querySelectorAll(`[${LazyLoad.CSS.itemAttribute}]`); | ||
|
|
||
| items.forEach((item) => { | ||
|
|
||
| switch (item.tagName) { | ||
|
|
||
| case 'IMG': | ||
| this.loadImage(item); | ||
| break; | ||
| case 'VIDEO': | ||
| this.loadVideo(item); | ||
| break; | ||
|
|
||
| } | ||
|
|
||
| }); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Images lazy-loading technique | ||
| * @param {HTMLElement} element - target image | ||
| */ | ||
| loadImage(element) { | ||
|
|
||
| /** | ||
| * Create temporary image with real image src. | ||
| * When temporary image is loaded, reveal real image | ||
| */ | ||
| const tempImage = new Image(); | ||
| const imageSrc = element.getAttribute(LazyLoad.CSS.itemAttribute); | ||
|
|
||
| tempImage.src = imageSrc; | ||
| tempImage.onload = () => { | ||
|
|
||
| element.src = imageSrc; | ||
| this.addLoadedClass(element); | ||
|
|
||
| }; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Videos lazy-loading technique | ||
| * @param {HTMLElement} element - target video | ||
| */ | ||
| loadVideo(element) { | ||
|
|
||
| const tempVideo = document.createElement('video'); | ||
| const tempVideoSource = document.createElement('source'); | ||
| const videoSrc = element.getAttribute(LazyLoad.CSS.itemAttribute); | ||
|
|
||
| tempVideoSource.src = videoSrc; | ||
| tempVideo.appendChild(tempVideoSource); | ||
|
|
||
| tempVideo.onloadeddata = () => { | ||
|
|
||
| element.querySelector('source').src = videoSrc; | ||
| this.addLoadedClass(element); | ||
| element.load(); | ||
|
|
||
| }; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Add loaded class when element is ready | ||
| * @param {HTMLElement} element - media target | ||
| */ | ||
| addLoadedClass(element) { | ||
|
|
||
| element.closest(`.${LazyLoad.CSS.wrapper}`).classList.add(LazyLoad.CSS.loaded); | ||
|
|
||
| } | ||
|
|
||
| } |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.