Practice) Accordion

zooyeongยท2023๋…„ 3์›” 22์ผ
0

8์ฃผ์ฐจ

๋ชฉ๋ก ๋ณด๊ธฐ
3/6
post-thumbnail

๐Ÿ”ฅ์•„์ฝ”๋””์–ธ ๋งŒ๋“ค๊ธฐ ์—ฐ์Šต!

๐Ÿ”ฝHTML ์ฝ”๋“œ

<body>

  <h3>์ƒ๋‹จ๊ฐ€์šด๋ฐ๋ฌธ๊ตฌ</h3>
  <div class="container">

    <table class="table">
      
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">Title</th>
          <th scope="col">Last</th>
          <th scope="col">Handle</th>
        </tr>
      </thead>
      
      <tbody>
        <tr>
          <th scope="row">1</th>
          <td>
            <div class="item-container">
              <p class="item-title">Mark</p>
              <div class="item-content">
                <p>์—ฌ๊ธฐ์— ์ˆจ๊ฒจ์ง„ ๋‚ด์šฉ</p>
                <p>์—ฌ๋Ÿฌ์ค„์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Œ</p>
              </div>
            </div>
          </td>
          <td>Otto</td>
          <td>@mdo</td>
        </tr>
        <tr>
          <th scope="row">2</th>
          <td>
            <div class="item-container">
              <p class="item-title">Mark2</p>
              <div class="item-content">
                <p>์—ฌ๊ธฐ์— ์ˆจ๊ฒจ์ง„ ๋‚ด์šฉ</p>
                <p>์—ฌ๋Ÿฌ์ค„์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Œ</p>
              </div>
            </div>
          </td>
          <td>Thornton</td>
          <td>@fat</td>
        </tr>
        <tr>
          <th scope="row">3</th>
          <td>
            <div class="item-container">
              <p class="item-title">Mark3</p>
              <div class="item-content">
                <p>์—ฌ๊ธฐ์— ์ˆจ๊ฒจ์ง„ ๋‚ด์šฉ</p>
                <p>์—ฌ๋Ÿฌ์ค„์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Œ</p>
              </div>
            </div>
          </td>
          <td></td>
          <td>@twitter</td>
        </tr>
      </tbody>
      
    </table>
  </div>
</body>

๐Ÿ”ฝCSS ์ฝ”๋“œ

h3{
  text-align: center;
}
.container{
  width: 600px;
  margin: 0 auto;
}

๐Ÿšฉํ˜„์žฌ ์ƒํƒœ
์•„์ฝ”๋””์–ธ์—ฐ์Šต์บก์ณ1




next step) Mark ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ์ˆจ๊ฒจ๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

๐Ÿ”ฝCSS ์ฝ”๋“œ

h3{
  text-align: center;
}
.container{
  width: 600px;
  margin: 0 auto;
}
.item-title{
  font-weight: bold;
  color: darkblue;
  cursor: pointer; /* ๋ˆ„๋ฅผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์€ ํšจ๊ณผ*/
}
.item-content{
  height: 0px;
  overflow: hidden;
  /* hidden->height๊ฐ€ 0์ด๋ผ ๋ฐ–์œผ๋กœ ์‚์ ธ๋‚˜์˜จ ๋ถ€๋ถ„์„ ์ˆจ๊ฒจ์ค€๋‹ค! */
}

๐Ÿšฉํ˜„์žฌ ์ƒํƒœ
์•„์ฝ”๋””์–ธ์—ฐ์Šต์บก์ณ2




next step) Mark๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ˆจ๊ธด ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ณด๊ฒ ๋‹ค.

๐Ÿ”ฝCSS ์ฝ”๋“œ

h3{
  text-align: center;
}
.container{
  width: 600px;
  margin: 0 auto;
}
.item-title{
  font-weight: bold;
  color: darkblue;
  cursor: pointer;
}
.item-content{
  height: 0px;
  overflow: hidden;
  transition-property: all; /* ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋ณ€ํ•˜๋Š” ํšจ๊ณผ */
  transition-duration: 1s; /* ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋ณ€ํ•˜๋Š” ํšจ๊ณผ */
}

๐Ÿ”ฝjs ์ฝ”๋“œ

const itemTitles = document.querySelectorAll('.item-title');
console.log(itemTitles);
for(let i=0; i<itemTitles.length; i++){
  itemTitles[i].addEventListener('click', function(){
    console.log('ํด๋ฆญ๋จ');
    console.log(itemTitles[i]);
    let itemContents = document.querySelectorAll('.item-content');
    console.log(itemContents);
 
    if(itemContents[i].classList.contains('active')){
      itemContents[i].classList.remove('active');
    } else{
      itemContents[i].classList.add('active');
    }
  });
}

๐Ÿšฉํ˜„์žฌ ์ƒํƒœ
์•„์ฝ”๋””์–ธ์—ฐ์Šต์บก์ณ3




next step) Mark๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ๋‹ค๋ฅธ ๋‚ด์šฉ์ด ์—ด๋ ค์žˆ์œผ๋ฉด ๋‹ซ๊ณ  ํ•ด๋‹น Mark์˜ ๋‚ด์šฉ๋งŒ ์—ด๋ฆฌ๊ฒŒ ๋งŒ๋“ค์–ด๋ณด๊ฒ ๋‹ค.

๐Ÿ”ฝjs ์ฝ”๋“œ

const itemTitles = document.querySelectorAll('.item-title');

console.log(itemTitles);

for(let i=0; i<itemTitles.length; i++){
  itemTitles[i].addEventListener('click', function(){

    console.log('ํด๋ฆญ๋จ');
    console.log(itemTitles[i]);

    let itemContents = document.querySelectorAll('.item-content');
    console.log(itemContents);
    
    if(itemContents[i].classList.contains('active')){
      itemContents[i].classList.remove('active');
    } else{
      for(let el of itemContents){
        el.classList.remove('active');
        //for๋ฌธ์„ ํ†ตํ•ด ์—ด๋ ค์žˆ๋Š” ๋‚ด์šฉ์„ ๋‹ซ์•„์ฃผ๊ธฐ
      }
      itemContents[i].classList.add('active');
    }
  })
}

๐Ÿšฉํ˜„์žฌ ์ƒํƒœ
์•„์ฝ”๋””์–ธ์—ฐ์Šต์บก์ณ4

profile
Have a good day โŒฏโ€™โ–พโ€™โŒฏ

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