滚动高度如何大于窗口高度?

时间:2014-02-05 17:00:28

标签: jquery

我试图让一个函数在用户滚动整个窗口高度之前停止运行,但我在控制台中看到的并没有加起来。不知何故,scrollHeight(这是scrollTop值)最终不仅仅是窗口高度本身。这怎么可能?

$(document).ready(function() {

    var windowHeight = $(window).height();

    if( $(window).width() > 720  && $(window).scrollTop() < (windowHeight-400) ) {

                $(window).bind('scroll', function(){

                var scrollHeight = $(window).scrollTop();
                console.log('scroll height', scrollHeight)
                console.log('window height', windowHeight)

                function parallaxScroll(){
                   $('#main').css('top',(500-(scrollHeight*2))+'px');
                };

                    parallaxScroll();
                });
    }
});

1 个答案:

答案 0 :(得分:2)

查看documentation from jQuery,您将获得视口的高度,这意味着您只能获得视图中的内容。您需要将高度设置为文档高度,如下所示:

$(document).ready(function() {

    var windowHeight = $(document).height();

    if( $(window).width() > 720  && $(window).scrollTop() < (windowHeight-400) ) {

                $(window).bind('scroll', function(){

                var scrollHeight = $(window).scrollTop();
                console.log('scroll height', scrollHeight)
                console.log('window height', windowHeight)

                function parallaxScroll(){
                   $('#main').css('top',(500-(scrollHeight*2))+'px');
                };

                    parallaxScroll();
                });
    }
});