简体   繁体   English

本机延迟加载 (loading=lazy) 即使启用了标志也无法正常工作

[英]Native lazy-loading (loading=lazy) not working even with flags enabled

I am currently trying to update my Website using the new loading="lazy" attribute as shown here: https://web.dev/native-lazy-loading我目前正在尝试使用新的 loading="lazy" 属性更新我的网站,如下所示: https://web.dev/native-lazy-loading

As seen in the video, everything works as expected, but compared with my waterfall diagram in chrome, it doesn't.正如视频中所见,一切都按预期工作,但与我在 chrome 中的瀑布图相比,它并没有。

How it looks:外观:铬开发者工具

How it should look:它应该如何看:

铬开发者工具

This is how its implemented:这是它的实现方式:

<img class="has-border" src="https://andreramoncombucket.s3.amazonaws.com/static/assets/img/work/personal-website/pw_full.jpg" style="object-fit: cover;" alt="..." loading="lazy">

I had a similar issue when trying to implement it.我在尝试实现它时遇到了类似的问题。

I use Chrome by default and it was not working.我默认使用 Chrome,但它无法正常工作。 When I tested it in Firefox, it did work.当我在 Firefox 中测试它时,它确实有效。 That made me think it was a browser problem.这让我认为这是一个浏览器问题。

After digging in a bit more, I found out the "problem" for my case.在深入挖掘之后,我发现了我的案例的“问题”。 It might be the same probably for many others.对于许多其他人来说,这可能是相同的。

It turns out Chrome is more impatient than Firefox when loading images tagged as lazy.事实证明,在加载标记为惰性的图像时, Chrome 比 Firefox 更不耐烦 That means it loads the images much earlier, so an image will not be loaded when it appears at the screen but earlier than that.这意味着它会更早地加载图像,因此当图像出现在屏幕上时不会加载图像,但会更早。 Firefox, on the other side, is loading the images almost when they are about to be shown at the screen.另一方面,Firefox 几乎在图像即将显示在屏幕上时加载图像。

The images I was testing were below the fold, but the page was not very long, so Chrome was loading the images anyway.我正在测试的图像在首屏,但页面不是很长,所以 Chrome 无论如何都在加载图像。

When I tried it in a much longer article, the images that were deep down the article did load lazily in Chrome as well.当我在一篇更长的文章中尝试它时,文章深处的图像也确实在 Chrome 中延迟加载。

Hope this helps!希望这可以帮助!

I had a similar problem, and after some research, I found the solution:我遇到了类似的问题,经过一番研究,我找到了解决方案:

Just need to add width and height to the IMG tag, this is because the browser needs to know the size of the element before applying lazy loading.只需要在IMG标签中添加宽度和高度,这是因为浏览器在应用延迟加载之前需要知道元素的大小。

Your code:你的代码:

<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy">

Your code after adding width and height:添加宽度和高度后的代码:

<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy" width="200px" height="200px">

Another alternative is using inline style:另一种选择是使用内联样式:

<img class="has-border" src="..." style="object-fit:cover; height:200px; width:200px;" alt="..." loading="lazy">

Take into consideration I just randomly set the dimensions of the IMG tag to 200px.考虑到我只是将IMG标签的尺寸随机设置为 200 像素。 You can find more information on this web.dev article您可以在此web.dev 文章中找到更多信息

hope it helps 👍希望有帮助👍

我最糟糕的原因是它不起作用 - 我忘记添加widthheight属性(它们在我的损坏代码中被添加为style

optionally if you dont want to change your image size use this可选地,如果您不想更改图像大小,请使用此

Original code原始代码

<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy">

With working Lazy load with no size restrictions使用没有大小限制的工作延迟加载

<img class="has-border" src="..." style="object-fit: cover;" alt="..." loading="lazy" width="auto" height="100%">

I have applied loading="lazy" to all of my images, included width="111px" height="111px" attributes for my <img> tags and even converted them into WebP format to reduce size, but my Firefox browser still keeps loading all images from the entire page.我已将loading="lazy"应用到我的所有图像,包括width="111px" height="111px"属性到我的<img>标签,甚至将它们转换为 WebP 格式以减小尺寸,但我的 Firefox 浏览器仍在加载整个页面中的所有图像。 I don't touch or scroll after refresh.刷新后我不触摸或滚动。 The indicator Dev tools says that it is - lazy-img .指标 Dev tools 说它是 - lazy-img

I use Nodejs, vue and tailwind .我使用Nodejs、vue 和 tailwind

What could be wrong?有什么问题吗?

It could be because of cache .可能是因为缓存

If you see here lazy-img in the Network tab that means everything is fine.如果您在“网络”选项卡中看到此处的 lazy-img ,则表示一切正常。

You can test on a private window if you want just to double check.如果您只想仔细检查,可以在私人 window 上进行测试。

在此处输入图像描述

You could try change src to data-src .您可以尝试将src更改为data-src It worked for me in Chrome (even though it is a supported browser).它在 Chrome 中对我有用(即使它是受支持的浏览器)。

Replace <img src> with <img data-src> > to avoid an eager load in unsupported browsers.<img src>替换为<img data-src> > 以避免在不受支持的浏览器中急切加载。

https://web.dev/native-lazy-loading/ https://web.dev/native-lazy-loading/

图像应在样式上定位为“相对”。

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

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