모ëí° íŹę¸° : window.screen.width
(height)
ë¸ëźě°ě íŹę¸° : window.outerWidth
(outerHeight)
(ě¤íŹëĄ¤ íŹí¨) ěš íě´ě§ íŹę¸° : window.innerWidth
(innerHeight)
(ě¤íŹëĄ¤ 미íŹí¨) ěš íě´ě§ íŹę¸° : document.documentElement.clientWidth
(clientHeight)
const all = document.querySelector('.all');
function changeSize () {
all.innerHTML = `
window.screen: ${screen.width}, ${screen.height}<br/ >
window.outer: ${outerWidth}, ${outerHeight}<br/ >
window.inner: ${this.innerWidth}, ${innerHeight}<br/ >
documentElement.clientWidth: ${document.documentElement.clientWidth}, ${document.documentElement.clientHeight}`;
}
changeSize();
window.addEventListener('resize', () => changeSize());
element.getBoundingClientRect()
DOMRect ę°ě˛´ëĽź ë°ííë¤. ěŹę¸°ěë ěěě íŹę¸°ě í¨ęť 롰íŹí¸ëĽź 기ě¤ěźëĄ íë 'ěě'ě ěëě ě¸ ěěš
ě ëí ě ëł´ę° ë´ę˛¨ ěë¤
top, left, bottom, right ę°ě ę°ëë¤. ě´ë bottomęłź rightě 'ěě'ě bottomęłź rightě ě미í늰, CSSěěě bottom, rightě´ ě미íë ë°ě ë¤ëĽ´ë¤.
DOMRect ę°ě˛´ě bottom, right ę°ě ë¸ëźě°ě ě ěźěŞ˝ęłź ě쪽ě 기ě¤ěźëĄ í´ëš ěěę° ěźë§íź ë¨ě´ě ¸ ěëě§ëĽź ęłě°í´ ꡸ 깰댏ë§íźě ę°ě ę°ě§ë¤.
mouseEvent.clientX(Y)
'ë§ě°ě¤ ě´ë˛¤í¸(í´ëŚ, ëë¸í´ëŚ ëą)ę° ë°ěí ęłł'ě´ ë¸ëźě°ě
ě Xěś, YěśěźëĄëśí° ěźë§ë ë¨ě´ě ¸ ěëę°
mouseEvent.pageX(Y)
'ë§ě°ě¤ ě´ë˛¤í¸(í´ëŚ, ëë¸í´ëŚ ëą)ę° ë°ěí ęłł'ě´ ëŹ¸ěę° ěěíë
Xěś, YěśěźëĄëśí° ěźë§ë ë¨ě´ě ¸ ěëę°
const box_7 = document.querySelector('.box-7');
box_7.addEventListener('click', event => {
console.log(box_7.getBoundingClientRect()); // ë¸ëźě°ě 기ě¤, ěěě ěěš (í´ëŚí ě§ě ě ěěš x)
console.log(`offset: ${event.offsetX}. ${event.offsetY}`) // í´ëŚí ěě 기ě¤, í´ëŚí ě§ě ě ěěš
console.log(`screen: ${event.screenX}, ${event.screenY}`); // 모ëí° ę¸°ě¤, í´ëŚí ě§ě ě ěěš
console.log(`client: ${event.clientX}, ${event.clientY}`); // ë¸ëźě°ě 기ě¤, í´ëŚí ě§ě ě ěěš
console.log(`page: ${event.pageX}, ${event.pageY}`); // 돸ě 기ě¤, í´ëŚí ě§ě ě ěěš
});
const by = document.querySelector('.by'); // by ë˛íź
const to = document.querySelector('.to'); // to ë˛íź
const into = document.querySelector('.into'); // into ë˛íź
by.addEventListener('click', () => scrollBy({top: 100, left: 0, behavior: 'smooth'}));
// yěśěźëĄ 100pxë§íź ëśëë˝ę˛ ě´ë
to.addEventListener('click', () => scrollTo(0, 100));
// yěśěźëĄëśí° 100px ě§ě ěźëĄ íë˛ě ě´ë
into.addEventListener('click', () => box_7.scrollIntoView({behavior: 'smooth', block: 'end', inline: 'nearest'}));
// box_7 ěěëĄ ě´ë â behavior: ëśëë˝ę˛ ě´ë, block: ěě§ ě ë Ź, inline: ěí ě ë Ź
position: absolute left(top)
ę° = clientX(Y)
ę°đ HTML
<html>
<body>
<div class="line horizon"></div>
<div class="line vertical"></div>
<img src="img/target.png" class="circle">
<span class="coord"></span>
</body>
</html>
đ CSS
body {
background-color: black;
}
.line {
background-color: white;
position: absolute;
}
.horizon {
width: 100%;
height: 1px;
top: 50%;
}
.vertical {
width: 1px;
height: 100%;
left: 50%;
}
.circle {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* ěŹę¸°ě 50%ë í´ëš ěěě íŹę¸°ě 50%뼟 ë§íë¤ */
}
.coord {
color: white;
position: absolute;
left: 50%;
top: 50%;
transform: translate(30px, 30px);
font-size: 30px;
}
đ javascript
const horizon = document.querySelector(".horizon");
const vertical = document.querySelector(".vertical");
const circle = document.querySelector(".circle");
const coord = document.querySelector(".coord");
document.addEventListener("mousemove", (event) => {
console.log(event.clientX, event.clientY);
const x = event.clientX;
const y = event.clientY;
horizon.style.top = `${y}px`; // ěíě ě top ę°ë§
vertical.style.left = `${x}px`; // ěě§ě ě left ę°ë§
circle.style.left = `${x}px`;
circle.style.top = `${y}px`;
coord.style.left = `${x}px`;
coord.style.top = `${y}px`;
coord.innerHTML = `${x}px, ${y}px`;
});