KEMBAR78
When is a lazy-loaded image "about to intersect the viewport" · Issue #5408 · whatwg/html · GitHub
Skip to content

When is a lazy-loaded image "about to intersect the viewport" #5408

@zcorpan

Description

@zcorpan

If img's lazy loading attribute is in the Lazy state, img does not intersect the viewport, and img is not about to intersect the viewport, then return true.

https://html.spec.whatwg.org/multipage/images.html#updating-the-image-data:lazy-loading-attribute

An element is said to intersect the viewport when it is being rendered and its associated CSS layout box intersects the viewport.

Note: This specification does not define the precise timing for when the intersection is tested, but it is suggested that the timing match that of the Intersection Observer API. [INTERSECTIONOBSERVER]

https://html.spec.whatwg.org/multipage/rendering.html#intersect-the-viewport

When to start loading a lazy-loaded image is a key aspect of the feature, but the spec doesn't give advice beyond what is quoted above. Right now, different implementations do different things: Chromium starts loading early (I think currently 3000px to 8000px before entering the viewport, depending on effective network speed and latency), Gecko and WebKit start loading late (when at least 1px is visible). See https://www.ctrl.blog/entry/lazy-loading-viewports.html -- they argue that the implemented extremes are too early and too late; nobody has the goldilocks "just right" behavior, yet.

From my experiments, it seems Chromium only applies the "margin" for top-level page scrolling. For images that are in scrollable elements, or in iframes, the loading starts when the element is at least 1px visible. The spec doesn't differentiate between different cases of "about to become visible". The element scroll container case is common for image carousels.

See this demo: https://lazy-img-demo.glitch.me/

To view the same demo in an iframe: https://glitch.com/edit#!/lazy-img-demo - click "Show" and then "Next to The Code".

I'm curious what JS libraries that implement lazy-loaded images do. Have they iterated on this, and know something we could apply here?

Usually, details like this are left to the UA to optimize. However, I think it's important to get some consistency in implementations for web developers to be able to use the feature and know that browsers won't load all images anyway (because their scrollable area is smaller than the browser's lazy margins) and won't load images too late, resulting in users always seeing images load after they're within the viewport.

cc @domfarolino @bengreenstein @emilio @smfr @othermaciej @rwlbuis

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions