← 一覧へ

Accordion / 003 — グラデーション|Gradient

デザイン見本

  • Beautiful gradient background
  • Colorful and attractive
  • Visually impactful design

美しいグラデーション背景を使用したアコーディオン。 開閉状態を示す矢印アイコンのアニメーションも実装されており、視覚的にリッチな印象を与えます。

実装コード

HTML
<div class="container">
    <div class="btn-box">
        <button data-default-text="Gradient Accordion" data-open-text="Close">Gradient Accordion</button>
    </div>
    <div class="more">
        <ul>
            <li>Beautiful gradient background</li>
            <li>Colorful and attractive</li>
            <li>Visually impactful design</li>
        </ul>
    </div>
</div>
CSS
/* アニメーション定義 */
@keyframes acc3-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: linear-gradient(135deg, #667eea, #764ba2);
    border: none;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 600;
    color: #fff;
    cursor: pointer;
    transition: all 0.3s ease;
    text-align: left;
    position: relative;
    overflow: hidden;
}

.container .btn-box button::before {
    content: '▼';
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    transition: transform 0.3s;
}

.container .btn-box button:hover {
    background: linear-gradient(135deg, #5a6fd8, #6a4190);
}

.container .more {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.5s ease;
    background: linear-gradient(135deg, #f093fb, #f5576c);
    border-radius: 0 0 10px 10px;
    margin-top: 0;
}

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

/* 開閉時の矢印回転 */
.container:has(.more.appear) .btn-box button::before {
    transform: translateY(-50%) rotate(180deg);
}

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

.container .more li {
    padding: 6px 0;
    color: #fff;
    font-weight: 500;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
    font-size: 13px;
}

.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;
                }
            });
        }
    });
});