Javascript 为什么是$(window.scrollTop();报告零?

Javascript 为什么是$(window.scrollTop();报告零?,javascript,jquery,Javascript,Jquery,我试图在$window.bind'scroll',function{}内运行一个函数;这取决于变量scrollHeight-但scrollHeight始终报告0 $(document).ready(function() { console.log('hello'); var scrollHeight = $(window).scrollTop(); $(window).bind('scroll',function(){ console.log('scr

我试图在$window.bind'scroll',function{}内运行一个函数;这取决于变量scrollHeight-但scrollHeight始终报告0

$(document).ready(function() {

    console.log('hello');
    var scrollHeight = $(window).scrollTop();

    $(window).bind('scroll',function(){
        console.log('scroll height =', scrollHeight)
    });
});
看一看小提琴,打开控制台,无论滚动多远,都可以看到scrollHeight=0

您永远不会更新滚动高度。因为它只被读取一次,所以每次启动scroll事件时都会得到该常量值

$(window).bind('scroll',function(){
    //update the value inside of the event handler
    var scrollHeight = $(window).scrollTop();
    console.log('scroll height =', scrollHeight);
});
您永远不会更新滚动高度。因为它只被读取一次,所以每次启动scroll事件时都会得到该常量值

$(window).bind('scroll',function(){
    //update the value inside of the event handler
    var scrollHeight = $(window).scrollTop();
    console.log('scroll height =', scrollHeight);
});

.2014年绑定?哦,我明白了,只是复制了OP的code@zerkms-只是与OP的帖子保持一致。2014年会怎么样?哦,我明白了,只是复制了OP的code@zerkms-只是与OP的帖子保持一致: