Javascript / CSS window.scrollTo(0,0)无效 [英] Javascript / CSS window.scrollTo(0,0) not working
问题描述
我正在构建一个Web应用程序,当执行某个操作时,该Web应用程序的一部分需要滚动到顶部。这是使用 window.scrollTo(0,0)
处理的,这完全有效,直到我在下面添加一些CSS样式(我需要这个项目)。
I am building a web app, and part of that web app needs to scroll to the top when an action is performed. This is handled using window.scrollTo(0,0)
which works perfectly until I add some CSS styling below (which I require for this project).
有没有人知道为什么下面的CSS会停止 window.scrollTo(0,0)
工作?
Has anybody got any idea why the CSS below would stop window.scrollTo(0,0)
working?
* {
-webkit-transform: translate3d(0,0,0);
-moz-transform: translate3d(0,0,0);
-ms-transform: translate3d(0,0,0);
-o-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
}
P.S。我不能在我的情况下使用jQuery,以防万一我建议我这样做。
P.S. I cannot use jQuery in my situation, just in case it's suggested that I do that.
推荐答案
我有同样的问题,这个修复它:
I had the same problem, this fixed it:
document.getElementById('ELEMENT_ID')。scrollIntoView();
其中 ELEMENT_ID
是您要滚动到视图的对象的ID,在您的情况下,它通常是标题。
Where ELEMENT_ID
is the id of the object you want to scroll into view, in your case it would typically be the header.
请注意,这仅适用于Firefox浏览器,因为其他浏览器尚未实现平滑滚动选项(请参阅更多信息: http://caniuse.com/#feat=scrollintoview )。
Note this only works fully on Firefox browsers as the other browsers have yet to implement the smooth scrolling option (see more: http://caniuse.com/#feat=scrollintoview).
这篇关于Javascript / CSS window.scrollTo(0,0)无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!