如何获得使用Javascript滚动条的位置? [英] How to get scrollbar position with Javascript?
问题描述
我想检测使用JavaScript的浏览器滚动条来决定页面的当前视图是位置。我的猜测是,我来检测轨道上的拇指,然后以拇指为赛道的总高度的百分比的高度。我是不是过于复杂,或者不使用Javascript提供比一个简单的解决方案?任何想法code-明智?
I'm trying to detect the position of the browser scrollbar with Javascript to decide where in the page the current view is. My guess is that I have to detect where the thumb on the track is, and then the height of the thumb as a percentage of the total height of the track. Am I over-complicating it, or does Javascript offer an easier solution than that? Any ideas code-wise?
推荐答案
您可以使用 element.scrollTop
和 element.scrollLeft
来获得垂直和水平偏移量,分别已滚动。 元素
可 document.body的
如果你关心整个页面。你可以把它比作 element.offsetHeight
和 element.offsetWidth
(同样,元素
可将车身),如果你需要的百分比。
You can use element.scrollTop
and element.scrollLeft
to get the vertical and horizontal offset, respectively, that has been scrolled. element
can be document.body
if you care about the whole page. You can compare it to element.offsetHeight
and element.offsetWidth
(again, element
may be the body) if you need percentages.
这篇关于如何获得使用Javascript滚动条的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!