当前位置:主页 > JavsScript库 > 菜单 / 导航 >

平滑响应的手风琴用户界面 - responsive-accordion
栏目分类:菜单 / 导航   发布日期:2021-09-02   浏览次数:

具有平滑过渡的响应式、美观的手风琴 UI。使用 HTML、CSS (SCSS) 和一点点 JavaScript 构建。

使用:

1. 手风琴 UI 的 HTML 结构。

<section class="accordion container">
    <div class="accordion__container">
        <div class="accordion__item">
            <header class="accordion__header">
                <i class='bx bx-plus accordion__icon'></i>
                <h3 class="accordion__title">什么是手风琴?</h3>
            </header>
 
            <div class="accordion__content">
                <p class="accordion__description">手风琴始终包含类别标题、展开和折叠状态、指示展开的图标以及它们之间的间距。</p>
            </div>
        </div>
 
        <div class="accordion__item">
            <header class="accordion__header">
                <i class='bx bx-plus accordion__icon'></i>
                <h3 class="accordion__title">何时以及如何使用它?</h3>
            </header>
 
            <div class="accordion__content">
                <p class="accordion__description">当用户只需要单个页面上的几个关键概念或内容描述时,应该使用它。</p>
            </div>
        </div>
 
        <div class="accordion__item">
            <header class="accordion__header">
                <i class='bx bx-plus accordion__icon'></i>
                <h3 class="accordion__title">如果用户在另一张卡片打开时点击折叠的卡片会发生什么?</h3>
            </header>
 
            <div class="accordion__content">
                <p class="accordion__description">碰巧打开的卡片被关闭,让位于下一张打开的卡片的信息,但有不同的设计更喜欢它反过来。</p>
            </div>
        </div>
 
        <div class="accordion__item">
            <header class="accordion__header">
                <i class='bx bx-plus accordion__icon'></i>
                <h3 class="accordion__title">如何选择一个图标来表示扩展?</h3>
            </header>
 
            <div class="accordion__content">
                <p class="accordion__description">您必须选择不同的图标来打开和关闭,以便用户了解他采取的操作。</p>
            </div>
        </div>
    </div>
</section>

2. 手风琴 UI 的主要 CSS 样式。

:root {
  /*========== 颜色 ==========*/
  --hue-color: 225;
  --first-color: hsl(var(--hue-color), 48%, 35%);
  --title-color: hsl(var(--hue-color), 48%, 22%);
  --text-color: hsl(var(--hue-color), 12%, 35%);
  --body-color: hsl(var(--hue-color), 49%, 98%);
  --container-color: #FFF;
  /*========== 字体和排版 ==========*/
  --body-font: 'Poppins', sans-serif;
  --normal-font-size: .938rem;
  --small-font-size: .813rem;
  --smaller-font-size: .75rem;
}
 
.accordion {
  display: grid;
  align-content: center;
  height: 100vh;
}
 
.accordion__container {
  display: grid;
  row-gap: .75rem;
  padding: 2rem 1rem;
  background-color: var(--container-color);
  border-radius: .5rem;
  box-shadow: 0 12px 32px rgba(51, 51, 51, 0.1);
}
 
.accordion__title {
  font-size: var(--small-font-size);
  color: var(--title-color);
  font-weight: 400;
  margin-top: .15rem;
  transition: .2s;
}
 
.accordion__header {
  display: flex;
  column-gap: .5rem;
  padding: 1.25rem 1.25rem 1.25rem 1rem;
  cursor: pointer;
}
 
.accordion__description {
  padding: 0 1.25rem 1.25rem 3rem;
  font-size: var(--smaller-font-size);
}
 
.accordion__icon {
  font-size: 1.5rem;
  height: max-content;
  color: var(--title-color);
  transition: .3s;
}
 
.accordion__item {
  box-shadow: 0 2px 6px rgba(38, 38, 38, 0.1);
  background-color: var(--container-color);
  border-radius: .25rem;
  position: relative;
  transition: all .25s ease;
}
 
.accordion__item::after {
  content: '';
  background-color: var(--first-color);
  width: 5px;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  border-radius: .25rem 0 0 .25rem;
}
 
.accordion__item:nth-child(1) {
  background-color: #fff7f0;
}
 
.accordion__item:nth-child(1)::after {
  background-color: #ffc08a;
}
 
.accordion__item:nth-child(2) {
  background-color: #f0f0ff;
}
 
.accordion__item:nth-child(2)::after {
  background-color: #8a8aff;
}
 
.accordion__item:nth-child(3) {
  background-color: #fff0f3;
}
 
.accordion__item:nth-child(3)::after {
  background-color: #ff8aa1;
}
 
.accordion__item:nth-child(4) {
  background-color: #f0faff;
}
 
.accordion__item:nth-child(4)::after {
  background-color: #8ad8ff;
}
 
.accordion__content {
  overflow: hidden;
  height: 0;
  transition: all .25s ease;
}
 
/*旋转图标并为标题添加字体粗细*/
.accordion-open .accordion__icon {
  transform: rotate(45deg);
}
 
.accordion-open .accordion__title {
  font-weight: 600;
}

3. 使手风琴充分响应,使其在台式机和移动设备等任何设备上都能完美运行。

@media screen and (min-width: 576px) {
  .accordion__container {
    width: 550px;
    padding: 2.5rem;
    justify-self: center;
    border-radius: .75rem;
  }
  .accordion__header {
    padding: 1.5rem;
  }
  .accordion__title {
    padding-right: 3.5rem;
  }
  .accordion__description {
    padding: 0 4rem 1.25rem 3.5rem;
  }
}
 
/* 对于大型设备 */
@media screen and (min-width: 968px) {
  .container {
    margin-left: auto;
    margin-right: auto;
  }
}

4. 启用手风琴逻辑的主要 JavaScript。

const accordionItems = document.querySelectorAll('.accordion__item')
accordionItems.forEach((item) =>{
  const accordionHeader = item.querySelector('.accordion__header')
  accordionHeader.addEventListener('click', () =>{
      const openItem = document.querySelector('.accordion-open')
      toggleItem(item)
      if(openItem && openItem!== item){
          toggleItem(openItem)
      }
  })
})
const toggleItem = (item) =>{
  const accordionContent = item.querySelector('.accordion__content')
  if(item.classList.contains('accordion-open')){
      accordionContent.removeAttribute('style')
      item.classList.remove('accordion-open')
  }else{
      accordionContent.style.height = accordionContent.scrollHeight + 'px'
      item.classList.add('accordion-open')
  }
}


官方网站:https://github.com/bedimcode/responsive-accordion

声源:本站内容均来自互联网,仅供交流学习之用,请勿作商业用途,所有资源版权归原作者所有。如果有侵犯到您的权益,请联系本站删除,谢谢合作!
jQuery库 JavsScript库 Html5库 CSS3库