[英]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.