与jQuery中的“scrollTop”相反

jQuery有一个名为scrollTop的函数,可用于查找隐藏在当前页面视图上方的像素数。

我不确定为什么,但没有scrollBottom函数返回当前页面视图下方的像素数。

有没有添加此function的jQuery插件? 或者是否需要使用窗口/文档高度和scrollTop值进行精心设计的数学运算?

你可以为此制作一个非常简单的插件:

$.fn.scrollBottom = function() { return $(document).height() - this.scrollTop() - this.height(); }; 

然后在你想要的任何元素上调用它,例如:

 $(window).scrollBottom(); //how many pixels below current view $("#elem").scrollBottom(); //how many pixels below element 

也许这将有助于告诉jquery滚动到底部 ,因为实际上没有相反的function。 同样是scrollLeft的问题 – 没有scrollRight

如果要向下滚动(与“scrollTop”相反) ,请尝试使用参数的垂直位置。 像这样:

 $(document).ready(function(){ $("button").click(function(){ //position on pixeles $("body").scrollTop(1300); }); }); 

正文的顶部将是: $("body").scrollTop(0);

您可以尝试使用scrollTo插件执行以下操作:

 $.scrollTo( document.height ) 
 function scrollBottom() { return $( window ).scrollTop() + $( window ).height(); } // example usage $( '#footer' ).css( 'top', scrollBottom() - $( '#footer' ).height() ); 

当scrollTop的默认行为在传递负值时滚动到0,我做了这个处理scrollTop并模拟“scrollDown”的函数。

如果anchor_pos为负(所以它高于我当前的滚动位置),我从当前滚动位置减去它的值(因为它有一个负值,我正在使用+符号)

 function jumpToAnchor(scrollable_div_selector, anchor_selector) { anchor_pos = $(anchor_selector).position().top; //check if negative number if (anchor_pos < 0) { anchor_pos = $(scrollable_div_selector).scrollTop() + anchor_pos; //anchor_pos is negative, so i'm substracting it } $(scrollable_div_selector).scrollTop(anchor_pos); } 

这就是我计算从元素底部到文档底部的距离:

 $(document).height() - ($('#element').offset().top + $('#element').height()); 

试试这个:

 return this[0].scrollHeight - this.scrollTop() - this.height();