据我了解,srcset
用于根据设备的 dpi
确定要加载的图像。我在 Google Web Fundamentals 上遇到了这个例子,它似乎使用了 w
单元和 vw
单元,它恰好是视口(viewport)宽度的 1%:
<img src="lighthouse-200.jpg" sizes="50vw"
srcset="lighthouse-100.jpg 100w, lighthouse-200.jpg 200w,
lighthouse-400.jpg 400w, lighthouse-800.jpg 800w,
lighthouse-1000.jpg 1000w, lighthouse-1400.jpg 1400w,
lighthouse-1800.jpg 1800w" alt="a lighthouse">
帮助我了解这是什么计量单位以及如何使用它?
最佳答案
根据 MDN w
是指:
a width descriptor (a positive integer directly followed by
w
). The width descriptor is divided by the source size given in thesizes
attribute to calculate the effective pixel density.
另外,W3状态:
The user agent will calculate the effective pixel density of each image from the specified
w
descriptors and the specified rendered size in thesizes
attribute. It can then choose any of the given resources depending on the user’s screen’s pixel density, zoom level, and possibly other factors such as the user’s network conditions. If the user’s screen is 320 CSS pixels wide, this is equivalent to specifyingwolf-400.jpg 1.25x, wolf-800.jpg 2.5x, wolf-1600.jpg 5x
. On the other hand, if the user’s screen is 1200 CSS pixels wide, this is equivalent to specifyingwolf-400.jpg 0.33x, wolf-800.jpg 0.67x, wolf-1600.jpg 1.33x
. By using thew
descriptors and thesizes
attribute, the user agent can choose the correct image source to download regardless of how large the user’s device is.
W3 页面上的示例 13 提供了更多详细信息。
关于html - 在image的 `w`中解释 `srcset`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40890825/