<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
background-color: black;
}
div {
width: 250px;
height: 250px;
margin-bottom: 5px;
background-color: yellow;
border-radius: 10%;
}
.special {
background-color: salmon;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div class="special"></div>
<div></div>
<div></div>
<div></div>
<script>
const special = document.querySelector('.special');
special.addEventListener('click', event => {
const rect = special.getBoundingClientRect();
console.log(rect);
console.log(`client: ${event.clientX}, ${event.clientY}`);
console.log(`page: ${event.pageX}, ${event.pageY}`);
});
</script>
</body>
</html>
- const rect = special.getBoundingClientRect() 는
special 객체의 getBoundingClientRect를 호출해서 rect에 값을 담아두는 것.
- console.log(`client: ${event.clientX}, ${event.clientY}`);
console.log(`client: ${event.pageX}, ${event.pageY}`);는
클릭 발생 시 MouseEvent로부터 값을 가져와 출력
'Front-end > Javascript 실습' 카테고리의 다른 글
1만시간의 법칙 - 클론 HTML 코드리뷰 (0) | 2022.03.23 |
---|---|
H/T count/delete (0) | 2022.03.21 |
버튼으로 지정된 곳으로 이동하기 (0) | 2022.03.17 |
윈도우에서 마우스 커서 좌표 찾기 (0) | 2022.03.16 |
web APIs - 윈도우 스크롤링 APIs (0) | 2022.03.13 |