我有一个问题与我的jQuery插件,我想以特定的方式显示页脚之前的页面结束。为此,我得到了实际的底部位置,并将其与整个身体的高度进行比较。
但是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(),效果相同。
发布于 2016-10-15 21:09:41
请使用
文档
例如,代替window:
高度$(窗口).resize(
(){=$(文档).height();});
https://stackoverflow.com/questions/38304881
复制相似问题