gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:34 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com