简体   繁体   English

延迟加载图像时避免过大的 DOM 大小

[英]Avoid an excessive DOM size when lazy loading images

I have a problem with dom size (3,044 elements).我对 dom 大小(3,044 个元素)有疑问。 I have a slider (Slick Slider) and images used in it causing DOM size prolem.我有一个滑块(Slick Slider)和其中使用的图像导致 DOM 大小问题。 Img tag has lazy loading, it loads background image first and then loading main image data-src. img 标签具有延迟加载功能,它先加载背景图像,然后加载主图像数据源。

Reference to DOM size is this image:参考 DOM 大小是这张图片:

<img src="/assets/img/img_bg_md.png" data-src="/uploads/images/202212/image_430x256_639c5b0a29f1f.webp" alt="Title" class="lazyload img-responsive img-post" width="1" height="1">

Is there a way to solve that problem?有没有办法解决这个问题?

I'm not sure I understand what you want, but I'll try to help.我不确定我明白你想要什么,但我会尽力提供帮助。 You could set the already fixed image size to the image size you already know it will be.您可以将已经固定的图像大小设置为您已知的图像大小。 So you could put a background image of the image for it to load prettier所以你可以放一张图片的背景图片,让它加载得更漂亮

<style>
  .img-responsive {
    background-color: #ccc;
  }
</style>

<img 
    src="/assets/img/img_bg_md.png" 
    data-src="/uploads/images/202212/image_430x256_639c5b0a29f1f.webp"
    alt="Title" 
    class="lazyload img-responsive img-post"
    style="width: 1px;height: 1px"
/ >


<script>
    const imagens = document.querySelectorAll('.img-responsive');
    var src, resp;
    for (const imagem of imagens) {
        src = imagem.getAttribute("data-src");
        resp = src.match(/image_(\d+)x(\d+)_/);
        if(const && resp.length > 2){
            imagem.style.width = resp[1] + 'px';
            imagem.style.height = resp[2] + 'px';
        }

        imagem.onload = function() {
            imagem.parentElement.style.backgroundColor = 'transparent';
            imagem.style.width = imagem.naturalWidth + 'px';
            imagem.style.height = imagem.naturalHeight + 'px';
        }
    }
</script>

Other suggestions:其他建议:

There are a few things you can try to reduce the DOM size when using lazy loading for images:在对图像使用延迟加载时,您可以尝试一些方法来减小 DOM 大小:

  • Use a lighter version of the main image as the background image.使用主图像的浅色版本作为背景图像。 This can help reduce the size of the DOM element and also improve the loading speed of the page.这有助于减小 DOM 元素的大小并提高页面的加载速度。

  • Use a placeholder image as the background image.使用占位符图像作为背景图像。 This can help reduce the size of the DOM element and also improve the loading speed of the page.这有助于减小 DOM 元素的大小并提高页面的加载速度。

  • Use a smaller image as the background image.使用较小的图像作为背景图像。 This can help reduce the size of the DOM element and also improve the loading speed of the page.这有助于减小 DOM 元素的大小并提高页面的加载速度。

  • Use responsive images.使用响应图像。 By using the srcset attribute, you can specify different sizes of the same image for different screen sizes, which can help reduce the size of the DOM element and also improve the loading speed of the page.通过使用 srcset 属性,可以为不同的屏幕尺寸指定同一张图片的不同尺寸,这有助于减小 DOM 元素的尺寸,也可以提高页面的加载速度。

  • Use image optimization tools.使用图像优化工具。 There are several tools available that can help you optimize your images, which can reduce the size of the DOM element and improve the loading speed of the page.有多种工具可以帮助您优化图像,从而减小 DOM 元素的大小并提高页面的加载速度。

  • Use a content delivery network (CDN).使用内容分发网络 (CDN)。 By using a CDN, you can offload the delivery of your images to a network of servers around the world, which can help reduce the size of the DOM element and improve the loading speed of the page.通过使用 CDN,您可以将图像的传送卸载到世界各地的服务器网络,这有助于减小 DOM 元素的大小并提高页面的加载速度。

It's worth noting that there is no one-size-fits-all solution to this problem, and you may need to try a combination of these approaches to find the best solution for your specific use case.值得注意的是,对于这个问题没有放之四海而皆准的解决方案,您可能需要尝试组合使用这些方法来找到适合您的特定用例的最佳解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM