当视口高度与窗口scrollTop位置一起添加时,Jquery $(document.body).height()与文档末尾不同

时间:2014-06-29 14:42:54

标签: javascript jquery html css scrolltop

当我向下滚动到文档末尾时,文档高度是如何小于窗口滚动顶部值+视口高度的。它们不应该是一样的吗?我在最近几个小时里一直在努力,但仍然没有掌握它。

$(function(){               
            vpw = parseFloat($(window).width());
            vph = parseFloat($(window).height());
            appearh = parseFloat(vph*0.4);
            dh  = $(document).height();
            footerh = $('#footer-area').height();
            footTop = dh - footerh;
            resizeDiv(vpw, vph, appearh);


            $(window).scroll(function(){
                scrollPos = $(window).scrollTop();
                jj = vph + scrollPos;
                console.log(scrollPos + '+' + vph + '=' + jj + ' is (at the bottom) ' + dh);
                if(scrollPos > appearh){
                    addWin = parseFloat(dh - vph);
                    $('#trends').removeClass('hidetrends',2000).addClass('showtrends',2000);
                    /*console.log( dh + '>' + scrollPos + ';' + addWin );
                    if(scrollPos >= 1672){
                        $('#trends').css('position', 'relative');
                    }else if(scrollPos <= 1672){
                        $('#trends').css('position', 'fixed');
                    }*/
                }else{
                    $('#trends').removeClass('showtrends',2000).addClass('hidetrends',2000);
                }

            });
        });
        window.onresize = function(event) {
            resizeDiv(vpw, vph, appearh);
        }
        function resizeDiv(vpw, vph, appearh) {
            $("#full-width").css({"height": vph + "px"});
        }

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个:

$(window).scroll(function(){
     vpw = parseFloat($(window).width());
         vph = parseFloat($(window).height());
         appearh = parseFloat(vph*0.4);
         dh  = $(document).height();
         footerh = $('#footer-area').height();
         footTop = dh - footerh;
         resizeDiv(vpw, vph, appearh);
         scrollPos = $(window).scrollTop();
         jj = vph + scrollPos;
         console.log(scrollPos + '+' + vph + '=' + jj + ' is (at the bottom) ' + dh);
....

这背后的原因是我认为在滚动时实际设置变量是明智的