简体   繁体   English

如何在react js中滚动到特定的id?

[英]How to scroll to particular id in react js?

My Html code is我的 HTML 代码是

 <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我已经添加了 scrollIntoView,但它不能用于在 react js 中滚动到特定 id 的任何其他解决方案

 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.下面的函数用于查找特定 id 的位置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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