【问题标题】:Is there a way to access whether a portion of a webpage is being rendered on screen or not via Javascript?有没有办法通过Javascript访问网页的一部分是否正在屏幕上呈现?
【发布时间】:2016-09-02 19:11:42
【问题描述】:

目前,我们的逻辑在客户端页面上的 iframe 中运行。我们现在请求检测该 iFrame 当前是否在查看窗口中(滚动到屏幕外)。

我只是想到这可能是 GPU 可能已经拥有的东西,这将允许它在页面上提供更好的渲染性能,我想知道是否有人知道这是否数据可通过 API/lib(如 OpenGL)访问。

我知道提供实际的像素信息可能是一个很大的安全问题,但是仅访问是否正在呈现特定 iFrame 将有助于可见性检测。

有人知道是否有办法做到这一点吗?到目前为止,目前的阅读并没有表明这是可能的,但我正在继续我的搜索,并认为我也会在这里发布。

任何信息或方向都会有所帮助。谢谢!

【问题讨论】:

    标签: javascript iframe gpu


    【解决方案1】:

    您可以计算对象是否在您的视口内。

    http://plnkr.co/edit/91gybbZ7sYVELEYvy1IK?p=preview

    $(document).ready(function() {
      myElement = $('.someObject');
      window.onscroll = function() {
    
        var screenTop = $(window).scrollTop();
        var screenBottom = screenTop + $(window).height();    
        var elTop = $(myElement).offset().top;
        var elBottom = elTop + $(myElement).height();
    
        var info = 'screenTop:' + screenTop + ' screenBottom:' + screenBottom + ' elTop:' + elTop + ' elBottom:' + elBottom;
    
        if((elBottom <= screenBottom) && (elTop >= screenTop)) {
          $('.info').html('Element is in view + <small>' + info + '</small>');
        } else {
          $('.info').html('Element is out of view + <small>' + info + '</small>');
        }
    
      }
    })
    

    【讨论】:

    • 感谢格弗里德的回复。不幸的是,在另一个域的 iFrame 中工作时,当您返回 null 时,似乎无法访问父窗口元素。
    • 确实如此 - 如果 iframe 来自另一个来源(域),则无法访问其内容。
    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 2016-11-02
    • 2014-09-19
    • 2016-12-28
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多