最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When does Element.getClientRects() return a collection of multiple objects? - Stack Overflow

programmeradmin6浏览0评论

Each time when I call Element.getClientRects(), it returns a collection of only one DOMRect object.

When does Element.getClientRects() return a collection of multiple DOMRect objects?

function handleClick() {
  console.log(event.target.getClientRects())
}
<ul 
  style="border: 1px solid black;" 
  onclick="handleClick()"
>
    <li>Click the text to see in console</li>
</ul>

Each time when I call Element.getClientRects(), it returns a collection of only one DOMRect object.

When does Element.getClientRects() return a collection of multiple DOMRect objects?

function handleClick() {
  console.log(event.target.getClientRects())
}
<ul 
  style="border: 1px solid black;" 
  onclick="handleClick()"
>
    <li>Click the text to see in console</li>
</ul>

Share Improve this question edited Dec 11, 2018 at 8:12 vrintle 5,6063 gold badges18 silver badges47 bronze badges asked Dec 8, 2018 at 17:09 Roman RomanRoman Roman 9371 gold badge9 silver badges29 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 10

The return value of Element.getClientRects() method is a collection of DOMRect objects, each associated with one CSS border-box around an element.

When elements have multiple border-boxes (like inline-elements), then Element.getClientRects() returns multiple DOMRect objects. An example:

let p = document.querySelector('p');
let span = document.querySelector('span');

console.log(p.getClientRects().length);
console.log(span.getClientRects().length);
p {
  border: 1px solid green;
}
span {
  border: 1px solid red;
}
<p>
  This is a paragraph with
  <span>Span Element having a looooooooooooooooooooooo nnnnnnnnnnnnnnnnnnnn ggggggggggggggggg text</span>
</p>

Also, the return value is dependant on the screen resolution. Smaller the size, smaller will be the number of CSS border-boxes.

发布评论

评论列表(0)

  1. 暂无评论