From ce64821500208926b273e88c2f09f908238aa63b Mon Sep 17 00:00:00 2001 From: Fluffy-Bean Date: Wed, 5 Apr 2023 11:50:05 +0000 Subject: [PATCH] Check for webp image support --- gallery/static/js/pre/main.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/gallery/static/js/pre/main.js b/gallery/static/js/pre/main.js index 11a1163..f1190b3 100644 --- a/gallery/static/js/pre/main.js +++ b/gallery/static/js/pre/main.js @@ -1,3 +1,11 @@ +let webpSupport = false; +try { + new Image().src = 'data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA='; + webpSupport = true; +} catch (e) { + webpSupport = false; +} + // fade in images function imgFade(obj, time = 250) { obj.style.transition = `opacity ${time}ms`; @@ -10,7 +18,11 @@ function loadOnView() { for (let i = 0; i < lazyLoad.length; i++) { let image = lazyLoad[i]; if (image.getBoundingClientRect().top < window.innerHeight && image.getBoundingClientRect().bottom > 0) { - if (!image.src) { image.src = image.getAttribute('data-src') } + if (!image.src && webpSupport) { + image.src = image.getAttribute('data-src') + '&e=webp' + } else if (!image.src) { + image.src = image.getAttribute('data-src') + } } } }