html - 在image的 `w`中解释 `srcset`

标签 html image css srcset

据我了解,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 the sizes 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 the sizes 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 specifying wolf-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 specifying wolf-400.jpg 0.33x, wolf-800.jpg 0.67x, wolf-1600.jpg 1.33x. By using the w descriptors and the sizes 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/

相关文章:

javascript - $scope 和此技术在通过服务共享数据方面有何不同?

php - 如何在youtube缩略图上添加图像

java - 在 ActionListener 中添加图像

css - 选择两个定义元素之间的元素

Javascript DIV 在滚动上移动

javascript - 如何使用javascript获取getElementsByTagName的长度

html - flex 布局溢出外容器

css - 链接垃圾邮件和我没有在我的 CSS 中写的各种垃圾

html - 无法显示背景图片

image - 无法将新的 docker 镜像推送到 bluemix