Accordion / 017 — FAQ|FAQ
デザイン見本
- A: This is a simple Q&A style accordion with clean Material Design principles.
シンプルで実用的なFAQスタイルのアコーディオン。 Material Designの原則に基づいたシャドウとアニメーションを採用し、QAコンテンツに最適です。 このデザインも、一つを開くと他が閉じる排他制御ロジックを持っています。
実装コード
HTML
<div class="container">
<div class="btn-box">
<button data-default-text="Q: What is this accordion?" data-open-text="Close">Q: What is this accordion?</button>
</div>
<div class="more">
<ul>
<li>A: This is a simple Q&A style accordion with clean Material Design principles.</li>
</ul>
</div>
</div>
CSS
/* アニメーション定義 */
@keyframes acc17-fadeIn {
0% {
opacity: 0;
transform: translateY(-10px)
}
100% {
opacity: 1;
transform: translateY(0)
}
}
.container {
margin: 4px 0;
position: relative;
width: 100%;
}
.container .btn-box button {
width: 100%;
padding: 16px 20px;
background: #f5f5f5;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
color: #333;
cursor: pointer;
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
text-align: left;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.container .btn-box button:hover {
background: #e0e0e0;
}
.container .more {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1);
background: #fff;
border-radius: 0 0 8px 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-top: 0;
z-index: 1;
position: relative;
}
.container .more.appear {
max-height: 200px;
margin-top: -4px;
animation: acc17-fadeIn 0.3s ease;
}
.container .more ul {
list-style: none;
padding: 12px 20px;
margin: 0;
}
.container .more li {
padding: 12px 0;
color: #333;
font-size: 13px;
line-height: 1.6;
}
JS
document.addEventListener("DOMContentLoaded", () => {
const containers = document.querySelectorAll('.container');
containers.forEach(container => {
const button = container.querySelector('.btn-box button');
const content = container.querySelector('.more');
if (button && content) {
button.addEventListener('click', function() {
const isOpen = content.classList.contains('appear');
// 他のアコーディオンを閉じる
containers.forEach(c => {
const cContent = c.querySelector('.more');
const cButton = c.querySelector('.btn-box button');
if(cContent) cContent.classList.remove('appear');
if(cButton) cButton.textContent = cButton.dataset.defaultText;
});
// クリックされたものだけ開く
if (!isOpen) {
content.classList.add('appear');
this.textContent = this.dataset.openText || 'Close';
}
});
}
});
});