[JavaScript] ๋‹ค๋ฅธ ์š”์†Œ ํด๋ž˜์Šค ๋ชจ๋‘ ์ง€์šฐ๊ธฐ
728x90

์„ ํƒ ์š”์†Œ ์™ธ์˜ ํ˜•์ œ์š”์†Œ ํด๋ž˜์Šค ๋ชจ๋‘ ์ง€์šฐ๊ธฐ 

๋ฏธ๋‹ˆ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋‹ค๊ฐ€ ํ˜•์ œ์š”์†Œ๊ฐ€ ๋งŽ์€ ์ƒํƒœ์—์„œ ํŠน์ • ์š”์†Œ๋ฅผ ์„ ํƒํ–ˆ์„ ๋•Œ๋งŒ is-active ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ ,

๋‚˜๋จธ์ง€ ์š”์†Œ๋“ค์€ is-active ํด๋ž˜์Šค๋ฅผ ์–ด๋–ป๊ฒŒ ์ง€์šธ ๊ฒƒ์ธ๊ฐ€...๋ฅผ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ์—์„œ ๋ณด๊ณ  ์ฐธ๊ณ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

์˜ˆ์ „์— ์ œ์ด์ฟผ๋ฆฌ๋กœ๋Š” ํ•œ์ค„๋กœ ์‰ฝ๊ฒŒ ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ ๋ฐ”๋‹๋ผ JS๋กœ๋งŒ ํ•˜๋ ค๋‹ˆ๊นŒ ์•„์ง ์˜ ์‰ฝ์ง€ ์•Š์€ ๊ฒƒ!!...

 

์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ํ˜„์žฌ play-button ์ด๋ผ๋Š” ๋™์ผํ•œ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง„ ๋ฒ„ํŠผ์ด 3๊ฐœ ์žˆ๊ณ , ํด๋ฆญํ•œ ์š”์†Œ์—๋งŒ is-active๋ฅผ ์ถ”๊ฐ€,

๋‚˜๋จธ์ง€๋Š” ์žˆ์—ˆ๋”๋ผ๋„ ์—†์• ์ฃผ๋Š” ๊ฒƒ์„ ์›ํ–ˆ๋‹ค.

 

 

 

ํ•ด๊ฒฐ

function handlePlayerImage(e) {
  e.preventDefault();
  const buttonGroup = this.parentNode;
  const active = buttonGroup.querySelector('.is-active');
  if (active) {
    active.classList.remove('is-active');
  }
  e.currentTarget.classList.add('is-active');
}

buttonList.forEach((button) => {
  button.addEventListener('click', handlePlayerImage);
});

์ด ๋ถ€๋ถ„์— ํ•ด๋‹นํ•˜๋Š” ์ผ๋ถ€ ์ฝ”๋“œ๋งŒ ๊ฐ€์ ธ์™”๋Š”๋ฐ, ํด๋ฆญํ•œ ์š”์†Œ์˜ ๋ถ€๋ชจ์š”์†Œ๋ฅผ ์ฐพ์•„ is-active ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š” ์š”์†Œ๋ฅผ ์ฐพ๊ณ 
active ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ์—†์• ์ฃผ๊ณ , ํ˜„์žฌ ํƒ€๊ฒŸ์ธ ์š”์†Œ์—๋งŒ ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๋„๋ก ํ–ˆ๋‹ค.

 

 

 

728x90