获取Flexbox定位元素的左侧和顶部位置

3

当使用flexbox布局元素时,我想知道它们相对于窗口的顶部和左侧是什么。

这个可能吗?

使用window.getComputedStyle返回topleft属性都是auto

const cells = document.querySelectorAll('.cell')

cells.forEach(cell => {
  cell.addEventListener('click', function(event) {
    const cell = event.target;
    const left = getComputedStyle(cell).left;
    const top = getComputedStyle(cell).top;
    document.getElementById('display').innerHTML = 'Left: ' + left + ' - Top: ' + top;
  });
})
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 50vh;
  margin: 0 auto;
  justify-content: center;
  align-items: center;
}

.container .row {
  display: flex;
}

:root {
  --cell-size: 20px;
}

.container .row .cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background-color: steelblue;
  border: 1px #ccc solid;
  border-radius: 100%;
  margin: calc(var(--cell-size) / 4);
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>CSS Animation demo</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="container">
    <div>Click a flex child to see its computed top and left</div>
    <div id="display">&nbsp;</div>
    <div class="row">
      <div class="cell"></div>
      <div class="cell"></div>
      <div class="cell"></div>
      <div class="cell"></div>
      <div class="cell"></div>
      <div class="cell"></div>
    </div>
  </div>
</body>
</html>


你无法获取计算样式,因为left和top只有在元素绝对定位时才会被计算。 - David Gomez
部分重复 https://dev59.com/Q3RC5IYBdhLWcg3wAcM3 - 讲解了如何获取元素的左右偏移量,但没有讲述 "仅限flex元素" 的条件。 - TylerH
1个回答

4

您需要使用 .offsetLeft.offsetTop

const cells = document.querySelectorAll('.cell')

cells.forEach(cell => {
  cell.addEventListener('click', function(event) {
    const cell = event.target;
    const left = cell.offsetLeft;
    const top = cell.offsetTop;
    document.getElementById('display').innerHTML = 'Left: ' + left + ' - Top: ' + top;
  });
})
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 50vh;
  margin: 0 auto;
  justify-content: center;
  align-items: center;
}

.container .row {
  display: flex;
}

:root {
  --cell-size: 20px;
}

.container .row .cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background-color: steelblue;
  border: 1px #ccc solid;
  border-radius: 100%;
  margin: calc(var(--cell-size) / 4);
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>CSS Animation demo</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="container">
    <div>Click a flex child to see its computed top and left</div>
    <div id="display">&nbsp;</div>
    <div class="row">
      <div class="cell"></div>
      <div class="cell"></div>
      <div class="cell"></div>
      <div class="cell"></div>
      <div class="cell"></div>
      <div class="cell"></div>
    </div>
  </div>
</body>
</html>


1
这些值肯定正确吗?如果我添加一个带有 position: absolute 的 div,并且将其 top 和 left 设置为 flexbox 子元素的 offsetLeftoffsetTop,它们并不完全对齐! - LondonRob
我的错:需要考虑边距! - LondonRob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接