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

查看:30
本文介绍了如何使用Javascript获取滚动条位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 JavaScript 检测浏览器滚动条的位置,以确定当前视图在页面中的位置.我的猜测是我必须检测轨道上拇指的位置,然后拇指的高度占轨道总高度的百分比.我是否过于复杂了,还是 JavaScript 提供了比这更简单的解决方案?在代码方面有什么想法吗?

I'm trying to detect the position of the browser's 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.scrollTopelement.scrollLeft 分别获取已滚动的垂直和水平偏移量.element 可以是 document.body 如果你关心整个页面.您可以将其与 element.offsetHeight 进行比较element.offsetWidth(同样,element 可能是正文)如果您需要百分比.

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天全站免登陆