JSFiddle - React, Tailwind, and code Playground
HTML
<section class="faq">
<h1 class="title">FAQ</h1>
<ul>
<li>
<div class="question">如何辦理退換貨?</div>
<div class="answer">請參考退貨服務頁面。</div>
</li>
<li>
<div class="question">退款方式及退款時間?</div>
<div class="answer">約7-8個工作天完成退款作業。</div>
</li>
<li>
<div class="question">未退回的退款,是否可直接折抵新訂單?</div>
<div class="answer">很抱歉,重新訂購時無法直接於新訂單扣抵。</div>
</li>
<li>
<div class="question">發票遺失是否仍能辦理退換貨?</div>
<div class="answer">很抱歉,需要有發票才能辦理退換貨。</div>
</li>
<li>
<div class="question">什麼情況無法辦理退換貨?</div>
<div class="answer">超過7天退貨期限,就無法辦理。</div>
</li>
</ul>
</section>
SCSS
* {
box-sizing: border-box;
}
.title {
font-size: 36px;
text-align: center;
padding: 20px 0;
border-bottom: 1px solid #000;
margin-bottom: 20px;
font-weight: bold;
}
li {
max-width: 550px;
margin: 0 auto;
font-size: 24px;
& + & {
margin-top: 20px;
}
.question {
padding: 0 50px 0 10px;
position: relative;
color: #4fbe84;
cursor: pointer;
&:hover {
background-color: #eafff4;
}
&::before {
content: 'Q: ';
font-weight: bold;
}
&::after {
content: '+';
position: absolute;
top: -5px;
right: 10px;
font-size: 36px;
width: fit-content;
transform: rotate(0deg);
transition: 0.5s linear;
}
}
.answer {
height: 0;
overflow: hidden;
background-color: #eafff4;
padding: 0 10px;
visibility: hidden;
&::before {
content: 'A: ';
font-weight: bold;
}
}
&.open .question{
background-color: #eafff4;
&::after {
transform: rotate(405deg);
}
}
}
.slide-down {
animation: slide-down 0.3s linear both;
}
.slide-up {
animation: slide-up 0.3s linear both;
}
@keyframes slide-down {
0% {
visibility: hidden;
height: 0;
}
95% {
visibility: visible;
height: 2rem;
}
100% {
visibility: visible;
height: auto;
}
}
@keyframes slide-up {
from {
visibility: visible;
height: 2rem;
}
to {
visibility: hidden;
height: 0;
}
}
JavaScript
const questions = document.querySelectorAll('.question');
function toggleOpen() {
const list = event.target.closest('li');
const answer = list.querySelector('.answer');
if (list.className.includes('open')) {
answer.classList.remove('slide-down');
answer.classList.add('slide-up');
list.classList.remove('open');
} else {
answer.classList.remove('slide-up');
answer.classList.add('slide-down');
list.classList.add('open');
}
}
for (let question of questions) {
question.onclick = toggleOpen;
}