Is there something wrong with my srcset definition, or is current browser support just weak?

三世轮回 提交于 2019-12-03 12:05:46
alexander farkas

In general with srcset you can't predict the chosen candidate.

Desktop Chrome: Chrome actually runs some very good but unpredictable algorithms to get the right image candidate. For example, if it is cached or as you said local it will use the largest image. Here everything should be fine.

Desktop Firefox: You seem to check with srcset on, but picture off. srcset with width descriptor and sizes attribute can only be enabled if both srcset and picture flag is enabled. But I would suggest that you test with current FF nightly because this feature will be truned on with 38 and to do so some bugs were fixed. Otherwise you will get the same behavior, you get with Safari for iOS. FF38 will have the desired behavior.

iOS: Chrome on iOS is not Chrome. The iOS browser does not support srcset with the width descriptor, but only supports the x descriptor. Even worse, if you use the width descriptor iOS thinks all candidates are equal (1x) and will load the first candidate from the srcset list.

Here is how you can deal with all your problems:

  1. Use a good polyfill: respimage or picturefill do the job
  2. Even if you don't use art direction use the following pattern:

<picture> <source srcset="images/image-1920.jpg 1920w, images/image-1440.jpg 1440w, images/image-1152.jpg 1152w, images/image-960.jpg 960w, images/image-840.jpg 840w, images/image-720.jpg 720w, images/image-576.jpg 576w, images/image-420.jpg 420w, images/image-288.jpg 288w, images/image-144.jpg 144w" sizes="(min-width: 40em) 576px, (min-width: 64em) 720px, (min-width: 72em) 960px, 100vw" /> <img /> </picture>

If you want to have more control you can also use lazySizes which gives you an autosizing calculation out of the box and in case you want to constrain the pixel ratio for mobile browsers (1.6x instead of 2x for example), the optimumx plugin.

You then could write your markup like this:

<picture> <source data-srcset="images/image-1920.jpg 1920w, images/image-1440.jpg 1440w, images/image-1152.jpg 1152w, images/image-960.jpg 960w, images/image-840.jpg 840w, images/image-720.jpg 720w, images/image-576.jpg 576w, images/image-420.jpg 420w, images/image-288.jpg 288w, images/image-144.jpg 144w" /> <img class="lazyload" data-sizes="auto" data-optimumx="1.6" /> </picture>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!