[miniProjects] 01_Expanding Cards

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

miniProjects

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

01_Expanding Cards

๐Ÿ’ป ์ฃผ์ œ : ์นด๋“œ๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ์ ‘ํ˜€์ ธ์žˆ๋˜ ์นด๋“œ๊ฐ€ ํฌ๊ฒŒ ๋‚˜ํƒ€๋‚จ.

  • classList์˜ add, remove ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•ด ํŒจ๋„์„ active ์ƒํƒœ๋กœ ๋งŒ๋“ฆ.
  • css์—์„œ ํŒจ๋„ active๋ฅผ ๊ตฌํ˜„ ํ›„ transition์œผ๋กœ ์œ ์—ฐํ•˜๊ฒŒ ํŒจ๋„์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•จ.

const panels = document.querySelectorAll('.panel');

panels.forEach((panel) => {
  panel.addEventListener('click', () => {
    removeActiveClasses()
    panel.classList.add('active')
  })
});

function removeActiveClasses() {
  panels.forEach(panel => {
    panel.classList.remove('active')
  })
};

๐Ÿ‘๐Ÿป'panel' ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง„ ์š”์†Œ๋ฅผ ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ์š”์†Œ์— 'active' ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๋‹ค๋ฅธ ์š”์†Œ์—์„œ๋Š” 'active' ํด๋ž˜์Šค๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋™์ž‘์„ ๊ตฌํ˜„ํ•จ.

โ—forEach๋ž€?

forEach๋Š” JavaScript ๋ฐฐ์—ด์˜ ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜. ์ด ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. forEach ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ง€์ •๋œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

forEach ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

const array = [1, 2, 3, 4, 5];

array.forEach(function(element) {
  console.log(element);
});

//๊ฒฐ๊ณผ
1
2
3
4
5

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

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