如何获得使用Javascript滚动条的位置? [英] How to get scrollbar position with Javascript?

查看:150
本文介绍了如何获得使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆