Scroll container

A note that stays inside a scrolling box, with a scroll handler that calculates again.

Demo

Try this: Scroll the dashed box. The note belongs to the Details button and sits above it. When the button comes close to the top edge of the box, the note jumps below the button, and when you scroll back it returns above. The Calculated values follow every scroll.

How it works

  1. The note is the item, the Details button is the target, and the scrolling box is the boundary. Both the button and the note are inside the scrolling content, so they move together when you scroll. Only the box stays where it is.
  2. The boundary is the box element. The library does not look for a scroll container, so you pass it. The other choices are "window" and "document", which measure the page viewport (see Limits for frames), and null for no boundary. A target can be an element, "window", "document", or a mouse or pointer event, as in the pointer example.
  3. The calculation is not automatic. The example listens to the scroll event of the box and calls calculate() again. The call reads the current positions, so scrolling needs no resize().
  4. When the button is close to the top edge of the box, the note would leave the boundary at the top. With flip: "both" it goes below the button. When you scroll back, the next calculation puts it above the button again.
  5. The example removes the transform of the note before every calculation, because calculate() starts from where the note is now.
  6. The box has tabindex="0", so keyboard users can scroll it with the arrow keys.

Variations

See also