[英]Window.scrollTo is undefined in React
I want to do a simple window.scrollTo(0,0) on a button click.我想做一个简单的 window.scrollTo(0,0) 单击按钮。 I want it to just move the window to the top.
我希望它只是将 window 移动到顶部。 Unfortunately the
window.scrollTo(0,0)
does nothing with the GUI.不幸的是,
window.scrollTo(0,0)
对 GUI 没有任何作用。
I suspect it has something to do with React and the structure of the application.我怀疑它与 React 和应用程序的结构有关。
scrollToTop: function() {
window.scrollTo(0, 0);
}
Do I have to do it in another way with react or is there something I have missed?我是否必须以另一种方式做出反应,或者我错过了什么?
I found that the problem was that I had a fixed position on my main container. 我发现问题是我在主容器上的位置固定。
main-container {
position: fixed; // Should use either static or relative
}
This made the window keep its position "fixed", which meant that window.scrollTo()
would not change the position. 这使窗口保持其位置“固定”,这意味着
window.scrollTo()
不会更改位置。
So be careful using position: fixed;
因此请小心使用
position: fixed;
unless you want the element to stick in the viewport. 除非您希望该元素粘贴在视口中。 (like a header)
(如标题)
I have the same issue and found out this CSS code will also block the scrollTo() function.我有同样的问题,发现这个 CSS 代码也会阻止 scrollTo() function。
body, html {
overflow-x: hidden;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.