scrollTop Syncs Two Scroll Panes
follower.scrollTop = driver.scrollTop keeps two panes aligned; scrollHeight minus clientHeight is the maximum reachable offset.
Code
// Two panes scrolled together: the driver writes, the follower reads
const driver = document.querySelector('#driver');
const follower = document.querySelector('#follower');
driver.addEventListener('scroll', () => {
// scrollTop is the hidden offset above the viewport, in CSS pixels
follower.scrollTop = driver.scrollTop;
console.log('synced to', driver.scrollTop, 'of', driver.scrollHeight - driver.clientHeight);
});
// Horizontal offset works the same way through scrollLeft
follower.scrollLeft = driver.scrollLeft;
// Out-of-range assignment is clamped, never rejected
follower.scrollTop = 99999;
console.log('clamped to:', follower.scrollTop);
// scrollTop 0 snaps back to the very top
follower.scrollTop = 0;
console.log('reset:', follower.scrollTop);How it works
Top-level bindings driver, follower hold the demo state — nothing sits inside a function scope.
What to watch for
- Ready as-is on a bare install of the language.
- A browser is the runtime here:
documentare not available in Node.
Applying it
Output lines are prefixed (synced to, clamped to:, reset:), making the effect of each step easy to see.
Statements that decide the outcome
driver.addEventListener('scroll', () => {
// scrollTop is the hidden offset above the viewport, in CSS pixels
follower.scrollTop = driver.scrollTop;
console.log('synced to', driver.scrollTop, 'of', driver.scrollHeight - driver.clientHeight);
});
Interactive Tools for This Topic
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- Does scrollTop Syncs Two Scroll Panes run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.