检查元素是否在屏幕上可见

时间:2011-03-18 15:11:19

标签: javascript position offset

  

可能重复:
  jQuery - Check if element is visible after scroling

我正在尝试确定屏幕上是否有元素可见。为了达到这个目的,我试图使用offsetTop找到元素的垂直位置,但返回的值不正确。在这种情况下,除非向下滚动,否则元素不可见。但尽管如此,当我的屏幕高度为703时,offsetTop返回的值为618,因此根据offsetTop,该元素应该是可见的。

我正在使用的代码如下:

function posY(obj)
{
  var curtop = 0;

  if( obj.offsetParent )
  {
    while(1)
    {
      curtop += obj.offsetTop;

      if( !obj.offsetParent )
      {
        break;
      }

      obj = obj.offsetParent;
    }
  } else if( obj.y )
    {
     curtop += obj.y;
    }

  return curtop;
}

提前谢谢!

2 个答案:

答案 0 :(得分:107)

BenM表示,您需要检测视口的高度+滚动位置以匹配您的顶部poisiton。你使用的功能是正常的,并且可以完成工作,尽管它需要更加复杂。

如果您不使用jQuery,那么脚本将是这样的:

function posY(elm) {
    var test = elm, top = 0;

    while(!!test && test.tagName.toLowerCase() !== "body") {
        top += test.offsetTop;
        test = test.offsetParent;
    }

    return top;
}

function viewPortHeight() {
    var de = document.documentElement;

    if(!!window.innerWidth)
    { return window.innerHeight; }
    else if( de && !isNaN(de.clientHeight) )
    { return de.clientHeight; }

    return 0;
}

function scrollY() {
    if( window.pageYOffset ) { return window.pageYOffset; }
    return Math.max(document.documentElement.scrollTop, document.body.scrollTop);
}

function checkvisible( elm ) {
    var vpH = viewPortHeight(), // Viewport Height
        st = scrollY(), // Scroll Top
        y = posY(elm);

    return (y > (vpH + st));
}

使用jQuery要容易得多:

function checkVisible( elm, evalType ) {
    evalType = evalType || "visible";

    var vpH = $(window).height(), // Viewport Height
        st = $(window).scrollTop(), // Scroll Top
        y = $(elm).offset().top,
        elementHeight = $(elm).height();

    if (evalType === "visible") return ((y < (vpH + st)) && (y > (st - elementHeight)));
    if (evalType === "above") return ((y < (vpH + st)));
}

这甚至提供了第二个参数。使用“可见”(或没有第二个参数)时,它会严格检查元素是否在屏幕上。如果它设置为“高于”,当相关元素在屏幕上或屏幕上方时,它将返回true。

请参阅操作:http://jsfiddle.net/RJX5N/2/

我希望这能回答你的问题。

- 改进版本

这要短得多,也应该这样做:

function checkVisible(elm) {
  var rect = elm.getBoundingClientRect();
  var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
  return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
}

用小提琴证明:http://jsfiddle.net/t2L274ty/1/

包含thresholdmode的版本包含:

function checkVisible(elm, threshold, mode) {
  threshold = threshold || 0;
  mode = mode || 'visible';

  var rect = elm.getBoundingClientRect();
  var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
  var above = rect.bottom - threshold < 0;
  var below = rect.top - viewHeight + threshold >= 0;

  return mode === 'above' ? above : (mode === 'below' ? below : !above && !below);
}

并用小提琴证明:http://jsfiddle.net/t2L274ty/2/

答案 1 :(得分:16)

你可以使用jQuery,因为它是跨浏览器兼容的吗?

function isOnScreen(element)
{
    var curPos = element.offset();
    var curTop = curPos.top;
    var screenHeight = $(window).height();
    return (curTop > screenHeight) ? false : true;
}

然后使用以下内容调用该函数:

if(isOnScreen($('#myDivId'))) { /* Code here... */ };
相关问题