使用 javascript 获取 div 滚动位置。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7316482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:43:38  来源:igfitidea点击:

Getting a div scroll position using javascript.

javascriptscroll

提问by Swaroop

I have one button in a div tag. That div tag also has a vertical scroll bar.

我在 div 标签中有一个按钮。该 div 标签还有一个垂直滚动条。

If I scroll that bar and click on the button which is inside that div, it should return the scroll position as an alert message.

如果我滚动该栏并单击该 div 内的按钮,它应该将滚动位置作为警报消息返回。

How to do that using JavaScript?

如何使用 JavaScript 做到这一点?

回答by James Allardice

The scrollTopproperty tells you the vertical scroll position:

scrollTop属性告诉您垂直滚动位置:

document.getElementById("yourButton").onclick = function() {
   alert(document.getElementById("yourDiv").scrollTop); 
}