Javascript 我需要使用React.JS水平滚动到div id?

Javascript 我需要使用React.JS水平滚动到div id?,javascript,reactjs,scroll,Javascript,Reactjs,Scroll,我试过scrollIntoView({“block”:“center”})在chrome上运行得很好,但在Internet Explorer上有问题 试试看 要获取页面中任何元素的位置,可以使用下面的“getBoundingClientRect”方法,并使用window.scrollTo()滚动到该特定位置 var htmlElement=document.getElementById('container'); var elementPosition=element.getBoundingCl

我试过scrollIntoView({“block”:“center”})在chrome上运行得很好,但在Internet Explorer上有问题

试试看

要获取页面中任何元素的位置,可以使用下面的“getBoundingClientRect”方法,并使用window.scrollTo()滚动到该特定位置

var htmlElement=document.getElementById('container');
var elementPosition=element.getBoundingClientRect()
window.scrollTo(elementPosition.left,0)


另请参阅此链接了解更多信息:

scrollIntoView在IE/Edge中提供部分支持

IE/Edge不支持滚动视图({“block”:“center”})


  • 您可以选择
    scrollIntoView(true)
    对应于
scrollIntoView({block:“start”,inline:“nearest”})

  • 您可以选择
    scrollIntoView(false)
    对应于
scrollIntoView({block:“end”,inline:“nearest”})


如果您需要
{“block”:“center”}
,我建议您使用其他API或方法

在这里阅读更多关于它的信息


未按预期工作!对于水平滚动,只需给出x坐标值。给它元素的左值。我已经根据您是否尝试过调试编辑了代码..因为它支持IE Too,您可以提供一个替代API吗?@shubhampandey我建议查看scrollTo,例如
var bcr=element.getBoundingClientRect();滚动到(bcr.left-bcr.width/2,bcr.top-bcr.height/2)
scrollFunc()
{    
     document.getElementById("myID").scrollIntoView({"block":"center"})    
}