与 jQuery 中的“scrollTop”相反
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4188903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opposite of "scrollTop" in jQuery
提问by Zachary Wright
jQuery has a function called scrollTop which can be used to find the number of pixels hidden above the current page view.
jQuery 有一个名为 scrollTop 的函数,可用于查找隐藏在当前页面视图上方的像素数。
I'm not really sure why, but there is no scrollBottom function which returns the number of pixels below the current page view.
我不太确定为什么,但是没有 scrollBottom 函数可以返回当前页面视图下方的像素数。
Is there a jQuery plugin which adds this functionality? Or is it going to require some elaborate math with the window/document height and the scrollTop value?
是否有添加此功能的 jQuery 插件?还是需要对窗口/文档高度和 scrollTop 值进行一些复杂的数学计算?
回答by Nick Craver
You could make a pretty simple plugin for this:
您可以为此制作一个非常简单的插件:
$.fn.scrollBottom = function() {
return $(document).height() - this.scrollTop() - this.height();
};
Then call it on whatever element you wanted, for example:
然后在您想要的任何元素上调用它,例如:
$(window).scrollBottom(); //how many pixels below current view
$("#elem").scrollBottom(); //how many pixels below element
回答by VKolev
Perhaps this will help how to tell jquery to scroll to bottom, because there is really no opposite function. Same is the problem with scrollLeft - there is no scrollRight
也许这将有助于如何告诉 jquery 滚动到底部,因为确实没有相反的功能。scrollLeft 也有同样的问题 - 没有 scrollRight
回答by Carlos Román
If you want to Scroll Down (Opposite of “scrollTop”), try to use the vertical position on the argument. Like this:
如果要向下滚动(与“scrollTop”相反),请尝试使用参数的垂直位置。像这样:
$(document).ready(function(){
$("button").click(function(){
//position on pixeles
$("body").scrollTop(1300);
});
});
The top of the body, will be: $("body").scrollTop(0);
身体的顶部,将是: $("body").scrollTop(0);
回答by Jirair He
try this:
尝试这个:
return this[0].scrollHeight - this.scrollTop() - this.height();
回答by Fernando Rybka
回答by rushkeldon
function scrollBottom()
{
return $( window ).scrollTop() + $( window ).height();
}
// example usage
$( '#footer' ).css( 'top', scrollBottom() - $( '#footer' ).height() );
回答by SaganTheBest
As scrollTop's default behaviour scrolls to 0 when passing a negative value, I did this function that handles scrollTop and simulate a "scrollDown".
由于 scrollTop 的默认行为在传递负值时滚动到 0,因此我执行了此函数来处理 scrollTop 并模拟“scrollDown”。
If anchor_posis negative (so it's above my current scroll position), I subtract its value from current scroll position (as it has a negative value, I'm using + sign)
如果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);
}
回答by Alex W
This is how I have calculated the distance from the bottom of the element to the bottom of the document:
这就是我如何计算从元素底部到文档底部的距离:
$(document).height() - ($('#element').offset().top + $('#element').height());