为啥我用 jQuery scrollTop()方法获取的滚动条高度都是 0 ?

2015-03-29 13:02:46 +08:00
 click

var scrollHeight=$(window).scrollTop();

window.onscroll=function(){
console.log(scrollHeight);
}

文档声明是<!DOCTYPE html>

5110 次点击
所在节点    问与答
5 条回复
booksmith
2015-03-29 13:08:59 +08:00
这是我已前遇到的

一开始我以为是这样的原因
> i think i find the anwser
> at win7 chrome33,
> $('body').scrollTop() will return correct value, and $('html').scrollTop() will return error value
> but at fedora20 chrome33 , $('body').scrollTop() will return 0 , and $('html').scrollTop() will return > > > correct value
>
> ps:
> at win7 chromium 35, chrome 35.0.1853.2 canary
> $('body').scrollTop() will return 0 , and $('html').scrollTop() will return correct value

后来再仔细找时,才发现是这个原因。

> It's my problem. my chrome use the features : Enable experimental Web Platform features at chrome://flags/ . And then your plugin doesnt work. when i disable it , everything is becoming ok.
> the chromium and canary default is enable this features , so they doesnt work too.

你自己看看是不是这个问题。
airyland
2015-03-29 14:22:03 +08:00
你进入页面就把值附给一个变量了,还能是其他值么?

window.onscroll=function(){
console.log($(window).scrollTop();
}
airyland
2015-03-29 14:22:46 +08:00
打太快,上面打少了一个括号。
click
2015-03-29 15:25:47 +08:00
@airyland 谢谢。明白了!
booksmith
2015-03-29 16:59:56 +08:00
~~!,我看得太大意,没注意楼主的写法...

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/180090

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX