<div class="container">
<h1>Accordion</h1>
<div class="btn-box">
<button id="btn-close-all">모두 닫기</button>
</div>
<table>
<caption>기사 리스트</caption>
<thead>
<th>번호</th>
<th>제목</th>
<th>카테고리</th>
<th>날짜</th>
<th>조회수</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>
<div class="accordion-container">
<h3 class="accordion-title">소제목1</h3>
<div class="accordion-content">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis perspiciatis dignissimos, quae
mollitia eaque tempora distinctio, dicta vel sit praesentium voluptatem ullam. Vero nam quod facilis?
Voluptatibus excepturi qui culpa.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis perspiciatis dignissimos, quae
mollitia eaque tempora distinctio, dicta vel sit praesentium voluptatem ullam. Vero nam quod facilis?
Voluptatibus excepturi qui culpa.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis perspiciatis dignissimos, quae
mollitia eaque tempora distinctio, dicta vel sit praesentium voluptatem ullam. Vero nam quod facilis?
Voluptatibus excepturi qui culpa.</p>
</div>
</div>
</td>
<td>장르1</td>
<td>1900.01.01</td>
<td>71</td>
</tr>
<tr>
<td>2</td>
<td>
<div class="accordion-container">
<h3 class="accordion-title">소제목2</h3>
<div class="accordion-content">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis perspiciatis dignissimos, quae
mollitia eaque tempora distinctio, dicta vel sit praesentium voluptatem ullam. Vero nam quod facilis?
Voluptatibus excepturi qui culpa.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis perspiciatis dignissimos, quae
mollitia eaque tempora distinctio, dicta vel sit praesentium voluptatem ullam. Vero nam quod facilis?
Voluptatibus excepturi qui culpa.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis perspiciatis dignissimos, quae
mollitia eaque tempora distinctio, dicta vel sit praesentium voluptatem ullam. Vero nam quod facilis?
Voluptatibus excepturi qui culpa.</p>
</div>
</div>
</td>
<td>장르2</td>
<td>2900.02.02</td>
<td>72</td>
</tr>
<tr>
<td>3</td>
<td>
<div class="accordion-container">
<h3 class="accordion-title">소제목3</h3>
<div class="accordion-content">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis perspiciatis dignissimos, quae
mollitia eaque tempora distinctio, dicta vel sit praesentium voluptatem ullam. Vero nam quod facilis?
Voluptatibus excepturi qui culpa.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis perspiciatis dignissimos, quae
mollitia eaque tempora distinctio, dicta vel sit praesentium voluptatem ullam. Vero nam quod facilis?
Voluptatibus excepturi qui culpa.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis perspiciatis dignissimos, quae
mollitia eaque tempora distinctio, dicta vel sit praesentium voluptatem ullam. Vero nam quod facilis?
Voluptatibus excepturi qui culpa.</p>
</div>
</div>
</td>
<td>장르3</td>
<td>3900.03.03</td>
<td>73</td>
</tr>
</tbody>
</table>
</div>
1 아코디언이 적용될 태그
2 아코디언이 적용될 태그에 줄 active 클래스를 미리 정의
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
text-align: center;
}
.container {
width: 1200px;
margin: 30px auto;
}
.btn-box {
display: flex;
justify-content: end;
}
#btn-close-all {
padding: 16px;
font-size: 16px;
}
table {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
border: 1px solid black;
}
thead {
background-color: antiquewhite;
}
#btn-close-all {
cursor: pointer;
}
#btn-close-all:hover {
background-color: cornflowerblue;
color: black;
transition: all 0.35s;
}
td:not(td:nth-child(2)) {
width: 10%;
}
.accordion-container {
width: 40%;
margin: auto;
}
.accordion-content {
height: 20px;
overflow-y: hidden;
transition: all 1.4s;
}
.active {
display: block;
height: 400px;
overflow: auto;
}
const $accordion = document.querySelectorAll('.accordion-container')
const $accordionContent = document.querySelectorAll('.accordion-content')
const $closeBtn = document.querySelector('#btn-close-all')
window.onload = () => {
for (let i = 0; i < $accordion.length; i++) {
$accordion[i].addEventListener('click', () => {
$accordionContent[i].classList.add('active')
})
}
$closeBtn.addEventListener('click', () => {
for (let i = 0; i < $accordionContent.length; i++) {
$accordionContent[i].classList.remove('active')
}
})
}