简体   繁体   English

为什么谷歌浏览器加载 JPG 而不是 Avif 和 WebP?

[英]Why Google Chrome loads JPG instead of Avif and WebP?

I'm scratching my head why Google Chrome loads JPG instead of Avif and WebP?我在摸不着头脑,为什么 Google Chrome 会加载 JPG 而不是 Avif 和 WebP? Here's my code:这是我的代码:

<picture>
<source media="(max-width: 991px)" srcset="/assets/img/768w.avif" type="image/avif">
<source media="(max-width: 991px)" srcset="/assets/img/768w.webp" type="image/webp">
<source media="(max-width: 991px)" srcset="/assets/img/576w.avif" type="image/avif">
<source media="(max-width: 991px)" srcset="/assets/img/576w.webp" type="image/webp">
<img src="/assets/img/768w.jpg" alt="image" width="100%" height="auto" class="img-fluid" decoding="async" loading="lazy">
</picture>

First of all of your sources are specified for screens smaller than 991px – I guess you are loading page on larger device (or window width) so only default src fits the query.首先,为小于 991px 的屏幕指定了所有源 - 我猜您正在更大的设备(或 window 宽度)上加载页面,因此只有默认 src 适合查询。 The second mistake is expecting filename should be recognized as media query.第二个错误是期望文件名应该被识别为媒体查询。 I guess you are looking for something like this:我猜你正在寻找这样的东西:

<source media="(max-width: 991px)" srcset="/assets/img/576w.avif 576w, /assets/img/768w.avif 768w" type="image/avif">

This means "while screen is smaller than 991px use avif. According to your screen pixel density and current width will be loaded one of two images".这意味着“当屏幕小于 991px 时使用 avif。根据您的屏幕像素密度和当前宽度将加载两个图像之一”。 Same for webp. webp也是一样。 You also need to specify sources for screens larger than 991px.您还需要为大于 991 像素的屏幕指定来源。 Take a look at this example看看这个例子

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

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