简体   繁体   中英

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();

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]; 
     } 
  } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM