平滑手风琴折叠CSS3关键帧动画

时间:2018-09-18 00:11:33

标签: css3 animation css-animations accordion

我的网站上有一个使用ES6 JS和CSS3构建的手风琴元素。我创建了一个CSS3关键帧动画来打开和关闭这些手风琴,但是折叠是突然发生的,并且不像打开动画那样平滑。我在以下代码中做错了什么?我该怎么做才能解决这个问题?

const initial = (accordions) => {
  accordions.forEach(accordion => {
    accordion.classList.add('close')
  })
}

const reset = (accordions) => {
  accordions.forEach(accordion => {
    accordion.classList.add('close')
    accordion.classList.remove('open')
  })
}

const toggle = (accordions) => {
  accordions.forEach(accordion => {
    accordion.onclick = function() {
      if (accordion.classList.contains('open')) {
        accordion.classList.remove('open')
        accordion.classList.add('close')
      } else {
        reset(accordions)
        accordion.classList.remove('close')
        accordion.classList.add('open')
      }
    }
  })
}

let accordions = Array(...document.querySelectorAll('.js-accordion'))
if (accordions.length) {
  initial(accordions)
  reset(accordions)
  toggle(accordions)
}
@keyframes open {
  0% {
    display: none;
    transform: translate3d(0, -1rem, 0);
    opacity: 0;
  }
  1% {
    display: block;
    transform: translate3d(0, -1rem, 0);
    opacity: 0;
  }
  100% {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
}
@keyframes close {
  0% {
    display: block;
    opacity: 1;
  }
  99% {
    display: none;
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}
.faq {
  padding: 4em 0;
}
.faq-item {
  background-repeat: no-repeat;
  background-size: 1.5em;
  background-position: 100% 50%;
  padding: 1em 0;
  cursor: pointer;
  border-bottom: 1px solid black;
}
.faq-item a {
  text-decoration: none;
}
.faq-item.open {
  background-image: url("accordion-minus.svg");
}
.faq-item.open .content {
  animation: open 0.5s ease-in-out;
  display: block;
}
.faq-item.close {
  background-image: url("accordion-plus.svg");
}
.faq-item.close .content {
  animation: close 0.5s ease-in-out;
  display: none;
}
.faq-border {
  border-bottom: 1px solid black;
}
.content {
  width: 95%;
}
<div class="faq">
<h3>Acupuncture</h3>

<div class="faq-item js-accordion">
Question
  <div class="content js-accordion-content">Answer</div>
</div>

</div>

1 个答案:

答案 0 :(得分:1)

我认为在此切换动画中不需要动画或关键帧。您需要的是transitiontransform

    不建议使用
  1. display进行过渡。因为在显示时dom无法获得尺寸:无
  2. 非常建议使用visibilityopacity显示和隐藏过渡元素
  3. 首先,放置.faq-item .content的样式。当该元素处于隐藏状态时
  4. 然后将样式显示在.faq-item.open .content

对不起,如果我不清楚的话。

这是您需要的吗? TRY THIS FIDDLE

const initial = (accordions) => {
  accordions.forEach(accordion => {
    accordion.classList.remove('open')
  })
}

const reset = (accordions) => {
  accordions.forEach(accordion => {
    accordion.classList.remove('open')
  })
}

const toggle = (accordions) => {
  accordions.forEach(accordion => {
    accordion.onclick = function() {
      if (accordion.classList.contains('open')) {
        accordion.classList.remove('open')
      } else {
        reset(accordions)
        accordion.classList.add('open')
      }
    }
  })
}

let accordions = Array(...document.querySelectorAll('.js-accordion'))
if (accordions.length) {
  initial(accordions)
  reset(accordions)
  toggle(accordions)
}
.faq {
  padding: 4em 0;
}
.faq-item {
  background-repeat: no-repeat;
  background-size: 1.5em;
  background-position: 100% 50%;
  padding: 1em 0;
  cursor: pointer;
  border-bottom: 1px solid black;
}
.faq-item a {
  text-decoration: none;
}
.faq-item.open {
  background-image: url("accordion-minus.svg");
}
.faq-item .content{
  display: block !important;
  transform: translateY(0);
  opacity:0;
  visibility: hidden;
  transition: all 0.5s ease-in-out;
}
.faq-item.open .content {
  display: block !important;
  transform: translateY(1em);
  opacity:1;
  visibility: visible;
}

.faq-border {
  border-bottom: 1px solid black;
}
.content {
  width: 95%;
}
<div class="faq">
<h3>Acupuncture</h3>

<div class="faq-item js-accordion">
Question
  <div class="content js-accordion-content">Answer</div>
</div>

</div>