如何获取浏览器视口中当前显示的内容

发布于 2024-09-17 20:31:36 字数 256 浏览 5 评论 0原文

如何获取当前正在显示长文档的哪一部分的指示?

例如,如果我的 html 包含 1,000 行
1
2
3
...
999
1000

并且用户位于显示第 500 行的中间附近,那么我想得到“500\n501\n502”或类似的内容。

显然,大多数场景都会比这更复杂,但我的要求是找到浏览器视口中当前显示的文本,以便我可以显示适合当前文本的状态值。

谢谢 马丁

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

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

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.

Thanks
Martin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海夜未眠 2024-09-24 20:31:36

如果您有 jQuery,则可以使用此函数来检查 DOM 元素当前是否显示在视口中:

function isInView(elem) {

    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

If you have jQuery, you can use this function to check if a DOM element is currently shown in the viewport:

function isInView(elem) {

    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}
如日中天 2024-09-24 20:31:36

您可以从scrollTop属性获取一个以像素为单位的值:

document.body.scrollTop = 40;

要了解文档的哪一部分是可见的,您可以循环遍历(比如说)所有 p 标签,直到找到带有负滚动值的 p 标签。前面的那个是窗口顶部的那个。

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

document.body.scrollTop = 40;

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.

一袭水袖舞倾城 2024-09-24 20:31:36

我刚刚在 msdn

function isinView(oObject)
{
   var oParent = oObject.offsetParent; 
   var iOffsetTop = oObject.offsetTop;
   var iClientHeight = oParent.clientHeight;
   if (iOffsetTop > iClientHeight) {
     alert("Special Text not in view. Expand Window to put Text in View.");
   }
   else{
      alert("Special Text in View!");
   }
}

I've just seen a piece of sample code on msdn

function isinView(oObject)
{
   var oParent = oObject.offsetParent; 
   var iOffsetTop = oObject.offsetTop;
   var iClientHeight = oParent.clientHeight;
   if (iOffsetTop > iClientHeight) {
     alert("Special Text not in view. Expand Window to put Text in View.");
   }
   else{
      alert("Special Text in View!");
   }
}
难理解 2024-09-24 20:31:36

是的,有办法。我将使用 YUI 的 API 来说明我的示例。首先,您的文本必须位于某种 dom 元素中,无论是 span、div、p 还是其他任何内容,它都必须位于元素中。这里我将假设列表项

var viewPortY = YAHOO.util.Dom.getDocumentScrollTop(),
viewPortHeight = YAHOO.util.Dom.getViewportHeight(), i = 0,
// get all the dom elements that contain the text, sorry if this isn't exact, its just a rough example
items = YAHOO.util.Dom.getElementBy(null, 'li', document.getElementById('item-container')),
viewedItems = [];
    for (i = 0 ; i < items.length; i++) {
        var y = YAHOO.util.Dom.getY(items[i])
        if (y > viewPortY && y < (viewPortY + viewPortHeight)) {
           viewedItems.push(items[i])
        }
    }

所以本质上,我得到了包含您感兴趣的文本的所有dom对象。然后我循环遍历,无论谁的Y坐标在视口Y和Y + ViewPort高度之间,我都会放入一个数组中。

Yes, there is a way. I will use YUI's API to illustrate my example. First your text must be in some sort of dom element, whether its a span, div, p or anything, it must be in a element. Here I will assume list item

var viewPortY = YAHOO.util.Dom.getDocumentScrollTop(),
viewPortHeight = YAHOO.util.Dom.getViewportHeight(), i = 0,
// get all the dom elements that contain the text, sorry if this isn't exact, its just a rough example
items = YAHOO.util.Dom.getElementBy(null, 'li', document.getElementById('item-container')),
viewedItems = [];
    for (i = 0 ; i < items.length; i++) {
        var y = YAHOO.util.Dom.getY(items[i])
        if (y > viewPortY && y < (viewPortY + viewPortHeight)) {
           viewedItems.push(items[i])
        }
    }

So essentially, I get all the dom objects that contain the text your interested in. I then loop through, and whoever's Y co-ordinate is between the viewports Y and Y + ViewPort Height, I put in an array.

笔落惊风雨 2024-09-24 20:31:36

我实现了我认为更适合我的环境的解决方案:

我正在为 Android 编写代码,这样我就可以轻松地从 javascript 与 Java 类进行交互。我的实际解决方案涉及获取我感兴趣的所有标签的 offsetTop 并将偏移量传递给 java.lang.

还注册一个 onscroll 处理程序,将 window.pageYOffset 传递给同一个 Java 类。然后java类可以将每个标签的offsetToppageYOffset进行比较,以查看哪个标签位于当前视口的顶部。

I implemented what I thought was a more optimal solution for my environment:

I am writing for Android so I can easily interact with a Java class from javascript. My actual solution involved getting offsetTop of all tags I am interested in and passing the offsets to java.

Also registering an onscroll handler that passed window.pageYOffset throught to the same Java class. Then the java class can compare offsetTop of each tag with pageYOffset to see which tag is at the top of the current viewport.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文