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

193
Views
How to move div from left to right hand side of the screen on scroll in ReactJS?

I am trying to move a div from left to right slowly as the user scrolls down. It should stay within it's section and doesn't have to move back to the left when you scroll up.

I am wondering how to do this without using any external package. This is what I have so far but the circle ends up scrolling off the page and I need it to stay on the right hand side once it has gotten there.

<div className="App">
      <div className="sectionOne">
        <div className="circle styles" style={{ position: 'absolute', left: `${position}rem` }}></div>
      </div>
      <div className="sectionTwo"></div>
    </div>
const [position, setPosition] = useState('70')

function moveCircle() {
    const speed = 5
    const scrolltop = window.pageYOffset
    const scrollAndSpeed = scrolltop / speed

    console.log(scrollAndSpeed.toString())
    setPosition(scrollAndSpeed.toString())
  }

  useEffect(() => {
    window.addEventListener(
      'scroll',
      function () {
        requestAnimationFrame(moveCircle)
      },
      false
    )
  }, [])
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I just noticed that you are using rem for the positioning.

So the Math.min would not work for this scenario. You can use the css clamp function.

style={{ position: 'absolute', left: `clamp(0vw, ${position}rem, 100vw - 1rem)` }}

Note that the 1rem i have put in there, should be the width of the .circle element.

about 4 years ago · Juan Pablo Isaza 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!