๋ฐ˜์‘ํ˜•

์š”์†Œ ์ˆจ๊น€ ๋ฐฉ์‹ ์ฐจ์ด์ 


  ๋ Œ๋” ํŠธ๋ฆฌ ๋ฆฌํ”Œ๋กœ์šฐ ๋ฆฌํŽ˜์ธํŠธ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ DOM ํŠธ๋ฆฌ
`display: none` ์ œ์™ธ ๋ฐœ์ƒ ๋ฐœ์ƒ ๋น„ํ™œ์„ฑ ์œ ์ง€
`visibility: hidden` ์œ ์ง€ ๋ฐœ์ƒ ์•ˆํ•จ ๋ฐœ์ƒ ๋น„ํ™œ์„ฑ ์œ ์ง€
`opacity: 0` ์œ ์ง€ ๋ฐœ์ƒ ์•ˆํ•จ ๋ฐœ์ƒ ํ™œ์„ฑ ์œ ์ง€
  1. display: none
    • ๋ Œ๋” ํŠธ๋ฆฌ : ์ œ์™ธ
    • ๋ฆฌํ”Œ๋กœ์šฐ : ๋ฐœ์ƒ (๋ Œ๋”ํŠธ๋ฆฌ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋์œผ๋ฏ€๋กœ)
    • ๋ฆฌํŽ˜์ธํŠธ : ๋ฐœ์ƒ (๋ฆฌํ”Œ๋กœ์šฐ ๋ฐœ์ƒ์‹œ ๋ฆฌํŽ˜์ธํŠธ๋„ ํ•จ๊ป˜ ๋ฐœ์ƒ)
    • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ : ๋น„ํ™œ์„ฑ
    • DOM ํŠธ๋ฆฌ : ์œ ์ง€ (์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์กฐ์ž‘ ๊ฐ€๋Šฅ)
  2. visibility: hidden
    • ๋ Œ๋” ํŠธ๋ฆฌ : ์œ ์ง€ (ํ™”๋ฉด์—์„  ์•ˆ๋ณด์ด์ง€๋งŒ ๊ณต๊ฐ„์€ ์ฐจ์ง€ํ•จ)
    • ๋ฆฌํ”Œ๋กœ์šฐ : ๋ฐœ์ƒ ์•ˆํ•จ (๋ ˆ์ด์•„์›ƒ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์œผ๋ฏ€๋กœ)
    • ๋ฆฌํŽ˜์ธํŠธ : ๋ฐœ์ƒ (์š”์†Œ๊ฐ€ ๋ณด์ด์ง€ ์•Š๋„๋ก ๋ณ€๊ฒฝํ–ˆ์œผ๋ฏ€๋กœ)
    • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ : ๋น„ํ™œ์„ฑ
    • DOM ํŠธ๋ฆฌ : ์œ ์ง€ (์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์กฐ์ž‘ ๊ฐ€๋Šฅ)
  3. opacity: 0
    • ๋ Œ๋” ํŠธ๋ฆฌ : ์œ ์ง€
    • ๋ฆฌํ”Œ๋กœ์šฐ : ๋ฐœ์ƒ ์•ˆํ•จ (๋ ˆ์ด์•„์›ƒ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์œผ๋ฏ€๋กœ)
    • ๋ฆฌํŽ˜์ธํŠธ : ๋ฐœ์ƒ (ํˆฌ๋ช…๋„๊ฐ€ ๋ณ€๊ฒฝ๋์œผ๋ฏ€๋กœ)
    • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ : ํ™œ์„ฑ
    • DOM ํŠธ๋ฆฌ : ์œ ์ง€ (์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์กฐ์ž‘ ๊ฐ€๋Šฅ)

 

ํŒ์—… ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ตฌํ˜„


HTML ๊ธฐ๋ณธ ๊ตฌ์กฐ

<button class="open-popup-btn">ํŒ์—…์ฐฝ ์—ด๊ธฐ</button>
<div class="popup">
  <h2>ํŒ์—… Window</h2>
  <button>๋ฒ„ํŠผ1</button>
  <button>๋ฒ„ํŠผ2</button>
  <button>๋ฒ„ํŠผ3</button>
</div>

 

๋ฐฉ๋ฒ• 1 | display: none ์‚ฌ์šฉ

โถ `.popup` ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณด์ด์ง€ ์•Š๋„๋ก ์„ค์ •. `display: none;` ์†์„ฑ์€ ๋ฐ•์Šค๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ณต๊ฐ„๋„ ์ฐจ์ง€ํ•˜์ง€ ์•Š๋Š”๋‹ค. 

.popup {
  display: none;
}

 

โท ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด `.visible` ํด๋ž˜์Šค ์ถ”๊ฐ€ํ•˜๋„๋ก ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์ž‘์„ฑ

const openBtn = document.querySelector(".open-popup-btn");
const popup = document.querySelector(".popup");

openBtn.addEventListener("click", () => {
  if (!popup.classList.contains("visible")) {
    popup.classList.add("visible");
  } else {
    popup.classList.remove("visible");
  }
});

 

โธ `.visible` ํด๋ž˜์Šค ์Šคํƒ€์ผ ์„ค์ •

 

`transform: translatey(...)`๋ฅผ ์„ค์ •ํ•˜๋ฉด ์ž…๋ ฅํ•œ ๊ฐ’์˜ y์ถ•๋ถ€ํ„ฐ ์œ„๋กœ ์˜ฌ๋ผ์˜ค๋Š” ํšจ๊ณผ๋ฅผ ๋‚ธ๋‹ค. ํŒ์—…์ฐฝ์ด ์ ์  ์ปค์ง€๋Š” ํšจ๊ณผ๋ฅผ ๋‚ด๋ ค๋ฉด 0%(from)์ผ ๋•Œ `height: 0%` ์†์„ฑ์„ ์ฃผ๋ฉด ๋œ๋‹ค. opacity๋Š” ํˆฌ๋ช…๋„ ์„ค์ •. 0์€ ์™„์ „ ํˆฌ๋ช…, 1์€ ๋ถˆํˆฌ๋ช…(ํˆฌ๋ช…๋„ ์—†์Œ)์ด๋ฏ€๋กœ ์™„์ „ ํˆฌ๋ช…(from)ํ–ˆ๋‹ค๊ฐ€ ์ ์  ๋ถˆํˆฌ๋ช…(to)์œผ๋กœ ๋ณ€ํ•œ๋‹ค.

@keyframes popup {
  0% {
    opacity: 0;
    transform: translatey(50%);
  }

  100% {
    opacity: 1;
    transform: none;
  }
}

 

.popup.visible {
  background: #f8f1e7;
  position: absolute;
  bottom: 0;
  width: 350px;
  height: 350px;
  border: 1px solid black;
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  animation: popup 0.5s ease-in-out;
}

 

โน ํŒ์—…์ฐฝ์ด ์‚ฌ๋ผ์งˆ ๋•Œ๋„ ํšจ๊ณผ๋ฅผ ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด JS๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆ˜์ •. ํŒ์—…์ฐฝ์ด ์˜ฌ๋ผ์˜จ ์ƒํƒœ(`visible` ํด๋ž˜์Šค ์ถ”๊ฐ€ ์ƒํƒœ)์—์„œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด `.disappear` ํด๋ž˜์Šค๊ฐ€ ์ถ”๊ฐ€๋˜๊ณ  400ms ํ›„์— `.visible` ํด๋ž˜์Šค๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค. `display: none` ์ƒํƒœ์ผ ๋• transition ์†์„ฑ์ด ์ ์šฉ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— `.disappear` ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ์ถ”๊ฐ€ํ•ด์„œ ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ๋ฅผ ๋จผ์ € ๋ณด์—ฌ์ค€ ํ›„ ์ผ์ • ์‹œ๊ฐ„ ์ดํ›„ `display: none` ์†์„ฑ์ด ์ ์šฉ๋˜๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•. 

const openBtn = document.querySelector(".open-popup-btn");
const popup = document.querySelector(".popup");

openBtn.addEventListener("click", () => {
  if (!popup.classList.contains("visible")) {
    popup.classList.remove("disappear");
    popup.classList.add("visible");
  } else {
    popup.classList.add("disappear");
    setTimeout(() => popup.classList.remove("visible"), 400);
  }
});

 

`.popup.disappear`ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์Šคํƒ€์ผ๋„ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. animation ์†์„ฑ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์•„๋ž˜์ฒ˜๋Ÿผ width ๊ฐ’์„ ํŒ์—…์ฐฝ ํฌ๊ธฐ์™€ ๋™์ผํ•˜๊ฒŒ, height๋Š” 0, transition ๊ฐ’์„ ์ฃผ๋ฉด ์•„๋ž˜๋กœ ์Šค๋ฅด๋ฅต ์‚ฌ๋ผ์ง€๋Š” ํšจ๊ณผ๋ฅผ ๋‚ธ๋‹ค. width ๊ฐ’๋„ 0์œผ๋กœ ์ฃผ๋ฉด ์š”์ˆ ๋žจํ”„์ฒ˜๋Ÿผ ํ•œ ๊ณณ์œผ๋กœ ์ ๋ ค์„œ ์‚ฌ๋ผ์ง„๋‹ค. 

transition์˜ `all` ์†์„ฑ์€ width, height ํฌ๊ธฐ ๋ณ€ํ™”์— ๋ชจ๋‘ transition ํšจ๊ณผ๋ฅผ ์ค€๋‹ค. width ํ˜น์€ height๋งŒ ์ž…๋ ฅํ•ด์„œ ํ•ด๋‹น ํฌ๊ธฐ ๋ณ€ํ™”์— ๋Œ€ํ•ด์„œ๋งŒ ํšจ๊ณผ๋ฅผ ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. `transition: all 0.5s ease-in-out` ์†์„ฑ์€ hover ํšจ๊ณผ๋ฅผ ์ ์ง„์ ์œผ๋กœ ํ‘œ์‹œํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋„ ์œ ์šฉํ•˜๋‹ค.

.popup.disappear {
  width: 350px;
  height: 0;
  transition: all 0.5s ease-in-out;
}

 

๋ฐฉ๋ฒ• 2 | visibility: hidden ์‚ฌ์šฉ

๋ถ€๋ชจ ์š”์†Œ ํ˜น์€ ๋ทฐํฌํŠธ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋†’์ด๊ฐ€ ๋™์ ์ด๋ผ๋ฉด height ๋Œ€์‹  max-height๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค

 

visibility: hidden ์†์„ฑ์„ ์‚ฌ์šฉํ•ด๋„ ํ™”๋ฉด์—์„  ์‚ฌ๋ผ์ง€์ง€๋งŒ, display: none๊ณผ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•œ๋‹ค๋Š” ๊ฒƒ. ๋”ฐ๋ผ์„œ ํŒ์—…์ฐฝ์ด ์˜ฌ๋ผ์˜ค์ง€ ์•Š์„๋•Œ์˜ ๊ธฐ๋ณธ height๋Š” 0px๋กœ ์„ค์ •ํ•ด๋‘”๋‹ค. visibility ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด animation์„ ์“ฐ์ง€ ์•Š๊ณ  transition๋งŒ ์‚ฌ์šฉํ•ด๋„ ๋น„์Šทํ•œ ํšจ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

.popup {
  visibility: hidden;
  background: #f8f1e7;
  position: absolute;
  bottom: 0;
  width: 350px;
  height: 0px; /* max-height: 0px */
  border: 1px solid black;
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transition: all 0.5s ease-in-out;
}

.popup.visible {
  visibility: visible;
  height: 350px; /* max-height: 350px */
}

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋Š” visible ํด๋ž˜์Šค๊ฐ€ ์ถ”๊ฐ€/์‚ญ์ œ๋˜๋Š” toggle๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

const openBtn = document.querySelector(".open-popup-btn");
const popup = document.querySelector(".popup");

openBtn.addEventListener("click", () => {
  popup.classList.toggle("visible");
});

 

ํšจ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ GIF

visibility: hidden / transition: all 0.5s ease-in-out;
display: none / transform: translateY(50%)

 

์ฝ”๋“œํŽœ

See the Pen ํŒ์—…์ฐฝ ์• ๋‹ˆ๋ฉ”์ด์…˜ css by ColorFilter (@colorfilter) on CodePen.

 

 

์ฐธ๊ณ  ๊ธ€


 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•