jquery中是否可以找到从底部到当前滚动状态的距离
假设已向下滚动到网页的中间。
是否可以找到距底部的距离(以长度或像素为单位)?
就像当滚动条到达底部时,它的值是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于整个页面:
对于特定元素:
For the whole page:
For a specific element:
也试试这个:
Also try this:
调整 rkleine 的答案。我发现它实际上是这样的:
即 $(window) 表示高度而不是 $('body')
Tweaking rkleine's answer. I found it was actually, this:
i.e. $(window) for the height not $('body')
这是普通 JS 中的代码:
Here is the code in vanilla JS:
我必须进一步调整以前的答案才能使其工作:
只是将其分解:
这获取从文档底部到窗口顶部的距离。
由于窗口高度会有所不同,因此减去当前窗口高度即可得到距底部的偏移量
现在,当用户完全向下滚动页面并向上滚动时,
bottomToWinBottom = distFromBottom = 0
,它的值是距离位于窗口下方,可以很好地指示垂直位置。I had to further tweak previous answers to make it work:
Just to break it down:
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
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.