Picturefill and lazyloading with lazysizes

廉价感情. 提交于 2020-01-01 07:04:03

问题


I'm trying to get lazyloading with lazysizes and picturefill to work.

The lazyloading itself works if I just use a basic image:

<img class=lazyload data-srcset="http://placehold.it/301x301">

If I check the network tab in chrome, I can see that the image was loaded after the red line, so everything is fine:

Now I added a <picture> element with my responsive images and try to load that also lazy:

<picture>

    <!--[if IE 9]><video style="display: none;"><![endif]-->
    <source srcset="http://placehold.it/1370x301 1x, http://placehold.it/2055x450 2x" media="(min-width: 1024px)" />
    <source srcset="http://placehold.it/1023x300 1x, http://placehold.it/1534x450 2x" media="(min-width: 768px)" />
    <source srcset="http://placehold.it/767x300 1x, http://placehold.it/1151x450 2x" media="(min-width: 384px)" />
    <!--[if IE 9]></video><![endif]-->

    <img
            class="lazyload"
            style="width: 100%; max-width: 100%;"
            alt="Sample pic"
            data-srcset="http://placehold.it/383x300 1x, http://placehold.it/575x450 2x"
    />

</picture>

Unfortunately, this image is not lazyloaded but loaded directly:

I also tried using only one src in the picture element image, but that doesn't make a difference.

According to the lazysizes doc, it should be possible to make this work together, so I guess I'm just missing a small detail?

Update: Lazysizes and Picturefill are both loaded in the <head>.


回答1:


You need to use data-srcset instead of srcset:

<picture>

    <!--[if IE 9]><video style="display: none;"><![endif]-->
    <source data-srcset="http://placehold.it/1370x301 1x, http://placehold.it/2055x450 2x" media="(min-width: 1024px)" />
    <source data-srcset="http://placehold.it/1023x300 1x, http://placehold.it/1534x450 2x" media="(min-width: 768px)" />
    <source data-srcset="http://placehold.it/767x300 1x, http://placehold.it/1151x450 2x" media="(min-width: 384px)" />
    <!--[if IE 9]></video><![endif]-->

    <img
            class="lazyload"
            style="width: 100%; max-width: 100%;"
            alt="Sample pic"
            data-srcset="http://placehold.it/383x300 1x, http://placehold.it/575x450 2x"
    />

</picture>


来源:https://stackoverflow.com/questions/38120076/picturefill-and-lazyloading-with-lazysizes

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