简体   繁体   English

图片元素:我需要在每个scrset属性中设置完整路径吗?

[英]Picture element: do I need to set the full path in every scrset attribute?

Responsive images, the use-cases and the new HTML5 picture element are explained quite well in this article . 本文很好地解释了响应式图像,用例和新的HTML5 picture元素。

Update : To be more specific, I mean with responsive images, resized images for different devices. 更新 :更具体地说,我的意思是响应式图像,即针对不同设备调整大小的图像。 Less image weight, so faster loading sites . 图像重量较小,因此加载站点更快 Up to 72% less weight . 减轻多达72%的重量

The shown Examples are just filenames (no path), in practice it will be more verbose like: 所示示例只是文件名(无路径),在实践中它将更加冗长,例如:

<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>

Setting the full path every time seems akward. 每次设置完整路径似乎很尴尬。

I prefer this: 我更喜欢这样:

<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>

BTW for readability and programming logic I prefer the responsive stuff in the path not in filename. 顺便说一句,出于可读性和编程逻辑的考虑,我更喜欢路径中的响应性内容,而不是文件名。 The you can do this 你可以做到的

 <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> 

If you drop support for a certain width, just delete the directory. 如果放弃对特定宽度的支持,只需删除目录。

But the main issue, we need quite a lot of code for 1 image. 但主要问题是,一张图片需要大量代码。

So, to limit that (without any JS solution): 因此,要限制它(没有任何JS解决方案):

Can I set something like a pathsrc attribute or scoped base element? 我可以设置类似pathsrc属性或范围基本元素的内容吗?

Nope. 不。

It is an obvious idea to use a template syntax in srcset. 在srcset中使用模板语法是一个显而易见的想法。 In fact it was part of the earliest srcset draft . 实际上,它是最早的srcset草案的一部分 However, URLs can and do contain anything, and don't always map to a template (eg generated GUIDs for different images), so it didn't work. 但是,URL可以并且确实包含任何内容,并且并不总是映射到模板(例如,为不同图像生成的GUID),因此它不起作用。

In theory you could use XHTML and xml:base . 理论上,您可以使用XHTML和xml:base In practice, you don't use XHTML, and xml:base support is being removed from browsers. 实际上,您不使用XHTML,并且浏览器中删除了 xml:base支持。

I would recommend not worrying so much about the repetition, just make sure you use gzip. 我建议不要为重复问题担心太多,只需确保使用gzip。

Disclaimer this is not an solution to your question. 免责声明这不是您问题的解决方案。 But a solution for responsive images 但是响应式图像的解决方案

SVG SVG

The <svg> element is highly supported. 高度支持 <svg>元素

There is an example of a responsive svg image: 有一个响应式svg图像的示例:

 circle { fill: firebrick; } svg { border: 1px solid black; } 
 <svg width="100%" height="100%" viewBox="0 0 100 100"> <rect x="10" y="10" width="50" height="50" fill="DarkOrange" /> <rect x="40" y="40" width="50" height="50" fill="SeaGreen" /> <circle cx="50" cy="50" r="30" /> </svg> 

Press the run code snippet and then Full page to test it out. 运行代码段 ,然后按整进行测试。

CSS CSS

Or you can simple use an image that scales down properly: 或者,您可以简单地使用适当缩放的图像:

 body { width: 100%; } .container { width: 100%; } .container img { display: block; max-width: 100%; height: auto; } 
 <div class="container"> <img src="http://i.ytimg.com/vi/4rY4PlSCy48/maxresdefault.jpg" /> </div> 

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

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