【发布时间】:2015-07-10 05:08:56
【问题描述】:
响应式图像、用例和新的 HTML5 picture 元素在 this article 中得到了很好的解释。
更新:更具体地说,我的意思是响应式图像,针对不同设备调整大小的图像。更小的图像重量,所以faster loading sites。 最高可达72% less weight。
显示的示例只是文件名(没有路径),实际上它会更详细,例如:
<picture>
<source
media="(min-width: 1280px)"
sizes="50vw"
srcset="wp-content/uploads/2015/03/opera-fullshot-200.webp 200w,
wp-content/uploads/2015/03/opera-fullshot-400.webp 400w,
wp-content/uploads/2015/03/opera-fullshot-800.webp 800w,
wp-content/uploads/2015/03/opera-fullshot-1200.webp 1200w,
wp-content/uploads/2015/03/opera-fullshot-1600.webp 1600w,
wp-content/uploads/2015/03/opera-fullshot-2000.webp 2000w"
type="image/webp">
<source
sizes="(min-width: 640px) 60vw, 100vw"
srcset="wp-content/uploads/2015/03/opera-closeup-200.webp 200w,
wp-content/uploads/2015/03/opera-closeup-400.webp 400w,
wp-content/uploads/2015/03/opera-closeup-800.webp 800w,
wp-content/uploads/2015/03/opera-closeup-1200.webp 1200w,
wp-content/uploads/2015/03/opera-closeup-1600.webp 1600w,
wp-content/uploads/2015/03/opera-closeup-2000.webp 2000w"
type="image/webp">
<source
media="(min-width: 1280px)"
sizes="50vw"
srcset="wp-content/uploads/2015/03/opera-fullshot-200.jpg 200w,
wp-content/uploads/2015/03/opera-fullshot-400.jpg 400w,
wp-content/uploads/2015/03/opera-fullshot-800.jpg 800w,
wp-content/uploads/2015/03/opera-fullshot-1200.jpg 1200w,
wp-content/uploads/2015/03/opera-fullshot-1600.jpg 1800w,
wp-content/uploads/2015/03/opera-fullshot-2000.jpg 2000w">
<img
src="wp-content/uploads/2015/03/opera-closeup-400.jpg" alt="The Oslo Opera House"
sizes="(min-width: 640px) 60vw, 100vw"
srcset="wp-content/uploads/2015/03/opera-closeup-200.jpg 200w,
wp-content/uploads/2015/03/opera-closeup-400.jpg 400w,
wp-content/uploads/2015/03/opera-closeup-800.jpg 800w,
wp-content/uploads/2015/03/opera-closeup-1200.jpg 1200w,
wp-content/uploads/2015/03/opera-closeup-1600.jpg 1600w,
wp-content/uploads/2015/03/opera-closeup-2000.jpg 2000w">
</picture>
每次都设置完整路径似乎很尴尬。
我更喜欢这个:
<picture pathset="http://example.com/wp-content/uploads/2015/03/">
<source
media="(min-width: 1280px)"
sizes="50vw"
srcset="opera-fullshot-200.webp 200w,
opera-fullshot-400.webp 400w,
opera-fullshot-800.webp 800w,
opera-fullshot-1200.webp 1200w,
opera-fullshot-1600.webp 1600w,
opera-fullshot-2000.webp 2000w"
type="image/webp">
<source
sizes="(min-width: 640px) 60vw, 100vw"
srcset="opera-closeup-200.webp 200w,
opera-closeup-400.webp 400w,
opera-closeup-800.webp 800w,
opera-closeup-1200.webp 1200w,
opera-closeup-1600.webp 1600w,
opera-closeup-2000.webp 2000w"
type="image/webp">
<source
media="(min-width: 1280px)"
sizes="50vw"
srcset="opera-fullshot-200.jpg 200w,
opera-fullshot-400.jpg 400w,
opera-fullshot-800.jpg 800w,
opera-fullshot-1200.jpg 1200w,
opera-fullshot-1600.jpg 1800w,
opera-fullshot-2000.jpg 2000w">
<img
src="http://example.com/wp-content/uploads/2015/03/opera-closeup-400.jpg" alt="The Oslo Opera House"
sizes="(min-width: 640px) 60vw, 100vw"
srcset="opera-closeup-200.jpg 200w,
opera-closeup-400.jpg 400w,
opera-closeup-800.jpg 800w,
opera-closeup-1200.jpg 1200w,
opera-closeup-1600.jpg 1600w,
opera-closeup-2000.jpg 2000w">
</picture>
顺便说一句,为了可读性和编程逻辑,我更喜欢响应式 路径中的东西不在文件名中。你可以这样做
<picture pathset=/pathtoimages/> <source fileset=opera-fullshot.webp sizes="(min-width: 640px) 60vw, 100vw" ... srcset="200w/ 200w, 800w/ 800w, 1200w/ 1200w, 1600w/ 1600w,"> ... <source fileset=opera-closeup.webp ... srcset="200w/ 200w, 800w/ 800w, 1200w/ 1200w, 1600w/ 1600w,"> </picture>如果您放弃对某个宽度的支持,只需删除该目录即可。
但主要问题是,我们需要为 1 张图片编写大量代码。
所以,为了限制(没有任何 JS 解决方案):
我可以设置类似 pathsrc 属性或作用域 base 元素吗?
【问题讨论】:
-
对图片元素的支持太差了,不推荐使用it。
-
@persijn 图片填充多边形填充几乎涵盖了所有正在使用的浏览器。
-
不幸的是,规格可以节省多达 72% 的图像重量,它增加了 2000% 的 HTML(img 标签)重量。
标签: html image responsive-design picture-element