[BUG] onScroll never refreshes the window container's height after a vertical-only viewport resize
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):
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():
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:
<div class="spacer"></div>
<div class="target"></div>
<div class="spacer"></div>
<pre id="readout"></pre>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; }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
- In Chrome 154 (measured values below come from headless Chrome for Testing 151 driven by Playwright, viewport 800x600).
- Open the demo (in the pen, use the "Debug" view so the preview is the top-level window). The readout shows
window.innerHeightandcontainer.winHeightequal (600),offsetStart900 (= 1500 - 600). - Resize the window height only (drag the bottom edge, or dock DevTools at the bottom) to 400.
window.innerHeightbecomes 400,container.winHeightstays 600,offsetStartstays 900 instead of 1100, and the debug enter line stays at its old position. - Scroll:
onEnterfires at scrollY 900, while the target's top is still 200 px below the viewport's bottom edge. - Now change the window width.
container.winHeightimmediately follows to 400 andoffsetStartbecomes 1100, which shows that onlybody'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 |
Source: juliangarnier/anime