LoginSignup
0
1

More than 3 years have passed since last update.

getBoundingClientRect を使って要素の座標情報を取得

Last updated at Posted at 2021-01-25

概要

getBoundingClientRectの情報を見てみる

let image = (<HTMLAnchorElement>document.getElementById("image"));
let image_domRect = image.getBoundingClientRect();
console.log(image_domRect)

上述のコードを実行するとConsoleに下記の情報が表示される。

DOMRect {x: 677.484375, y: 269, width: 143.015625, height: 15, top: 269, …}
bottom: 284
height: 15
left: 677.484375
right: 820.5
top: 269
width: 143.015625
x: 677.484375
y: 269
__proto__: DOMRect

使用例

image_dialogというダイアログがあるとする。
前述の座標情報をもとに、ダイアログが表示される位置を指定することができる。

image_dialog.style.top = (image_domRect.top + 20) + 'px';
image_dialog.style.left = Math.abs(image_domRect.left - image_domRect.width - 100) + 'px';
$(image_dialog).show();
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1