Anuruddh Pratap Singh
Anuruddh Pratap Singh

Reputation: 17

How to scroll to particular id in react js?

My Html code is

<p id="4" style="color: red;">Dit antwoord is verplicht</p>
<p id="5" style="color: red;">Dit antwoord is verplicht</p>

i have add scrollIntoView but it's not working any other solution for scroll to particular id in react js

 document.getElementById(4).scrollIntoView();

Upvotes: 0

Views: 1476

Answers (1)

Cognisun Inc
Cognisun Inc

Reputation: 446

Here you can give try,

 window.scrollTo(0,this.findPosition(document.getElementById(4))); 

Below function is for finding place of particular id.

 findPosition(obj) {
   var currenttop = 0; 
   if (obj.offsetParent) { 
      do { 
          currenttop += obj.offsetTop;
       } while ((obj = obj.offsetParent)); 
      return [currenttop]; 
     } 
  } 

Upvotes: 1

Related Questions