SnippetUI

丝滑无缝手风琴组件 (Smooth Reveal Accordion)

一款告别固定高度与复杂运算,依靠纯粹 CSS Grid 原生驱动平滑展开与折叠的手风琴组件。

HTMLCSSJavaScript

预览 (Preview)

洗練されたデザインと、スムーズに展開するアニメーションが特徴のコンポーネントです。モダンなCSS技術を使用し、固定の高さやJavaScriptによる複雑な計算に頼ることなく、コンテンツの自然な高さに合わせてシームレスに開閉します。

CSS Gridを利用しています。grid-template-rowsの値を0frから1frへとトランジションさせることで、モダンブラウザ全体で滑らかで信頼性の高い高さのアニメーションを実現しています。

はい、対応しています。ボタンには適切なaria-expanded属性が使用されており、スクリーンリーダーなどの支援技術を使用するユーザーにも、各アコーディオンパネルの開閉状態が正しく伝わります。

应用场景与架构优势 (Use Cases & Features)

  • FAQ 终极解法:帮助落地页或帮助中心高度折叠、结构化大量常见的答疑模块。
  • 高密度配置菜单:在后台管理面板中,将复杂的表单或配置项隔离收纳,严格遵循渐进式披露原则(Progressive Disclosure)。
  • 极客级原理解析:彻底抛弃以往在 JS 中痛苦计算节点 scrollHeight 或是强行定死的 max-height,利用现代 CSS Grid 的 grid-template-rows 黑科技实现了真正自适应高度的极简过渡。默认开启“排他性折叠”(单点互斥)。

设计与交互美学 (Design & UX)

  • 物理级的折叠弹性:配合定制的 cubic-bezier 缓动轴,0fr1fr 的伸缩过渡如同物理引擎演算般充满弹性和重力感,令人忍不住反复把玩。
  • 状态的色彩回馈:一旦某个面板进入 Active 状态,描边边界、文字色号及尾部指引 Icon 的色相和 180° 旋转都会瞬间切换,精准输出交互层面的信号反馈。
  • A11y 无障碍合规准则:在展开与收拢的同时,JS 层会同步重载 aria-expanded 的布尔值,为所有处于暗语环境中的屏幕阅读设备建立完善的信息锚点。

极速魔改与定制 (Customization Guide)

  • 关闭互斥(允许多开):在随附的 JS 脚本中,直接大刀阔斧地抹掉 document.querySelectorAll('.accordion-item').forEach 的遍历清零逻辑,即可解除面板之间的状态锁定。
  • 重新锚定品牌高光:CSS 表中的 #8b5cf6(梦幻紫)是预设的高光着色点。直接覆盖该十六进制参数,便能重定义激活状态下的视觉基准线。
  • 重置状态指引针:组件默认的 SVG 为简练的折线箭头(Chevron),您可根据宿主项目的 UI 字典随意替换为 Plus/Minus (加减号) 或其它形态。

实现代码 (Implementation)

<div class="accordion-container">
<div class="accordion-item">
  <button class="accordion-header" aria-expanded="false">
    <span class="accordion-title">このアコーディオンの特徴は?</span>
    <span class="accordion-icon">
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>
    </span>
  </button>
  <div class="accordion-content-wrapper">
    <div class="accordion-content">
      洗練されたデザインと、スムーズに展開するアニメーションが特徴のコンポーネントです。モダンなCSS技術を使用し、固定の高さやJavaScriptによる複雑な計算に頼ることなく、コンテンツの自然な高さに合わせてシームレスに開閉します。
    </div>
  </div>
</div>
<!-- 他のアコーディオンアイテムをここに追加 -->
</div>

<style>
.accordion-container {
max-width: 600px;
width: 100%;
display: flex;
flex-direction: column;
gap: 0.75rem;
font-family: system-ui, -apple-system, sans-serif;
}

.accordion-item {
border: 1px solid #e5e7eb;
border-radius: 12px;
overflow: hidden;
background-color: #ffffff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.accordion-item:hover {
border-color: #d1d5db;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.accordion-item.active {
border-color: #8b5cf6;
box-shadow: 0 4px 12px rgba(139, 92, 246, 0.1);
}

.accordion-header {
width: 100%;
padding: 1.25rem 1.5rem;
display: flex;
align-items: center;
justify-content: space-between;
background: none;
border: none;
cursor: pointer;
text-align: left;
color: #1f2937;
}

.accordion-title {
font-weight: 600;
font-size: 1.05rem;
transition: color 0.3s ease;
}

.accordion-icon {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
color: #6b7280;
transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1), color 0.3s ease;
}

.accordion-item.active .accordion-icon {
transform: rotate(180deg);
color: #8b5cf6;
}

.accordion-item.active .accordion-title {
color: #8b5cf6;
}

.accordion-content-wrapper {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.accordion-item.active .accordion-content-wrapper {
grid-template-rows: 1fr;
}

.accordion-content {
overflow: hidden;
padding: 0 1.5rem;
color: #4b5563;
line-height: 1.6;
font-size: 0.95rem;
opacity: 0;
transform: translateY(-8px);
transition: opacity 0.4s ease, transform 0.4s ease, padding 0.4s ease;
}

.accordion-item.active .accordion-content {
padding-bottom: 1.25rem;
opacity: 1;
transform: translateY(0);
}
</style>

<script>
document.querySelectorAll('.accordion-header').forEach(button => {
button.addEventListener('click', () => {
  const item = button.closest('.accordion-item');
  const isExpanded = button.getAttribute('aria-expanded') === 'true';
  
  document.querySelectorAll('.accordion-item').forEach(otherItem => {
    if (otherItem !== item) {
      otherItem.classList.remove('active');
      otherItem.querySelector('.accordion-header').setAttribute('aria-expanded', 'false');
    }
  });

  if (!isExpanded) {
    item.classList.add('active');
    button.setAttribute('aria-expanded', 'true');
  } else {
    item.classList.remove('active');
    button.setAttribute('aria-expanded', 'false');
  }
});
});
</script>

引擎兼容声明 (Browser Support)

  • Chrome: 完美支持
  • Firefox: 完美支持
  • Safari: 完美支持 (要求 Safari 16 以上版本方能全面释放 Grid 插值动画潜力)
  • Edge: 完美支持

※ 在极为古旧或不支持 grid-template-rows 连续补间的设备上,高度过渡将静默降级为“零帧瞬间折叠”,确保了核心信息获取链的绝对安全与坚固。