JSFiddle - React, Tailwind, and code Playground
HTML
<header>
<h1>FAQ</h1>
</header>
<div class="wrap">
<button class="question">如何辦理退換貨? </button>
<div class="content"> 請參考退貨服務頁面。</div>
<button class="question">退款方式及退款時間?</button>
<div class="content"> 約7-8個工作天完成退款作業。</div>
<button class="question">未退回的退款,是否可直接折抵新訂單?</button>
<div class="content">很抱歉,重新訂購時無法直接於新訂單扣抵。</div>
<button class="question">發票遺失是否仍能辦理退換貨?</button>
<div class="content">很抱歉,需要有發票才能辦理退換貨。</div>
<button class="question">什麼情況無法辦理退換貨?</button>
<div class="content">超過7天退貨期限,就無法辦理。</div>
</div>
CSS
*{
box-sizing: border-box;
}
header{
text-align: center;
}
.wrap{
border-radius: 10px;
width: 500px;
margin: auto;
}
.question, .answer{
background-color: #f5f5dc;
color: #444;
cursor: pointer;
padding: 15px;
margin-top: 5px;
border: none;
text-align: left;
font-size: 15px;
width: 100%;
display: block;
position: relative;
}
.question::before{
content: "Q:";
padding-right:5px;
}
.question::after{
content:"+";
position: absolute;
right: 5px;
top: 0;
font-size: 1.5rem;
}
.question:hover{
background-color: #ccc;
}
.answer:hover{
background-color: #ccc;
}
.answer::after{
content: "-";
position: absolute;
right: 8px;
top: -7px;
font-size: 2rem;
}
.content {
width: 100%;
padding: 0 14px;
max-height: 0;
overflow: hidden;
background-color: #f1f1f1;
transition: max-height 0.2s ease-out;
margin: auto;
}
.content::before{
content:"A:";
}
JavaScript
var btns=document.querySelectorAll('.question');
function open(){
var question=event.target.closest('.question');
var content=question.nextElementSibling;
question.classList.toggle("answer");
if(content.style.maxHeight==""){
content.style.maxHeight=content.scrollHeight+'px';
}else if(content.style.maxHeight=="0px"){
content.style.maxHeight=content.scrollHeight+'px';
}else{
content.style.maxHeight="0px";
}
}
for(var btn of btns ){
btn.onclick=open;
}