Use Picture element to automatically choose webp

Remove loadOnView function as has to be remade for new picture element
Remove broken way of checking for Webp support
This commit is contained in:
Michał Gdula 2023-04-22 11:46:20 +00:00
parent 16df5bc255
commit 2cf7bc9091
10 changed files with 119 additions and 124 deletions

View file

@ -5,26 +5,8 @@ function imgFade(obj, time = 200) {
setTimeout(() => { obj.style.animation = null; }, time);
}
// Lazy load images when they are in view
function loadOnView() {
const lazyLoad = document.querySelectorAll('#lazy-load');
const webpSupport = checkWebpSupport();
for (let i = 0; i < lazyLoad.length; i++) {
const image = lazyLoad[i];
if (image.getBoundingClientRect().top < window.innerHeight && image.getBoundingClientRect().bottom > 0) {
if (!image.src && webpSupport) {
image.src = `${image.getAttribute('data-src')}&e=webp`;
} else if (!image.src) {
image.src = image.getAttribute('data-src');
}
}
}
}
window.onload = function () {
loadOnView();
const times = document.querySelectorAll('.time');
for (let i = 0; i < times.length; i++) {
// Remove milliseconds
@ -70,8 +52,6 @@ window.onload = function () {
}
};
window.onscroll = function () {
loadOnView();
// Top Of Page button
const topOfPage = document.querySelector('.top-of-page');
if (document.body.scrollTop > 300 || document.documentElement.scrollTop > 20) {
@ -90,6 +70,3 @@ window.onscroll = function () {
}
}
};
window.onresize = function () {
loadOnView();
};

View file

@ -1,10 +0,0 @@
function checkWebpSupport() {
let webpSupport = false;
try {
webpSupport = document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') === 0;
} catch (e) {
webpSupport = false;
}
return webpSupport;
}

View file

@ -1,4 +1,4 @@
.image-container
.image-container, picture
margin: auto
width: 100%
@ -20,7 +20,7 @@
object-position: center
@media (max-width: 1100px)
.image-container
.image-container, picture
margin: 0 auto
max-height: 69vh

View file

@ -26,4 +26,4 @@
padding: 0
font-size: 1.25rem
font-weight: 700
font-weight: 700