使用jquery获取元素相对于视口的位置

DA.*_*DA. 115 javascript jquery position viewport offset

获取页面上元素相对于视口(而不是文档)的位置的正确方法是什么.jQuery.offset功能似乎有希望:

获取第一个元素的当前坐标,或者在匹配元素集中相对于文档设置每个元素的坐标.

但这与文件有关.是否存在相对于视口返回偏移量的等效方法?

Igo*_* G. 281

确定元素大小和位置的最简单方法是调用其 getBoundingClientRect()方法.此方法返回视口坐标中的元素位置.它不需要参数,并返回一个具有left,right,topbottom属性的对象.left和top属性给出元素左上角的X和Y坐标,右侧和底部属性给出右下角的坐标.

element.getBoundingClientRect(); // Get position in viewport coordinates

到处支持.

  • 这显然是这个问题的最佳答案. (41认同)
  • 很好的答案,并使其成为jquery只需这样做:`$('#myElement')[0] .getBoundingClientRect().top`(或任何其他位置) (25认同)
  • IE5增加了这种方法令人难以置信......当事情好的时候,很好! (15认同)
  • 最新的Firefox不支持`getBoundingClientRect不是函数` (2认同)
  • @ user007我确认它是由最新的Firefox支持的. (2认同)

Cor*_*lou 40

这里有两个函数来获取页面高度和滚动量(x,y)而不使用(膨胀)维度插件:

// getPageScroll() by quirksmode.com
function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;
    }
    return new Array(xScroll,yScroll)
}

// Adapted from getPageSize() by quirksmode.com
function getPageHeight() {
    var windowHeight
    if (self.innerHeight) { // all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }
    return windowHeight
}
Run Code Online (Sandbox Code Playgroud)


Age*_*191 26

查看Dimensions插件,特别是scrollTop()/ scrollLeft().有关信息,请访问http://api.jquery.com/scrollTop.

  • 维度插件现在是jQuery核心的一部分.ViewPort插件也很有用:http://www.appelsiini.net/projects/viewport (16认同)
  • 我不想使用另一个插件,但是$(window).scrollTop()正是我需要的!谢谢! (8认同)

Sal*_*n A 21

jQuery.offset需要结合使用scrollTop,scrollLeft如下图所示:

视口滚动和元素偏移

演示:

function getViewportOffset($e) {
  var $window = $(window),
    scrollLeft = $window.scrollLeft(),
    scrollTop = $window.scrollTop(),
    offset = $e.offset(),
    rect1 = { x1: scrollLeft, y1: scrollTop, x2: scrollLeft + $window.width(), y2: scrollTop + $window.height() },
    rect2 = { x1: offset.left, y1: offset.top, x2: offset.left + $e.width(), y2: offset.top + $e.height() };
  return {
    left: offset.left - scrollLeft,
    top: offset.top - scrollTop,
    insideViewport: rect1.x1 < rect2.x2 && rect1.x2 > rect2.x1 && rect1.y1 < rect2.y2 && rect1.y2 > rect2.y1
  };
}
$(window).on("load scroll resize", function() {
  var viewportOffset = getViewportOffset($("#element"));
  $("#log").text("left: " + viewportOffset.left + ", top: " + viewportOffset.top + ", insideViewport: " + viewportOffset.insideViewport);
});
Run Code Online (Sandbox Code Playgroud)
body { margin: 0; padding: 0; width: 1600px; height: 2048px; background-color: #CCCCCC; }
#element { width: 384px; height: 384px; margin-top: 1088px; margin-left: 768px; background-color: #99CCFF; }
#log { position: fixed; left: 0; top: 0; font: medium monospace; background-color: #EEE8AA; }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<!-- scroll right and bottom to locate the blue square -->
<div id="element"></div>
<div id="log"></div>
Run Code Online (Sandbox Code Playgroud)

  • 这对我很有用,但我用它来确定工具提示是否越界,所以我修改了包含底部和右侧的值:http://jsfiddle.net/EY6Rk/21/ (2认同)