Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
How do I mirror the scrolling of two deferent boxes

I have two boxes with the same content but they are of different dimensions and I synchronise scrolling between them so if you scroll one the other follows and vice versa, the problem is that once you scroll down it seams to continue scrolling on its own, is there any way to fix this?

Codepen: https://codepen.io/rgbskills/pen/JjrRpVN?editors=0010

let master = document.getElementById('text');
let slave = document.getElementById('text2');

const masterScrollHeight = master.scrollHeight;
const masterClientHeight = master.clientHeight;
const masterHeight = masterScrollHeight - masterClientHeight;

const slaveScrollHeight = slave.scrollHeight;
const slaveClientHeight = slave.clientHeight;
const slaveHeight = slaveScrollHeight - slaveClientHeight;


const handleScrollMaster = (e) => {
  const newMasterScrollTop = e.currentTarget.scrollTop;
  const percentageMasterDone = newMasterScrollTop / masterHeight;
  const newSlaveScrollTop = Math.ceil(percentageMasterDone * slaveHeight);
  
  slave.scroll({
    top: newSlaveScrollTop
  })
}

const handleScrollSlave = (e) => {
  
  const newSlaveScrollTop = e.currentTarget.scrollTop;
  const percentageSlaveDone = newSlaveScrollTop / slaveHeight;
  const newMasterScrollTop = Math.ceil(percentageSlaveDone * masterHeight);
  
  master.scroll({
    top: newMasterScrollTop
  })
}

master.addEventListener("scroll", handleScrollMaster);
slave.addEventListener("scroll", handleScrollSlave);
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

I'm not sure why this fixes my problem but here is my solution:

I simply removed the Math.ceil from one of the event listener funstions.

Codepen: https://codepen.io/rgbskills/pen/JjrRpVN?editors=0010

let master = document.getElementById('text');
let slave = document.getElementById('text2');

const masterScrollHeight = master.scrollHeight;
const masterClientHeight = master.clientHeight;
const masterHeight = masterScrollHeight - masterClientHeight;

const slaveScrollHeight = slave.scrollHeight;
const slaveClientHeight = slave.clientHeight;
const slaveHeight = slaveScrollHeight - slaveClientHeight;


const handleScrollMaster = (e) => {
  const newMasterScrollTop = e.currentTarget.scrollTop;
  const percentageMasterDone = newMasterScrollTop / masterHeight;
  const newSlaveScrollTop = Math.ceil(percentageMasterDone * slaveHeight);
  slave.scroll({
    top: newSlaveScrollTop
  })
}

const handleScrollSlave = (e) => {
  const newSlaveScrollTop = e.currentTarget.scrollTop;
  const percentageSlaveDone = newSlaveScrollTop / slaveHeight;
  // for some reason if I apply Math.ceil here it will cause an infinite scroll
  const newMasterScrollTop = percentageSlaveDone * masterHeight;
  
  master.scroll({
    top: newMasterScrollTop
  })
}

master.addEventListener("scroll", handleScrollMaster);
slave.addEventListener("scroll", handleScrollSlave);
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!