jquery中是否可以找到从底部到当前滚动状态的距离

发布于 2024-08-31 12:39:44 字数 122 浏览 6 评论 0原文

假设已向下滚动到网页的中间。

是否可以找到距底部的距离(以长度或像素为单位)?

就像当滚动条到达底部时,它的值是 0,但如果它距离底部 500px,那么我需要那个 500px 值。

Suppose have scrolled down to the middle of a web page.

Is it possible to find the distance, in length or pixels, from the bottom?

Like when scroll-bar hits the bottom then its 0 but if it 500px from the bottom then I need that 500px value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

指尖上的星空 2024-09-07 12:39:44

对于整个页面:

let scrollPosition = window.pageYOffset;
let windowSize     = window.innerHeight;
let bodyHeight     = document.body.offsetHeight;

alert(Math.max(bodyHeight - (scrollPosition + windowSize), 0));

对于特定元素:

let scrollDistanceTotal      = el.scrollHeight - el.clientHeight;
let scrollDistanceFromBottom = scrollDistanceTotal - el.scrollTop;

For the whole page:

let scrollPosition = window.pageYOffset;
let windowSize     = window.innerHeight;
let bodyHeight     = document.body.offsetHeight;

alert(Math.max(bodyHeight - (scrollPosition + windowSize), 0));

For a specific element:

let scrollDistanceTotal      = el.scrollHeight - el.clientHeight;
let scrollDistanceFromBottom = scrollDistanceTotal - el.scrollTop;
铃予 2024-09-07 12:39:44

也试试这个:

$(document).height() - ($('body').height() + $('body').scrollTop())

Also try this:

$(document).height() - ($('body').height() + $('body').scrollTop())
谁许谁一生繁华 2024-09-07 12:39:44

调整 rkleine 的答案。我发现它实际上是这样的:

$(document).height() - ($(window).height() + $('body').scrollTop())

即 $(window) 表示高度而不是 $('body')

Tweaking rkleine's answer. I found it was actually, this:

$(document).height() - ($(window).height() + $('body').scrollTop())

i.e. $(window) for the height not $('body')

執念 2024-09-07 12:39:44

这是普通 JS 中的代码:

var distanceFromBottom = document.body.scrollHeight - window.innerHeight - window.scrollY

Here is the code in vanilla JS:

var distanceFromBottom = document.body.scrollHeight - window.innerHeight - window.scrollY
神经大条 2024-09-07 12:39:44
var distanceFromBottom = $(document).height() - $(window).height() - $(document).scrollTop();
var distanceFromBottom = $(document).height() - $(window).height() - $(document).scrollTop();
森罗 2024-09-07 12:39:44

我必须进一步调整以前的答案才能使其工作:

distFromBottom = $(document).height() - $(window).scrollTop() - $(window).height();

只是将其分解:

let bottomToWinTop = $(document).height() - $(window).scrollTop();

这获取从文档底部到窗口顶部的距离。
由于窗口高度会有所不同,因此减去当前窗口高度即可得到距底部的偏移量

let bottomToWinBottom = bottomToWinTop - $(window).height();

现在,当用户完全向下滚动页面并向上滚动时,bottomToWinBottom = distFromBottom = 0,它的值是距离位于窗口下方,可以很好地指示垂直位置。

I had to further tweak previous answers to make it work:

distFromBottom = $(document).height() - $(window).scrollTop() - $(window).height();

Just to break it down:

let bottomToWinTop = $(document).height() - $(window).scrollTop();

This gets the distance from the bottom of document to the top of the window.
Since window height will vary, subtract the current window height to get the offset from the bottom

let bottomToWinBottom = bottomToWinTop - $(window).height();

Now bottomToWinBottom = distFromBottom = 0 when user is scrolled completely down the page and as they scroll up, it's value is the distance below the window which makes for a good indicator of vertical positioning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文