HTML5 srcset:混合 x 和 w 语法 [英] HTML5 srcset: Mixing x and w syntax

查看:23
本文介绍了HTML5 srcset:混合 x 和 w 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出一种方法来为 iOS8 客户端提供高 dpi 图像,同时还为支持 w 语法的浏览器提供响应式图像资源.根据 W3C 标准,应该可以在一个 srcset 属性中混合两种语法:

I'm trying to figure out a way to provide high dpi images to iOS8 clients while also providing responsive image resources for browsers supporting the w syntax. According to the W3C standard it should be possible to mix both syntaxes in one srcset attribute:

<img alt="The Breakfast Combo"
    src="banner.jpeg"
    srcset="banner-HD.jpeg 2x, banner-phone.jpeg 100w, banner-phone-HD.jpeg 100w 2x">

(来源:http://drafts.htmlwg.org/srcset/w3c-srcset/)

但是,当我在 Chrome 38(OS X,没有高 dpi)中运行该示例时,它在其他情况下支持 w 语法,浏览器总是加载最大的图像(banner-HD.jpeg),无论视口大小如何.当我添加

However, when I run that example in Chrome 38 (OS X, no high dpi) which does support the w syntax in other cases the browser always loads the biggest image (banner-HD.jpeg), regardless of the viewport size. When I add

banner.jpeg 1x

到 srcset Chrome 使用该图像但仍然忽略 100w 图像.

to the srcset Chrome uses that image but still ignores the 100w images.

就我而言,我想为两者指定较小版本的图像以及 2x 资源:

In my case I would like to specify a smaller version of an image as well as 2x resources for both:

<img src="default.png"
    srcset="small.png 480w, small@2x.png 480w 2x, medium.png 1x, medium@2x.png 2x">

这似乎适用于 2x iOS8 设备,它们选择 medium@2x.png 因为它们不支持 w 语法.然而,无论视口大小如何,Chrome 似乎仍然不关心并加载 medium.png.

That seems to work on 2x iOS8 devices, which pick medium@2x.png because they don't support the w syntax. However Chrome still doesn't seem to care and loads medium.png regardless of the viewport size.

我在这里做错了什么还是这是 Chrome 实现 srcset 中的一个已知问题?

Am I doing something wrong here or is this a known problem in Chrome's implementation of srcset?

推荐答案

  1. 不要看其他浏览器做什么.Chrome 是唯一一个正确的(FF 38+).
  2. 不要看这份草案.它不会也不会实施.这是正确的:https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset

在一个描述符中混合 x 和 w 是无效的,浏览器会丢弃那些候选,因为 w 描述符总是被计算到 x 描述符中:

Mixing x with w in one descriptor is invalid and the browser will drop those candidates, because a w descriptor is always calculated into a x descriptor:

<!-- invalid -->
<img srcset="a.jpg 1x 300w, b.jpg 2x 600w" />

浏览器使用/解析不同候选者的 x 和 w 描述符,但这是无效的,并且在 99% 的情况下都没有意义:

Mixing x with a w descriptor for different candidates is used/parsed by the browser but is invalid and doesn't make sense in 99% of all cases:

<!-- makes no sense: -->
<img srcset="300.jpg 1x, 600.jpg 600w" sizes="100vw" />

<!-- would make sense, because sizes is static in layoutpixel defined (i.e. 600 / 300 = 2x): -->
<img srcset="300.jpg 1x, 600.jpg 600w" sizes="300px" />

这意味着如果您使用 w 描述符,您也会自动针对视网膜进行优化,您不需要使用额外的 x 描述符(即 480w 2x = 960w).

This means if you use the w descriptor you automatically also optimize for retina, you don't need to use an additional x descriptor (i.e. 480w 2x = 960w).

此外,在大多数使用 w 描述符的情况下,您的默认/后备图像也应在 srcset 中定义:

Additionally, in most cases of using a w descriptor your default/fallback image should also be defined in srcset:

<img src="small.png"
    srcset="small.png 480w, mediumg.png 640w, large.png 960w"
    sizes="100vw" />

  1. 尝试 respimage polyfill(业余尝试宣传我的 polyfill)
  1. try respimage polyfill (dilettantish try to advertise my polyfill)

这篇关于HTML5 srcset:混合 x 和 w 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆