admin管理员组文章数量:1301558
I'm using lighthouse to measure page performance and I noticed, even if i use a low resolution background image and load the high one with javascript it still affects the larget contentful paint on lighthouse even when i wait until page is fully loaded... Is there something am i missing?
window.onload = function () {
const header = document.querySelector('.header');
header.style.backgroundImage =
'url(assets/img/the-image.webp)';
};
here is the link to the page. The low res image is hidden behind an overlay while the high res is being loaded
/
I'm using lighthouse to measure page performance and I noticed, even if i use a low resolution background image and load the high one with javascript it still affects the larget contentful paint on lighthouse even when i wait until page is fully loaded... Is there something am i missing?
window.onload = function () {
const header = document.querySelector('.header');
header.style.backgroundImage =
'url(assets/img/the-image.webp)';
};
here is the link to the page. The low res image is hidden behind an overlay while the high res is being loaded
https://nifty-cori-b75591lify.app/
Share edited Sep 7, 2020 at 14:58 Jackal asked Sep 7, 2020 at 14:40 JackalJackal 3,5215 gold badges44 silver badges94 bronze badges 11-
use some lazyload plugins, like
lazysizes
– demkovych Commented Sep 7, 2020 at 14:42 -
1
Isn't the value for
backgroundImage
missing a closing parenthesis in your snippet? – Parzh from Ukraine Commented Sep 7, 2020 at 14:49 - 1 Lighthouse usually suggests the improvements you can make. Can you share either a link to the page or share the generated lighthouse report. The link would be more helpful though. – JackSparrow Commented Sep 7, 2020 at 14:52
- 1 @Jackal According to web.dev/lcp/#what-is-a-good-lcp-score you are meeting a good LCP score. I'm beginning to wonder if this is a problem worth solving and is your attempt a case of over optimizing. Maybe others can chime in here. You could try a few other things like caching the image and using CDN or reducing the area of the header itself though I don't think it will improve your LCP. Anyways, just go through the link and see if there is a need and possibility to reduce the LCP time. You may have to do away with the full height header image. – JackSparrow Commented Sep 7, 2020 at 15:17
- 1 As per blog.chromium/2020/05/… - Largest Contentful Paint measures perceived load speed and marks the point in the page load timeline when the page's main content has likely loaded. I think you have pretty much done everything and it was a good choice to go with @AurelienB answer – JackSparrow Commented Sep 7, 2020 at 16:06
2 Answers
Reset to default 6I have bee keen on not using "background-image" for my high res background image.
I prefer to have something like :
<style>
.BackgroundImage-Container {
position: relative;
width : 100%;
height : 100%;
}
.BackgroundImage {
position: absolute;
top : 0;
right : 0;
bottom: 0;
left: 0;
width : 100%;
height : 100%;
object-fit: cover;
}
</style>
<picture class="BackgroundImage-Container">
<img class="BackgroundImage" src="./some-image.png" />
</picture>
So I am able to use any lazyload
, srcset
, <source media="" ></source>
that pleases me.
You can do Lazy Loading with Blurred Image Effect, couple of useful links for you!
see this thread:
#:~:text=attr('large%2Dclass',having%20to%20change%20the%20js!
https://tsucres.me/2017-12-01/Progressive-image-load.html
https://www.sitepoint./five-techniques-lazy-load-images-website-performance/
本文标签:
版权声明:本文标题:javascript - How to load a high res background image without impacting large contentful paint on page load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741676861a2391931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论