← 一覧へ

Accordion / 005 — アイコン|Icon

デザイン見本

  • Plus/minus icon included
  • Icon rotation animation
  • Usability-focused design

プラス・マイナスのアイコンを用いた直感的なデザイン。 開閉状態に応じてアイコンが回転して変化するため、ユーザーが状態を認識しやすいUIです。

実装コード

HTML
<div class="container">
    <div class="btn-box">
        <button data-default-text="Icon Accordion" data-open-text="Close">Icon Accordion</button>
    </div>
    <div class="more">
        <ul>
            <li>Plus/minus icon included</li>
            <li>Icon rotation animation</li>
            <li>Usability-focused design</li>
        </ul>
    </div>
</div>
CSS
/* アニメーション定義 */
@keyframes acc5-fadeIn {
    0% {
        opacity: 0;
        transform: translateY(-10px)
    }
    100% {
        opacity: 1;
        transform: none
    }
}

.container {
    width: 100%;
}

.container .btn-box button {
    width: 100%;
    padding: 16px 20px;
    background: #f8f9fa;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    color: #495057;
    cursor: pointer;
    transition: all 0.3s ease;
    text-align: left;
    position: relative;
}

.container .btn-box button::before {
    content: '+';
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 18px;
    font-weight: bold;
    color: #2196f3;
    transition: transform 0.3s ease;
}

.container .btn-box button:hover {
    background: #e9ecef;
    border-color: #2196f3;
    color: #2196f3;
}

.container .more {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
    background: #fff;
    border: 2px solid #e9ecef;
    border-top: none;
    border-radius: 0 0 8px 8px;
}

.container .more.appear {
    max-height: 200px;
    animation: acc5-fadeIn 0.3s ease;
    margin-top: -2px;
}

/* 開閉時のアイコン回転 (+ → ×) */
.container:has(.more.appear) .btn-box button::before {
    transform: translateY(-50%) rotate(45deg);
}

.container .more ul {
    list-style: none;
    padding: 12px 28px;
    margin: 0;
}

.container .more li {
    padding: 8px 0;
    color: #495057;
    border-bottom: 1px solid #e9ecef;
    font-size: 13px;
    position: relative;
}

/* リストアイテムの装飾 */
.container .more li::before {
    content: '•';
    position: absolute;
    left: -14px;
    color: #2196f3;
}

.container .more li:last-child {
    border-bottom: none;
}
JS
document.addEventListener("DOMContentLoaded", () => {
    document.querySelectorAll('.container').forEach(container => {
        const button = container.querySelector('.btn-box button');
        const content = container.querySelector('.more');
        
        if (button && content) {
            button.addEventListener('click', function() {
                content.classList.toggle('appear');
                
                // ボタンテキストの切り替え
                if (content.classList.contains('appear')) {
                    this.textContent = this.dataset.openText || 'Close';
                } else {
                    this.textContent = this.dataset.defaultText;
                }
            });
        }
    });
});