css
* { margin: 0; padding: 0; box-sizing: border-box; }
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
text-align: center;
background: rgba(0, 0, 0, .8);
transition: all .5s;
}
header h2 {
color: #bbb;
line-height: 80px;
border-bottom: 1px solid #aaa;
}
header div { color: #fff; line-height: 50px; }
header div span { color: red; font-weight: bold; }
header.scr {
transform: translateY(-80px);
background: rgba(255, 255, 255, .8);
}
header.scr div { color:#000; }
section {}
section div {
height: 500px;
font-size: 5em;
display: flex;
justify-content: center;
align-items: center;
background-color: powderblue;
}
section div:nth-child(2n) { background: pink; } /* odd : ํ์ | even : ์ง์ */
html
<header>
<h2>scroll</h2>
<div>scroll ๊ฐ</div>
</header>
<section>
<div>01</div>
<div>02</div>
<div>03</div>
<div>04</div>
<div>05</div>
</section>
js
const elHeader = document.querySelector("header");
const headerDiv = document.querySelector("header div");
// ์คํฌ๋กค์ ๋ด๋ฆด ๋ - h2๊ฐ ์๋ณด์
// ์คํฌ๋กค์ ์ฌ๋ฆด ๋ - h2๊ฐ ๋ณด์
// ์คํฌ๋กค ๋ด๋ฆด ๋/์ฌ๋ฆด ๋๋ฅผ ๊ตฌ๋ถํด์ผ ํจ
let position = {
y: 0,
oy: 0,
status: true,
} // ํ์ฌy, ๊ณผ๊ฑฐy, y / oy ๋น๊ต๊ฐ (boolean. true-๋ด๋ฆผ / false-์ฌ๋ฆผ)
// window.addEventListener("scroll") // method
window.onscroll = function() { // property
position.y = window.scrollY; // ํ์ฌ ์คํฌ๋กค ๊ฐ
// console.log(window.scrollY);
position.status = position.oy < position.y;
// ํ์ฌ ์คํฌ๋กค ๊ฐ์ด ๊ณผ๊ฑฐ ์คํฌ๋กค ๊ฐ๋ณด๋ค ์ปธ์ ๋ true
position.oy = position.y; // ํ์ฌ ์คํฌ๋กค ๊ฐ์ ๊ณผ๊ฑฐ ์คํฌ๋กค ๊ฐ์ ๋์
headerDiv.innerHTML = `
์คํฌ๋กค ํ์ฌ ๊ฐ <span>${position.y}</span> /
์คํฌ๋กค ์ ์ฒด ๊ธธ์ด <span>${document.scrollingElement.scrollHeight}</span> /
๋ธ๋ผ์ฐ์ ์ ๋๋น1 <span>${window.innerWidth}</span> /
์คํฌ๋กค์ด ์๋๋ก ๋ด๋ ค์ค๋ ์ค์ธ๊ฐ? <span>${position.status}</span>
`;
if (position.status) {
elHeader.classList.add("scr");
} else {
elHeader.classList.remove("scr");
}
};
/*
window.scrollY - ๋ฌธ์๊ฐ ์์ง์ผ๋ก ์ผ๋ง๋ ์คํฌ๋กค ๋์๋์ง ํฝ์
๋จ์๋ก ๋ฐํ
window.innerWidth - ๋ทฐํฌํธ์ ๋๋น๋ฅผ ๋ฐํ
*/
์์ฑ์์
css
* { margin: 0; padding: 0; box-sizing: border-box; }
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
text-align: center;
background: rgba(0, 0, 0, .8);
transition: all .5s;
}
header h2 {
color: #bbb;
line-height: 80px;
border-bottom: 1px solid #aaa;
}
header div { color: #fff; line-height: 50px; }
header div span { color: red; font-weight: bold; }
header.scr {
transform: translateY(-80px);
background: rgba(255, 255, 255, .8);
}
header.scr div { color:#000; }
section {}
section div {
height: 500px;
font-size: 5em;
display: flex;
justify-content: center;
align-items: center;
background-color: powderblue;
}
section div:nth-child(2n) { background: pink; } /* odd : ํ์ | even : ์ง์ */
html
<header>
<h2>๋ง์ฐ์ค์ ์์น</h2>
<div>
screenX : <span id="scrx">0</span>
screenY : <span id="scry">0</span> |
pageX : <span id="pagex">0</span>
pageY : <span id="pagey">0</span> |
clientX : <span id="clx">0</span>
clientY : <span id="cly">0</span>
</div>
</header>
<section>
<div>
- screenX, screenY : ์คํฌ๋ฆฐ(๋ชจ๋ํฐ) ๊ธฐ์ค ์์น๊ฐ <br>
- pageX, pageY : ์ด๋ ค์๋ ํ์ด์ง(๋ธ๋ผ์ฐ์ ) ๊ธฐ์ค ์์น๊ฐ. ์คํฌ๋กค ๋ ๋ถ๋ถ๋ ํฌํจ <br>
- clientX, clientY : ๋์ ๋ณด์ด๋ ํ์ด์ง(๋ธ๋ผ์ฐ์ )์์ญ ๊ธฐ์ค ์์น๊ฐ
</div>
<div>02</div>
<div>03</div>
<div>04</div>
<div>05</div>
</section>
js
const elHeader = document.querySelector("header");
const headerDiv = document.querySelector("header div");
function mousePos(event) {
scrx.innerText = event.screenX;
scry.innerText = event.screenY;
pagex.innerText = event.pageX;
pagey.innerText = event.pageY;
clx.innerText = event.clientX;
cly.innerText = event.clientY;
}
window.addEventListener("mousemove", mousePos);