• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

287
Views
How to get scroll Position in reactjs when click on button

As you can see it I am not able to scroll down the page when I click on the button, This all code running in one file in App.js
Even I tried with useRef() and UseState() also but not working
I need help in JavaScript logic only
Javascript:

const myFunction = () => {
        const element = document.getElementById("myDIV");
        element.scrollLeft = 50;
        element.scrollTop = 10;
    };

HTML:

<div
    id="myDiv"
    style={{ height: "250px", width: "250px", overflow: "auto" }}
>
        <div
                id="content"
                style={{
                height: "800px",
                width: "2000px",
                backgroundColor: "coral",
            }}
        >
        <button onClick={myFunction}>Scroll</button>
        <p>Some text inside a div element.</p>
        <p>Some text inside a div element.</p>
        <p>Some text inside a div element.</p>
        <p>Some text inside a div element.</p>
    </div>
</div>
almost 3 years ago · Santiago Gelvez
3 answers
Answer question

0

Here:

const element = document.getElementById("myDIV");

the ID is wrong. It should be myDiv because you set the div's ID to myDiv:

<div
    id="myDiv"
    style={{ height: "250px", width: "250px", overflow: "auto" }}
>

and also you can use the scroll function of Element instead of setting scrollLeft and scrollTop:

const myFunction = () => {
  const element = document.getElementById("myDiv");
  element.scroll(50, 10);
}

But I recommend using refs (I know you mentioned it but I'm not sure if you used it correctly).

Create a ref:

const divRef = useRef();

Assign the ref to the div:

<div
    id="myDiv"
    ref={divRef}
    style={{ height: "250px", width: "250px", overflow: "auto" }}
>

Then scroll it (you don't need the element variable anymore):

const myFunction = () => {
  divRef.current.scroll(50, 10);
};
almost 3 years ago · Santiago Gelvez Report

0

This is code referanceuseRef() Not working here event I used useRef(null) also

function App() {
    const divRef = useRef();

    const myFunction = () => {
        divRef.current.scroll(0, 100); // Not Working, need to scroll y-100 only 
    };

    return (
        <>
            <div
                id="myDiv"
                style={{ height: "250px", width: "250px", overflow: "auto" }}
            >
                <div
                    id="content"
                    ref={divRef}
                    style={{
                        height: "800px",
                        width: "2000px",
                        backgroundColor: "coral",
                    }}
                >
                    <button onClick={myFunction}>Scroll</button>
                    <br/>
                    <input onClick={myFunction} type="radio" name="s" id="s" />
                    <label onClick={myFunction} htmlFor="">Salary Person</label>
                    <br/>
                    <input onClick={myFunction} type="radio" name="s" id="s" />
                    <label onClick={myFunction} htmlFor="">Salary Person</label>
                    <br/>
                    <input onClick={myFunction} type="radio" name="s" id="s" />
                    <label onClick={myFunction} htmlFor="">Salary Person</label>
                    <p>Some text inside a div element.</p>
                    <p>Some text inside a div element.</p>
                    <p>Some text inside a div element.</p>
                    <p>Some text inside a div element.</p>
                </div>
            </div>
        </>
    );
}
almost 3 years ago · Santiago Gelvez Report

0

I think this is what you wanted. Since the "scroll" button dissapears after the scroll you can

import React, { useEffect, useState } from "react";
import "./App.css";

function App() {
const [scrollPos, setScrollPos] = useState(0);

useEffect(() => {
  setScrollPos(scrollPos + 100);
}, []);

function myFunction() {
// Get the scrollable element which is the parent div.
  let element = document.getElementById("myDiv");
  // Set this to whatever you need. Right now it will start at      100, then go to 200, 300...
  setScrollPos(scrollPos + 100);

  // Calls the scroll function.
  element.scroll({
    // Top is for the y position.
    top: scrollPos,
    //Use left for x position.
    //left: 100
    // It looks nicer if the scroll is smooth
    behavior: "smooth"
  });
}

return (
<>
  <div
    id="myDiv"
    style={{ height: "250px", width: "250px", overflow: "auto" }}
  >
    <div
      id="content"
      style={{
        height: "800px",
        width: "2000px",
        backgroundColor: "coral"
      }}
    >
      <button
        onClick={myFunction}
        //Add this is you want the button to stay in the corner.
        //style={{ position: "absolute" }}
      >
        Scroll
      </button>
      <br />
      <input onClick={myFunction} type="radio" name="s" id="s" />
      <label onClick={myFunction} htmlFor="">
        Salary Person
      </label>
      <br />
      <input onClick={myFunction} type="radio" name="s" id="s" />
      <label onClick={myFunction} htmlFor="">
        Salary Person
      </label>
      <br />
      <input onClick={myFunction} type="radio" name="s" id="s" />
      <label onClick={myFunction} htmlFor="">
        Salary Person
      </label>
      <p>Some text inside a div element.</p>
      <p>Some text inside a div element.</p>
      <p>Some text inside a div element.</p>
      <p>Some text inside a div element.</p>
    </div>
  </div>
</>

); }

export default App;

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error