Posted - 04/01/2023 : 22:07:38
Hey All,
Is there a way to Lazy Load images in the Description box on the Proddetail page?
I can Lazy Load images on non product pages using some javascript with class="lazy" and using data-scr="/example-image.jpg"
I got this info from here: https://css-tricks.com/the-complete-guide-to-lazy-loading-images/ I'm using Method #2 Intersection Observer API
When I use data-src=" for a picture in the proddetail page and save... it changes it to data-="" src=" and then the images won't even load.
This was tried using CKEDITOR
I switched to FROALA and the data-src remained in place but the class="lazy" switched to: class="lazy fr-fic fr-dii"
Side Note: Neither of the HTML editors were able to display the image using data-scr= format.
Patrick
This is the script I am using that works great for lazy loading on other pages
<script> document.addEventListener("DOMContentLoaded", function() { var lazyloadImages;
if ("IntersectionObserver" in window) { lazyloadImages = document.querySelectorAll(".lazy"); var imageObserver = new IntersectionObserver(function(entries, observer) { entries.forEach(function(entry) { if (entry.isIntersecting) { var image = entry.target; image.src = image.dataset.src; image.classList.remove("lazy"); imageObserver.unobserve(image); } }); });
lazyloadImages.forEach(function(image) { imageObserver.observe(image); }); } else { var lazyloadThrottleTimeout; lazyloadImages = document.querySelectorAll(".lazy"); function lazyload () { if(lazyloadThrottleTimeout) { clearTimeout(lazyloadThrottleTimeout); }
lazyloadThrottleTimeout = setTimeout(function() { var scrollTop = window.pageYOffset; lazyloadImages.forEach(function(img) { if(img.offsetTop < (window.innerHeight + scrollTop)) { img.src = img.dataset.src; img.classList.remove('lazy'); } }); if(lazyloadImages.length == 0) { document.removeEventListener("scroll", lazyload); window.removeEventListener("resize", lazyload); window.removeEventListener("orientationChange", lazyload); } }, 20); }
document.addEventListener("scroll", lazyload); window.addEventListener("resize", lazyload); window.addEventListener("orientationChange", lazyload); } }) </script>
Edited by - midvalleydrifter001 on 04/02/2023 18:16:42
|