๐ŸŽจ ํƒญ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ (ํ•˜๋“œ์ฝ”๋“œ vs ๋ฐ˜๋ณต๋ฌธ)

jjyu_myยท2025๋…„ 2์›” 10์ผ
0

JAVASCRIPT

๋ชฉ๋ก ๋ณด๊ธฐ
12/14
post-thumbnail

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ๊ฐ„๋‹จํ•œ ํƒญ ๊ธฐ๋Šฅ์„ ํ•˜๋“œ์ฝ”๋“œ ๋ฐฉ์‹๊ณผ ๋ฐ˜๋ณต๋ฌธ์„ ํ™œ์šฉํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์—ฌ ๋น„๊ตํ•ด๋ณด์•˜๋‹ค


๐Ÿ—๏ธ HTML ์ฝ”๋“œ


    <div class="container mt-5">
      <ul class="list">
        <li class="tab-button">Products</li>
        <li class="tab-button orange">Information</li>
        <li class="tab-button">Shipping</li>
      </ul>
      <div class="tab-content">
        <p>์ƒํ’ˆ์„ค๋ช…์ž…๋‹ˆ๋‹ค. Product</p>
      </div>
      <div class="tab-content show">
        <p>์ŠคํŽ™์„ค๋ช…์ž…๋‹ˆ๋‹ค. Information</p>
      </div>
      <div class="tab-content">
        <p>๋ฐฐ์†ก์ •๋ณด์ž…๋‹ˆ๋‹ค. Shipping</p>
      </div>
    </div> 

๐Ÿ“Œ ์„ค๋ช…
โœ… 3๊ฐœ์˜ ํƒญํ™”๋ฉด ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค
โœ… ๊ฐ ํƒญ์„ ํด๋ฆญํ•˜๋ฉด ํ•ด๋‹น ๋‚ด์šฉ์ด ๋ณด์—ฌ์ง„๋‹ค


โœ๏ธ ํ•˜๋“œ์ฝ”๋“œ ๋ฐฉ์‹ (๋น„ํšจ์œจ์ ์ธ ์ฝ”๋“œ ๐Ÿ˜ต)

// ๊ฐ๊ฐ์˜ ํƒญ์— ๋ณ€์ˆ˜์ •์˜
const buttons = document.querySelectorAll(".tab-button"); // ๋ชจ๋“  ํƒญ ๋ฒ„ํŠผ์„ ์„ ํƒ
const productsButton = buttons[0]; // ์ฒซ ๋ฒˆ์งธ ํƒญ ๋ฒ„ํŠผ (์ œํ’ˆ)
const informationButton = buttons[1]; // ๋‘ ๋ฒˆ์งธ ํƒญ ๋ฒ„ํŠผ (์ •๋ณด)
const shippingButton = buttons[2]; // ์„ธ ๋ฒˆ์งธ ํƒญ ๋ฒ„ํŠผ (๋ฐฐ์†ก)
const contents = document.querySelectorAll(".tab-content"); // ๋ชจ๋“  ํƒญ ๋‚ด์šฉ์„ ์„ ํƒ
const productsContent = contents[0]; // ์ฒซ ๋ฒˆ์งธ ํƒญ ๋‚ด์šฉ (์ œํ’ˆ)
const informationContent = contents[1]; // ๋‘ ๋ฒˆ์งธ ํƒญ ๋‚ด์šฉ (์ •๋ณด)
const shippingContent = contents[2]; // ์„ธ ๋ฒˆ์งธ ํƒญ ๋‚ด์šฉ (๋ฐฐ์†ก)

// ๋ฒ„ํŠผ 1, 2, 3์— ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ ๋ถ€์ฐฉ
productsButton.addEventListener("click", productsHandle); // ์ฒซ ๋ฒˆ์งธ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ productsHandle ํ•จ์ˆ˜ ์‹คํ–‰
informationButton.addEventListener("click", informationHandle); // ๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ informationHandle ํ•จ์ˆ˜ ์‹คํ–‰
shippingButton.addEventListener("click", shippingHandle); // ์„ธ ๋ฒˆ์งธ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ shippingHandle ํ•จ์ˆ˜ ์‹คํ–‰

// ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ํƒญ์œผ๋กœ ์ด๋™๋˜๋Š” ๊ฐ ํ•จ์ˆ˜ ์‹คํ–‰
function productsHandle() {
  // ๋‹ค๋ฅธ ๋ฒ„ํŠผ์—์„œ 'orange' ํด๋ž˜์Šค๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ์ฒซ ๋ฒˆ์งธ ๋ฒ„ํŠผ์— ์ถ”๊ฐ€
  informationButton.classList.remove("orange");
  shippingButton.classList.remove("orange");
  productsButton.classList.add("orange");

  // ๋‹ค๋ฅธ ํƒญ ๋‚ด์šฉ์„ ์ˆจ๊ธฐ๊ณ , ์ฒซ ๋ฒˆ์งธ ํƒญ ๋‚ด์šฉ์„ ํ‘œ์‹œ
  informationContent.classList.remove("show");
  shippingContent.classList.remove("show");
  productsContent.classList.add("show");
}

function informationHandle() {
  // ๋‹ค๋ฅธ ๋ฒ„ํŠผ์—์„œ 'orange' ํด๋ž˜์Šค๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ๋ฒ„ํŠผ์— ์ถ”๊ฐ€
  productsButton.classList.remove("orange");
  shippingButton.classList.remove("orange");
  informationButton.classList.add("orange");

  // ๋‹ค๋ฅธ ํƒญ ๋‚ด์šฉ์„ ์ˆจ๊ธฐ๊ณ , ๋‘ ๋ฒˆ์งธ ํƒญ ๋‚ด์šฉ์„ ํ‘œ์‹œ
  productsContent.classList.remove("show");
  shippingContent.classList.remove("show");
  informationContent.classList.add("show");
}

function shippingHandle() {
  // ๋‹ค๋ฅธ ๋ฒ„ํŠผ์—์„œ 'orange' ํด๋ž˜์Šค๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ์„ธ ๋ฒˆ์งธ ๋ฒ„ํŠผ์— ์ถ”๊ฐ€
  productsButton.classList.remove("orange");
  informationButton.classList.remove("orange");
  shippingButton.classList.add("orange");

  // ๋‹ค๋ฅธ ํƒญ ๋‚ด์šฉ์„ ์ˆจ๊ธฐ๊ณ , ์„ธ ๋ฒˆ์งธ ํƒญ ๋‚ด์šฉ์„ ํ‘œ์‹œ
  productsContent.classList.remove("show");
  informationContent.classList.remove("show");
  shippingContent.classList.add("show");
}

๐Ÿ† ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ (๊น”๋”ํ•œ ์ฝ”๋“œ โœจ)

// ๊ฐ๊ฐ์˜ ํƒญ์— ๋ณ€์ˆ˜์ •์˜
const buttons = document.querySelectorAll(".tab-button");
const contents = document.querySelectorAll(".tab-content");

// ๋ชจ๋“  ๋ฒ„ํŠผ์— ํด๋ฆญ ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ ๋ถ€์ฐฉ
buttons.forEach((button, index) => {
  button.addEventListener("click", () => handleTab(index));
});

function handleTab(index) {
  // ๋ชจ๋“  ๋ฒ„ํŠผ์—์„œ 'orange' ํด๋ž˜์Šค ์ œ๊ฑฐํ•˜๊ณ  ํด๋ฆญํ•œ ๋ฒ„ํŠผ์— ์ถ”๊ฐ€
  buttons.forEach((button) => button.classList.remove("orange"));
  buttons[index].classList.add("orange");

  // ๋ชจ๋“  ํƒญ ๋‚ด์šฉ์„ ์ˆจ๊ธฐ๊ณ  ํด๋ฆญํ•œ ํƒญ ๋‚ด์šฉ๋งŒ ํ‘œ์‹œ
  contents.forEach((content) => content.classList.remove("show"));
  contents[index].classList.add("show");
}

๐ŸŽจ ๊ตฌํ˜„ ํ™”๋ฉด

๐Ÿ“Œ ์ดˆ๊ธฐ ํ™”๋ฉด

๐Ÿ“Œ ํƒญ ํด๋ฆญ ์‹œ ๋ณ€๊ฒฝ๋œ ํ™”๋ฉด


profile

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

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด