In view

Find out which elements of a scrolling container are completely visible.

Demo

Try this: Scroll the box, or click it and use the arrow keys. A grey box that is completely visible turns yellow and says so. A box that is cut off at the top or bottom edge stays grey.

How it works

  1. Every box is an item with its own calculator, and the scrolling container is the boundary. There is no target: the library only checks the box against the boundary.
  2. calculate() returns distance.overflow, the list of edges the box overflows, or null if it overflows none. A box without overflow is completely visible.
  3. Without a target and without stick, nothing moves the box. The example ignores moveBy and only reads distance.
  4. The container's scroll event calls the update, because a calculation is not automatic: it runs when you call calculate(). The call also reads the current positions, so scrolling needs no resize(). The example calls it once at the start for the first state.
  5. The container has tabindex="0", so keyboard users can scroll it with the arrow keys.

Variations

See also