22.11.2024 Have you read it yet 3715 once It helped 2 to the readers

Disabling lazy loading of images (lazyload)

Some ready-made solutions on 1C-Bitrix or built-in browser functions may delay image loading until it appears in the visible area. This situation is typical for lazy loading image libraries such as lazysizes or lazyload.

Why does it work this way?

The initial image upload (or its placeholder) uses the value data:image/gif;base64,... in the attribute src so that the browser can display a minimal transparent or empty placeholder. The library then replaces it with the physical path specified in data-src as soon as the image enters the visible area.

If the setting is enabled on your ASPRO website: Use "lazy" (LazyLoad) image loading (displaying only visible images) and you need to disable it only for a specific block on the page, you can do the following:

JavaScript solution for the class .exclude-lazy img

Add a code that immediately sets the attribute value. src for images located in containers with the class .exclude-lazy, and eliminates lazy loading:

document.addEventListener("DOMContentLoaded", () => { const images = document.querySelectorAll(".exclude-lazy img.lazyload"); images.forEach((img) => { if (img.dataset.src) { img.src = img.dataset.src; // Setting src based on data-src img.removeAttribute("data-src"); // Removing the data-src attribute if it is no longer needed img.classList.remove("lazyload"); // Removing the lazyload class } }); });

Explanation:

  1. Image Search: document.querySelectorAll(".exclude-lazy img.lazyload") — select only images with the class .lazyload inside the container with the class .exclude-lazy.
  2. Replacement src: For each such image, we replace the attribute src on the value data-src.
  3. Deleting attributes: Removing the attribute data-src And the class lazyload to disable lazy loading and make the image look normal.

This code ensures that the images with the class exclude-lazy they will not be subject to lazy loading and will load immediately using their attribute. data-src.

Feedback

Was the material useful?

The assessment helps to understand which materials are worth developing and supplementing.

Discuss the task

Ask for help

Describe the task and leave your contacts. I will look at the introductory notes and contact you in a convenient way.

Direction *