프로젝트를 진행하면서 좌표
에 관한 공부를 많이 하게 되는 것 같은데,
평소에는 몰랐던 부분을 공부하게 되서 좋다.😄
아래 사진을 예시로 들어보자면 ①는 절대위치, ②는 Viewport를 기준으로 한 상대위치이다.
-getBoundingClientRect()
을 이용한다.
Viewport를 기준으로 상대적인 좌표를 제공해준다.
const target = document.getElementById("target");
const clientRect = target.getBoundingClientRect();
const relativeTop = clientRect.top,
relativeLeft = clientRect.left,
relativeRight = clientRect.right,
relativeBottom = clientRect.bottom;
Window.pageYOffset
-> 문서가 수직으로 얼마나 스크롤 됐는지 픽셀 단위로 반환한다.const target = document.getElementById("target");
const clientRect = target.getBoundingClientRect();
const absoluteTop = window.pageYOffset + clientRect.top,