๐ฉ๐ปโ๐ป ๊ฐ๋จํ ํญ ๊ธฐ๋ฅ์ ํ๋์ฝ๋ ๋ฐฉ์๊ณผ ๋ฐ๋ณต๋ฌธ์ ํ์ฉํ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์ฌ ๋น๊ตํด๋ณด์๋ค
<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");
}
๐ ์ด๊ธฐ ํ๋ฉด
๐ ํญ ํด๋ฆญ ์ ๋ณ๊ฒฝ๋ ํ๋ฉด