如何获得内容的当前显示在浏览器窗口 [英] How to get content currently being displayed in browser viewport

查看:132
本文介绍了如何获得内容的当前显示在浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得当前正在显示的内容很长的文档的一部分的指示?

How can I get an indication of what part of a long document is currently being displayed?

例如。如果我的HTML包含1000行结果
   1结果
   2结果
   3结果
...结果
   999结果
   1000

E.g. if my html contains 1,000 lines
1
2
3
...
999
1000

和用户显示附近的第500行的中间,然后我想获得500 \\ N501 \\ N502或者类似的东西。

and the user is near the middle showing the 500th line then I would like to get "500\n501\n502" or something like that.

显然,大多数情况下会比这更复杂,但我的要求是要找到文本目前正在显示在浏览器窗口,所以我可以显示适合当前文本的状态值。

Obviously most scenarios would be more complex than this, but my requirement is to find which text is currently being displayed in the browser viewport so I can show a status value appropriate to the current text.

谢谢
马丁

推荐答案

您可以从scrollTop的属性像素得到一个值:

You can get a value in pixels from the scrollTop property:

的document.body.scrollTop = 40;

document.body.scrollTop = 40;

要知道你的文档的一部分是可见的,你可以通过(说)的所有P标签循环,直到你找到一个带负scrollTop的值。在此之前的一个是一个在窗口的顶部。

To know what part of your document that is visible, you could loop through (say) all p-tags until you find one with a negative scrollTop value. The one before that is the one at the top of the window.

这篇关于如何获得内容的当前显示在浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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