๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/JavaScript jQuery

[JavaScript / jQuery] ํƒญ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

by Connecting-the-dots 2022. 3. 6.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

 

  • ํƒญ ๋งŒ๋“œ๋Š” ๋ถ€๋ถ„์€ ๊ฐ•์˜์—์„œ ๊ธฐ๋ณธ HTML, CSS ๋ฅผ ์ œ๊ณตํ•ด์ฃผ์–ด์„œ ๊ธฐ๋Šฅ๋งŒ ๋งŒ๋“ค๋ฉด ๋˜์—ˆ๊ธฐ์— ํฌ๊ฒŒ ์–ด๋ ต์ง€๋Š” ์•Š์•˜๋‹ค.
  • ๋‹ค๋งŒ Dark Mode ๋ฅผ ์ œ๋Œ€๋กœ ์ ์šฉํ•˜๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ border-bottom ๋ถ€๋ถ„์ด ์›ํ•˜๋Š” ๋Œ€๋กœ ๋‚˜์˜ค์ง€ ์•Š์•„ ์ผ๋‹จ ๋ณด๋ฅ˜ํ•ด๋‘์—ˆ๋‹ค.

๐Ÿ’œ HTML

<div class="container mt-5">
    <ul class="list">
        <li class="tab-button active">Products</li>
        <li class="tab-button">Information</li>
        <li class="tab-button">Shipping</li>
    </ul>

    <div class="tab-content show">
        <p>์ƒํ’ˆ์„ค๋ช…์ž…๋‹ˆ๋‹ค. Product</p>
    </div>
    <div class="tab-content">
        <p>์ƒํ’ˆ์ •๋ณด์ž…๋‹ˆ๋‹ค. Info</p>
    </div>
    <div class="tab-content">
        <p>๋ฐฐ์†ก์ •๋ณด์ž…๋‹ˆ๋‹ค. Shipping</p>
    </div>
</div>

๐Ÿ’œ CSS

ul.list {
  list-style-type: none;
  margin: 0;
  padding: 0;
  border-bottom: 1px solid #ccc;
}

ul.list::after {
  content: "";
  display: block;
  clear: both;
}

.tab-button {
  display: block;
  padding: 10px 20px 10px 20px;
  float: left;
  margin-bottom: -1px;
  color: grey;
  text-decoration: none;
  cursor: pointer;
}

.active {
  border-top: 2px solid gold;
  border-right: 1px solid #ccc;
  border-bottom: 1px solid white;
  border-left: 1px solid #ccc;
  color: black;
  margin-top: -2px;
}

.tab-content {
  display: none;
  padding: 10px;
}

.show {
  display: block;
}

๐Ÿ’œ JavaScript

for (let i = 0; i < $('.tab-button').length; i++){
        $('.tab-button').eq(i).on('click', function(){
            $('.tab-button').removeClass('active');
            $('.tab-content').removeClass('show');
            $('.tab-button').eq(i).addClass('active');
            $('.tab-content').eq(i).addClass('show');
    })
}
  • ํŠน์ • ์ˆœ์„œ์˜ ํƒญ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ, ํ•ด๋‹น ํƒญ์ด ํ™œ์„ฑํ™”๋˜๋ฉด์„œ ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.
  • ์ด ๋•Œ, ์ฝ”๋“œ๋ฅผ ์ œ๋Œ€๋กœ ์งœ์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด ํ•˜๋‚˜์˜ ํƒญ์„ ํด๋ฆญํ•˜๊ณ  ๋‚œ ํ›„ ๋‹ค๋ฅธ ํƒญ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ํ™œ์„ฑํ™”๋˜๋Š” ํƒญ์ด ๋ฐ”๋€Œ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ๊ทธ๋ƒฅ ํ™œ์„ฑํ™”๋œ ํƒญ์ด ํ•˜๋‚˜๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  • ์ด ๋ถ€๋ถ„์€ ๊ทธ๋ƒฅ ํŠน์ • ํƒญ์„ ๋ˆŒ๋ €์„ ๋•Œ ๋‚ด๊ฐ€ ๋ˆ„๋ฅธ ํƒญ์ด ํ™œ์„ฑํ™”๋˜๊ธฐ ์ „์— ๋ชจ๋“  ํƒญ๋“ค์„ ๋น„ํ™œ์„ฑํ™”์‹œ์ผœ๋†“๋Š” ๊ฒƒ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  HTML ํŒŒ์ผ์—์„œ ํƒญ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ, JavaScript ํŒŒ์ผ์—์„œ ํƒญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋„๋ก ๋ฐ˜๋ณต๋ฌธ ๋ฐ  length ๋ฅผ ํ™œ์šฉํ•ด ํ™•์žฅ์„ฑ์„ ๋†’์˜€๋‹ค.
728x90
๋ฐ˜์‘ํ˜•