scrollTop Syncs Two Scroll Panes

follower.scrollTop = driver.scrollTop keeps two panes aligned; scrollHeight minus clientHeight is the maximum reachable offset.

JavascriptData Integrity

Code

javascriptsnippet
20 lines
// 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: document are 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

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.