Javascript 使用jQuery获取文档的实际高度
我的jQuery插件有问题,我想在页面结束前以特定的方式显示页脚。为此,我得到了实际的底部位置,并和整个身体的高度进行了比较 但是jQuery函数height()返回的值不正确,这是一个问题。例如,我知道站点的高度为5515px,但函数返回给我一个值:8142px 我在页面上的几个地方使用了插件mCustomScrollbar,我认为这可能会导致问题。我不能禁用它,因为页面中有一些元素要求它看起来很好 我的代码:Javascript 使用jQuery获取文档的实际高度,javascript,jquery,html,Javascript,Jquery,Html,我的jQuery插件有问题,我想在页面结束前以特定的方式显示页脚。为此,我得到了实际的底部位置,并和整个身体的高度进行了比较 但是jQuery函数height()返回的值不正确,这是一个问题。例如,我知道站点的高度为5515px,但函数返回给我一个值:8142px 我在页面上的几个地方使用了插件mCustomScrollbar,我认为这可能会导致问题。我不能禁用它,因为页面中有一些元素要求它看起来很好 我的代码: (function($){ $.fn.scrollingElements
(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);
(函数($){
$.fn.scrollingElements=函数(){
var height=$(window.height();
var max_height=$(文档).height();
var prev_top=$(窗口).scrollTop();
var prev_底部=顶部+高度;
$(窗口)。滚动(函数(){
var top=$(window.scrollTop();
var底部=顶部+高度;
控制台日志(最大高度,底部);
如果(上顶<高度和上顶>高度){
//log('show header','show sticky','show footer small');
}
如果(上顶点>高度和顶部<高度){
//log('hide header','hide sticky','hide footer');
}
如果(上顶点<2*高度和顶部>2*高度){
//log('show sroll top');
}
如果(上一个顶部>2*高度和顶部<2*高度){
//log('hide scroll top');
}
如果(上下<最大高度-100和下>最大高度-100){
//log('show footer large');
}
如果(上下>最大高度-100和下<最大高度-100){
//log('show footer small');
}
上一个顶部=顶部;
上一个底部=底部;
});
$(窗口)。调整大小(函数(){
高度=$(窗口).height();
});
}
})(jQuery);
回答Tommy Riordan的问题:
将$(document).height()更改为document.body.clientHeight不起作用,但仍然会得到错误的结果。已尝试使用$('body').height(),效果相同。请使用
文件
而不是窗口
例如:
$(窗口)。调整大小(函数(){
高度=$(文档).height();
});
为什么不直接使用
位置:固定;底部:0
在CSS中执行此操作?手动减去生成的高度…Rory,不,我不能,Bhojendra,你是什么意思?该页面有动态高度,我无法手动将其放置在那里。请尝试获取身体高度,它可能适合您。