V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
click
V2EX  ›  问与答

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

  •  
  •   click · Mar 29, 2015 · 5600 views
    This topic created in 4053 days ago, the information mentioned may be changed or developed.

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

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

    文档声明是<!DOCTYPE html>

    5 replies    2015-03-29 16:59:56 +08:00
    booksmith
        1
    booksmith  
       Mar 29, 2015
    这是我已前遇到的

    一开始我以为是这样的原因
    > 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  
       Mar 29, 2015
    你进入页面就把值附给一个变量了,还能是其他值么?

    window.onscroll=function(){
    console.log($(window).scrollTop();
    }
    airyland
        3
    airyland  
       Mar 29, 2015
    打太快,上面打少了一个括号。
    click
        4
    click  
    OP
       Mar 29, 2015
    @airyland 谢谢。明白了!
    booksmith
        5
    booksmith  
       Mar 29, 2015
    ~~!,我看得太大意,没注意楼主的写法...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2411 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 01:46 · PVG 09:46 · LAX 18:46 · JFK 21:46
    ♥ Do have faith in what you're doing.