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

标签 javascript jquery html

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

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

我在页面的几个地方使用了插件 mCustomScrollbar,我认为它可能会导致问题。我无法禁用它,因为页面包含需要它看起来不错的元素。

我的代码:

(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() 具有相同的效果。

最佳答案

请使用

document

代替窗口 例如:

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

关于javascript - 使用 jQuery 获取文档的实际高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304881/

相关文章:

javascript - 如何使用 jQuery 增加索引值

javascript - jQuery UI Portlet 问题(无法单击输入框)

javascript - 如何使用 jQuery 选择的插件重置表单?

html - 如何从主要内容制作带有背景的三 Angular 形

javascript - 带有文本形式替代的 Bootstrap 子菜单?

javascript - CSS 背景图像转换使视频标记缓冲区

javascript - 尝试创建轮播效果时出错

javascript - 如何将状态映射回 Vue.js AWS Amplify 身份验证页面中的组件

javascript - 使用indexOf循环删除html实体

javascript - 在表单中传递值