對 getBoundingClientRect 感到困惑

wstcegg

getBoundingClientRect()對Javascript 中的函數感到困惑。

根據官方文檔,如果const rect = getBoundingClientRect();,那麼我們有

rect.width = rect.right - rect.left;

rect.height = rect.bottom - rect.top;

這是否意味著左右位置是左包括右排,即[左,右)?否則應該是

rect.height = rect.bottom - rect.top + 1;

看看下面的5x5地圖。[inclusive, inclusive]mode 中,所有 s 的邊界矩形1應該是left=2, right=4, top=3, bottom=4,我們有

width=right-left+1=4-2+1
height=bottom-top+1=4-3+1

5x5 網格

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 1 1 1
0 0 1 1 1

一個更具體的問題:

是什麼rect.left意思?這是否意味著到客戶端容器或第ith 像素左側的距離?怎麼樣rect.right

朱利安·吉班德

這樣考慮:

在此處輸入圖像描述

如果灰色方塊代表屏幕空間,那麼紅色方塊有邊界框:

{
  x: 1, y: 1,
  width: 3, height: 3
}

這意味著它有left=1, right=4, 因此,right-left=width

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章