本文picture
很好地解释了响应式图像、用例和新的 HTML5元素。
更新:更具体地说,我的意思是响应式图像,针对不同设备调整大小的图像。更小的图像重量,因此更快的加载网站。重量减轻多达72%。
显示的示例只是文件名(没有路径),实际上它会更详细,例如:
<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属性或作用域的基本元素吗?