[miniProjects] 40_Background Boxes

๋ณด๋ฆฌยท2023๋…„ 7์›” 24์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
40/47

40_Background Boxes

๐Ÿ’ป์ฃผ์ œ : gif์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” 3d ๋ฐ•์Šค

โœจJS

btn.addEventListener('click', () => boxesContainer.classList.toggle('big'))

function createBoxes() {
  for(let i=0; i<4; i++) {
    for(let j=0; j<4; j++) {
      const box = document.createElement('div')
      box.classList.add('box')
      box.style.backgroundPosition = `${-j * 125}px ${-i * 125}px`
      //boxContainer ์•ˆ์— box ์ž์‹ ๋„ฃ๊ธฐ
      boxesContainer.appendChild(box)
    }
  }
}
  • magic ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ bigํด๋ž˜์Šค๊ฐ€ ์ถ”๊ฐ€๋จ.
  • 4x4 ๋ฐ•์Šค๊ฐ€ ๋‚˜์˜ค๋„๋ก ์ด์ค‘ for๋ฌธ์„ ์”€.
  • createElement๋กœ div๋ฅผ 16๊ฐœ ์ƒ์„ฑ ํ›„, classList๋กœ box ํด๋ž˜์Šค๋ฅผ ์„ค์ •.
	<div class="box" style="background-position: 0 0;"></div>
    <div class="box" style="background-position: -125px 0;"></div>
    <div class="box" style="background-position: -250px 0;"></div>
    <div class="box" style="background-position: -375px 0;"></div>
  • ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ gif๋ฅผ ์กฐ์ •ํ•˜์—ฌ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€ ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ๊ฐ ํด๋ž˜์Šค๋ฅผ ์ชผ๊ฐœ์–ด ์œ„์น˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•จ.
  • backgroundPosition๋กœ ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์„ค์ •.

โœจCSS

๋ฒ„ํŠผ ๋ˆŒ๋ ธ์„ ๋•Œ ๋ˆ„๋ฅด๋Š” ํšจ๊ณผ

.magic:active {
  box-shadow: none;
  /* ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ 2px ์•„๋ž˜๋กœ ์›€์ง์ž„ */
  transform: translateY(2px);
}

big์ด๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ ๋ถ™์—ˆ์„ ๋•Œ ํšŒ์ „ํšจ๊ณผ

.boxes {
  height: 500px;
  width: 500px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  position: relative;
  transition: .4s ease;
}

.boxes.big .box {
  transform: rotateZ(360deg);
}

gif ์„ค์ •

  • ๋…ธ๋ž€์ƒ‰ ๋ง‰๋Œ€๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด skewX,Y๋ฅผ ํ†ตํ•ด 3D ํšจ๊ณผ๋ฅผ ์คŒ.
  • ::after์™€ ::before๋ฅผ ์„ค์ •ํ•˜์—ฌ ์œ„์น˜ ์„ค์ •.
.box {
  background-image: url('https://gifburg.com/images/gifs/spongebob/gifs/0010.gif');
  background-repeat: no-repeat;
  background-size: 500px 500px;
  position: relative;
  background-color: yellow;
  height: 125px;
  width: 125px;
  transition: .4s ease;
}

.box::after {
  content: '';
  background-color: #f6e58d;
  position: absolute;
  top: 8px;
  right: -15px;
  height: 100%;
  width: 15px;
  transform: skewY(45deg);
}

.box::before {
  content: '';
  background-color: #f9ca24;
  position: absolute;
  left: 8px;
  bottom: -15px;
  height: 15px;
  width: 100%;
  transform: skewX(45deg);
}

โ“::after, ::before

::after์™€ ::before๋Š” CSS ๊ฐ€์ƒ ์š”์†Œ(pseudo-elements)๋กœ, HTML์— ์ง์ ‘ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ฝ˜ํ…์ธ ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์š”์†Œ์˜ ํŠน์ • ์œ„์น˜์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

::after

  • ์„ ํƒํ•œ ์š”์†Œ์˜ ๋‚ด์šฉ(content) ๋’ค์— ์ƒ์„ฑ๋œ๋‹ค. ์ฆ‰, ํ•ด๋‹น ์š”์†Œ์˜ ๋งจ ๋’ค์— ์œ„์น˜๋œ๋‹ค.

::before

  • ์„ ํƒํ•œ ์š”์†Œ์˜ ๋‚ด์šฉ(content) ์•ž์— ์ƒ์„ฑ๋œ๋‹ค. ์ฆ‰, ํ•ด๋‹น ์š”์†Œ์˜ ๋งจ ์•ž์— ์œ„์น˜๋œ๋‹ค.
<div class="box">Content</div>
.box::after {
  content: "After";
}

.box::before {
  content: "Before";
}

์œ„์˜ ์ฝ”๋“œ์—์„œ
.box๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง„ ์š”์†Œ์— ::after์™€ ::before ๊ฐ€์ƒ ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

๊ฒฐ๊ณผ๋Š”
BeforeContentAfter ์ด๋‹ค.

ํ•ด๋‹น ์š”์†Œ์—๋Š” "Before"๊ฐ€ ๋จผ์ € ์ถœ๋ ฅ๋˜๊ณ , ๊ทธ ๋’ค์— "Content"๊ฐ€ ์›๋ž˜ ์š”์†Œ์˜ ๋‚ด์šฉ์œผ๋กœ ์ถœ๋ ฅ๋˜๊ณ , ๋งˆ์ง€๋ง‰์œผ๋กœ "After"๊ฐ€ ๋œ๋‹ค. ์ฆ‰, ::before๋Š” ์š”์†Œ์˜ ๋งจ ์•ž์—, ::after๋Š” ์š”์†Œ์˜ ๋งจ ๋’ค์— ์œ„์น˜ํ•˜์—ฌ ์ฝ˜ํ…์ธ ๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋œ๋‹ค.

profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

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

comment-user-thumbnail
2023๋…„ 7์›” 24์ผ

๊ฐœ๋ฐœ์ž๋กœ์„œ ์„ฑ์žฅํ•˜๋Š” ๋ฐ ํฐ ๋„์›€์ด ๋œ ๊ธ€์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ