jQuery的scrollTop的()的所有元素,则返回0 [英] jQuery scrollTop() returns 0 for all elements
问题描述
在我的工作,我们有我AngularJS创建一个页面的网站。结果
我们使用UI路由器插件(版本0.2.0)。结果
最近我注意到,从一个国家转移到另一个时,窗口不滚动到顶部。结果
我甚至试图把它滚动到顶部使用上的每个状态变化jQuery的 scrollTop的()
函数手动(使用 $ stateChangeSuccess
事件)。但它没有工作。
At my job we have a one page site I created with AngularJS.
We're using the ui-router plugin (version 0.2.0).
Recently I've noticed that when moving from one state to another, the window isn't scrolled to the top.
I even tried to scroll it to the top manually using jQuery's scrollTop()
function on every state change (using the $stateChangeSuccess
event). But it didn't work.
于是我开始调查,我已经注意到, scrollTop的()
是页面上的每一个元素返回0。结果
不仅如此,但是当我打印 window.scrollY
控制台我得到0(无论我在哪里在页面上)。不仅在我的code,但即使我只是把它写在Chrome开发工具的控制台。
So I started investigating, and I've noticed that scrollTop()
is returning 0 for every element on the page.
Not only that, but when I print the window.scrollY
to the console I get 0 (no matter where I'm at on the page). Not only in my code, but even if I just write it in the chrome dev tools console.
我已经写了几个应用程序与AngularJS和UI路由器,它只是在这个特殊的情况发生。
I've written several apps with AngularJS and ui-router, and it only happens in this particular one.
我检查,看看是否我已覆盖 scrollTop的()
功能甚至 window.scrollY
现场,但没有发现任何东西。
I've checked to see if I have overwritten the scrollTop()
function or even the window.scrollY
field, but haven't found anything.
我已经使用用户界面视图
与自动滚屏=真正的
和自动滚屏=FALSE
,但它并没有发挥作用。
I've tried using the ui-view
with autoscroll="true"
and autoscroll="false"
, but it didn't make a difference.
我也试过给 HTML
和体
元素高度:100%
,我也尝试过没有。但是什么都没有。
I also tried giving the html
and the body
elements height:100%
, and I also tried it without. But nothing.
我真的不知道下一步该怎么做。
I really don't know what to do next.
我无法重现该问题,但如果你认为有任何code,我应该在这里发布,可能是任何帮助,我会很高兴这样做。
I wasn't able to reproduce the problem, but if you think there is any code I should post here that could be of any help, I'll be glad to do that.
谢谢!
编辑:
我已经运行在控制台上这个功能:
I've run this function on the console:
var l = $('*').length;
for(var i = 0; i < l; i++) {
var elem = $('*:eq(' + i + ')');
if(elem.scrollTop() > 0) {
console.log(elem, elem.scrollTop());
}
}
函数打印出来只有一个元素与它的顶部滚动。结果
该元素是一个包装的div持有的全部内容和主要观点(我已经嵌套在我的应用程序视图)。结果
如果我使用 scrollTop的(0)
此元素上我得到了我想要的东西,但它仅与症状的交易,而不是真正的问题。
The function printed out only one element with it's top scroll.
The element is a wrapping div that holds the whole content and the main view (I have nested views in my app).
If I use scrollTop(0)
on this element I get what I wanted, but it only deals with the symptom, and not the real problem.
推荐答案
由于@Hans评论,还有本来就不是一个问题。
我有一个包装元素,它被定位绝对
与
As @Hans commented, there wasn't actually a problem.
I had a wrapping element, that was positioned absolute
with:
top:0;
bottom:0;
left:0;
right:0;
overflow:auto
所以,窗口的 scrollTop的
总是0,并且滚动条实际上属于所述缠绕元件。
So The window's scrollTop
was always 0, and the scrollbar actually belonged to the wrapping element.
由于我便无法摆脱缠绕元件的定位,我用UI路由器的 $ stateChangeSuccess
事件,并手动滚动缠绕元件的顶端。
Since I could't get rid of the wrapping element's positioning, I used ui-router's $stateChangeSuccess
event, and manually scrolled the wrapping element to the top.
这篇关于jQuery的scrollTop的()的所有元素,则返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!