简体   繁体   English

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

[英]How to get content currently being displayed in browser viewport

How can I get an indication of what part of a long document is currently being displayed? 如何获得当前显示长文档的哪一部分的指示?

Eg if my html contains 1,000 lines 例如,如果我的html包含1,000行
1 1
2 2
3 3
... ...
999 999
1000 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. 并且用户在显示第500行的中间附近然后我想得到“500 \\ n501 \\ n502”或类似的东西。

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 谢谢马丁

If you have jQuery, you can use this function to check if a DOM element is currently shown in the viewport: 如果你有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));
}

You can get a value in pixels from the scrollTop property: 您可以从scrollTop属性获取以像素为单位的值:

document.body.scrollTop = 40; 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. 要知道文档中哪些部分是可见的,您可以遍历(比方说)所有p-tag,直到找到具有负scrollTop值的文本。 The one before that is the one at the top of the window. 之前的那个是窗口顶部的那个。

I've just seen a piece of sample code on msdn 我刚刚在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!");
   }
}

Yes, there is a way. 是的,有一种方法。 I will use YUI's API to illustrate my example. 我将使用YUI的API来说明我的例子。 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. 首先,你的文本必须是某种dom元素,无论是span,div,p还是其他任何东西,它必须在元素中。 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. 基本上,我得到了所有包含你感兴趣的文本的dom对象。然后我循环遍历,无论Y坐标是在视口Y和Y + ViewPort Height之间,我都放入了一个数组。

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. 我正在为Android写作,所以我可以轻松地从javascript与Java类进行交互。 My actual solution involved getting offsetTop of all tags I am interested in and passing the offsets to java. 我的实际解决方案包括获取我感兴趣的所有标签的offsetTop并将偏移量传递给java。

Also registering an onscroll handler that passed window.pageYOffset throught to the same Java class. 还注册了一个将window.pageYOffset传递给同一Java类的onscroll处理程序。 Then the java class can compare offsetTop of each tag with pageYOffset to see which tag is at the top of the current viewport. 然后java类可以将每个标记的offsetToppageYOffset进行比较,以查看哪个标记位于当前视口的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM