首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用jQuery获取文档的实际高度

使用jQuery获取文档的实际高度
EN

Stack Overflow用户
提问于 2016-07-11 18:39:47
回答 1查看 113关注 0票数 4

我有一个问题与我的jQuery插件,我想以特定的方式显示页脚之前的页面结束。为此,我得到了实际的底部位置,并将其与整个身体的高度进行比较。

但是jQuery函数height()返回错误的值有一个问题。例如,我知道站点的高度为5515px,但函数返回给我一个值: 8142px

我在页面上的几个地方使用了插件mCustomScrollbar,我认为它可能会导致问题。我不能禁用它,因为页面的元素要求它看起来很好。

我的代码:

代码语言:javascript
运行
复制
(function($){
    $.fn.scrollingElements = function(){
        var height = $(window).height();
        var max_height = $(document).height();
        var prev_top = $(window).scrollTop();
        var prev_bottom = top + height;
        $(window).scroll(function(){
            var top = $(window).scrollTop();
            var bottom = top + height;

            console.log(max_height, bottom);

            if(prev_top < height && top > height){
                // console.log('show header', 'show sticky', 'show footer small');
            }
            if(prev_top > height && top < height){
                // console.log('hide header', 'hide sticky', 'hide footer');
            }
            if(prev_top < 2*height && top > 2*height){
                // console.log('show sroll top');
            }
            if(prev_top > 2*height && top < 2*height){
                // console.log('hide scroll top');
            }

            if(prev_bottom < max_height - 100 && bottom > max_height - 100){
                // console.log('show footer large');
            }
            if(prev_bottom > max_height - 100 && bottom < max_height - 100){
                // console.log('show footer small');
            }

            prev_top = top;
            prev_bottom = bottom;

        });
        $(window).resize(function(){
            height = $(window).height();
        });
    }
})(jQuery);

对Tommy Riordan的回答:将$(document).height()更改为document.body.clientHeight不起作用,仍然得到了糟糕的结果。已尝试使用$('body').height(),效果相同。

EN

回答 1

Stack Overflow用户

发布于 2016-10-15 21:09:41

请使用

文档

例如,代替window

高度$(窗口).resize(

(){=$(文档).height();});

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38304881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档