#1192·anime

[BUG] onScroll never refreshes the window container's height after a vertical-only viewport resize

Author: LucasDemeaCreated Sep 28, 2026Updated Sep 28, 2026

Describe the bug

In window mode, ScrollContainer only watches document.body with a ResizeObserver and never listens to resize on window (src/events/scroll.js):

javascript
this.resizeObserver = new ResizeObserver(() => this.resizeTicker.restart());
this.resizeObserver.observe(this.element); // this.element === doc.body when useWin
(this.useWin ? win : this.element).addEventListener('scroll', this, false);

body's box only depends on its content. When the content is sized in px (no vh, no 100% chain), changing the viewport height alone does not resize body, so the observer never fires and container.winHeight / container.height keep the old value. Every enter / leave line is then computed against the stale viewport height: onEnter fires while the target is still below the fold, and debug lines are drawn at the old position.

This happens on any page for a vertical-only resize: dragging the bottom edge of the window, opening DevTools docked at the bottom, the mobile URL bar collapsing, or a parent iframe changing height. Changing the width repairs it, because a reflow changes body's height and the observer fires. Calling observer.refresh() does not help either, since it reuses container.height.

Suggested fix: in window mode, also restart the ticker on window resize, and remove the listener in revert():

javascript
if (this.useWin) win.addEventListener('resize', this.onWinResize = () => this.resizeTicker.restart());

Provide a minimal reproduction demo of the bug

https://codepen.io/lucasdemea/pen/vExZMKG

The pen loads [email protected] (UMD bundle from jsDelivr) and boils down to:

xml
<div class="spacer"></div>
<div class="target"></div>
<div class="spacer"></div>
<pre id="readout"></pre>
css
body { margin: 0; }
.spacer { height: 1500px; background: #eee; }
.target { height: 100px; background: tomato; }
#readout { position: fixed; top: 8px; left: 8px; padding: 8px; background: #000; color: #fff; }
javascript
const { onScroll } = anime;

const observer = onScroll({
  target: '.target',
  enter: 'bottom top',
  debug: true,
  onEnter: () => console.log('enter at scrollY', window.scrollY),
});

const readout = document.querySelector('#readout');
(function tick() {
  readout.textContent =
    `window.innerHeight  : ${window.innerHeight}\n` +
    `container.winHeight : ${observer.container.winHeight}\n` +
    `observer.offsetStart: ${Math.round(observer.offsetStart)}`;
  requestAnimationFrame(tick);
})();

Detail the steps to reproduce the issue

  1. In Chrome 154 (measured values below come from headless Chrome for Testing 151 driven by Playwright, viewport 800x600).
  2. Open the demo (in the pen, use the "Debug" view so the preview is the top-level window). The readout shows window.innerHeight and container.winHeight equal (600), offsetStart 900 (= 1500 - 600).
  3. Resize the window height only (drag the bottom edge, or dock DevTools at the bottom) to 400. window.innerHeight becomes 400, container.winHeight stays 600, offsetStart stays 900 instead of 1100, and the debug enter line stays at its old position.
  4. Scroll: onEnter fires at scrollY 900, while the target's top is still 200 px below the viewport's bottom edge.
  5. Now change the window width. container.winHeight immediately follows to 400 and offsetStart becomes 1100, which shows that only body's reflow triggers the refresh.

Measured values from the Playwright run:

step innerHeight body height container.winHeight offsetStart
initial 800x600 600 3100 600 900
height only, 800x400 400 3100 600 900
then width, 700x400 400 3100 400 1100