[英]I need to horizontally scroll to a div id using React.JS ?
scrollFunc()
{
document.getElementById("myID").scrollIntoView({"block":"center"})
}
我尝试了scrollIntoView({“ block”:“ center”})在chrome中可以正常使用,但是Internet Explorer上有问题!
scrollIntoView在IE / Edge中具有部分支持。
IE / Edge不支持scrollIntoView({"block":"center"})
scrollIntoView(true)
对应于 scrollIntoView({block: "start", inline: "nearest"})
scrollIntoView(false)
对应于 scrollIntoView({block: "end", inline: "nearest"})
如果需要{"block":"center"}
我建议使用其他API或方法。
在此处了解更多信息。
https://developer.mozilla.org/zh-CN/docs/Web/API/Element/scrollIntoView https://caniuse.com/#search=scroll
试试看
要获取页面中任何元素的位置,您可以像这样使用'getBoundingClientRect'方法,并使用window.scrollTo()滚动到该特定位置。
var htmlElement = document.getElementById('container'); var elementPosition = element.getBoundingClientRect() window.scrollTo(elementPosition.left, 0)
另请参阅此链接以获取更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/API/Element/getBoundingClientRect
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.