V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
click
V2EX  ›  问与答

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

  •  
  •   click · 2015-03-29 13:02:46 +08:00 · 5106 次点击
    这是一个创建于 3316 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    文档声明是<!DOCTYPE html>

    5 条回复    2015-03-29 16:59:56 +08:00
    booksmith
        1
    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
        2
    airyland  
       2015-03-29 14:22:03 +08:00
    你进入页面就把值附给一个变量了,还能是其他值么?

    window.onscroll=function(){
    console.log($(window).scrollTop();
    }
    airyland
        3
    airyland  
       2015-03-29 14:22:46 +08:00
    打太快,上面打少了一个括号。
    click
        4
    click  
    OP
       2015-03-29 15:25:47 +08:00
    @airyland 谢谢。明白了!
    booksmith
        5
    booksmith  
       2015-03-29 16:59:56 +08:00
    ~~!,我看得太大意,没注意楼主的写法...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   896 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:34 · PVG 05:34 · LAX 14:34 · JFK 17:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.