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

164
Views
¿Cómo reflejo el desplazamiento de dos cuadros deferentes?

Tengo dos cajas con el mismo contenido pero son de diferentes dimensiones y sincronizo el scroll entre ellas por lo que si haces scroll en una sigue la otra y viceversa, el problema es que una vez que haces scroll hacia abajo parece que sigue haciendo scroll solo, es hay alguna forma de arreglar esto?

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

No estoy seguro de por qué esto soluciona mi problema, pero aquí está mi solución:

Simplemente Math.ceil de una de las funciones de escucha de eventos.

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!