繁体   English   中英

img srcset 和尺寸无法正常工作

[英]img srcset and sizes not working as I would expect

我目前正在尝试在我的网站上制作更好的响应式图像。 经过一番研究,我发现了srcsetsizes属性。

我的目标如下:

  • 当屏幕尺寸高于 600 像素或低于 300 像素时,我想提供 250x250 的图像
  • 当它介于这两个值之间时,我想提供 350x350 的图像
  • 我还想要具有更高像素比的屏幕的更高分辨率图像

这就是我想出的。 但它不像我想的那样工作。 始终提供 250x250 的小型广告。

<img src="https://placehold.it/250"
  srcset="https://placehold.it/700 700w, https://placehold.it/500 500w, https://placehold.it/350 350w, https://placehold.it/250 250w"
   sizes="((max-width: 600px) and (min-width: 300px) and (min-resolution: 2)) 350px, (min-resolution: 2) 250px, ((max-width: 600px) and (min-width: 300px)) 350px, 250px" />

我还有一个问题:
在我的测试中,我发现当调整窗口大小时浏览器不会加载新图像,因此应该提供不同的图像。 有可能这样做吗?

好的。 一些tidious研究后,我发现我不明白sizes正确-attribute。

以下是我如何实现我想要的:

<img src="https://placehold.it/250"
  srcset="https://placehold.it/700 700w, https://placehold.it/500 500w, https://placehold.it/350 350w, https://placehold.it/250 250w"
   sizes="(max-width: 600px) calc(100vw - 50px), 250px" />

暂无
暂无

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

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