230328 - onscroll, screen

๋ฐฑ์Šน์—ฐยท2023๋…„ 3์›” 29์ผ
1

๐Ÿšฉ javascript webAPI

onscroll

๐Ÿ“ ์„ค๋ช…

  • ํŽ˜์ด์ง€์—์„œ ์Šคํฌ๋กค ์‹œ ๋™์ž‘


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

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 - ๋ทฐํฌํŠธ์˜ ๋„ˆ๋น„๋ฅผ ๋ฐ˜ํ™˜
*/

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






์ž‘์„ฑ์˜ˆ์ •

๐Ÿšฉ javascript webAPI

position ๊ด€๋ จ API

๐Ÿ“ ์„ค๋ช…

  • ํŽ˜์ด์ง€์—์„œ ์›ํ•˜๋Š” ์œ„์น˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Œ (ex. ๋งˆ์šฐ์Šค์˜ ์œ„์น˜)
  • ๋ชจ๋‹ˆํ„ฐ or ๋ธŒ๋ผ์šฐ์ € ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

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> | &nbsp;&nbsp;
    pageX : <span id="pagex">0</span>
    pageY : <span id="pagey">0</span> | &nbsp;&nbsp;
    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);

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€