在元素上设置填充时,动画突然跳跃

时间:2019-02-22 19:00:13

标签: javascript css animation ecmascript-6

在以Animate Plus为动画的手风琴中,当在元素上设置padding时,折叠的动画在关闭时会突然跳跃……

jumping animation

未设置padding时,动画很流畅……

smooth animation

设置padding后如何使手风琴动画化?

我的JavaScript代码:

const accordions = Array.from(document.querySelectorAll("dl")).map(dl => ({
  element: dl,
  translate: 0
}))

const getButtons = accordion =>
  Array.from(accordion.element.getElementsByTagName("button"), element => ({
    element,
    translate: 0
  }))

const timing = {
  easing: "out-quartic",
  duration: 400
}

const clear = element =>
  Object.values(element.attributes).forEach(({ name }) =>
    element.removeAttribute(name)
  )

const hide = async (accordion, buttons, collapsing) => {
  const objects = buttons.filter(({ translate }) => translate)
  const direction = "reverse"
  rotate(collapsing.previousElementSibling.lastElementChild, direction)
  slide(accordion, objects)
  await fold(collapsing, direction)
  clear(collapsing)
}

const show = (accordion, buttons, expanding) => {
  const button = expanding.previousElementSibling.lastElementChild
  const index = buttons.findIndex(({ element }) => element == button)
  const objects = buttons.slice(index + 1)
  const { height } = expanding.getBoundingClientRect()
  expanding.className = "open"
  rotate(button)
  slide(accordion, objects, height)
  fold(expanding)
}

const slide = (accordion, array, to = 0) => {
  center(accordion, to)
  animate({
    ...timing,
    elements: array.map(({ element }) => element.parentElement),
    transform(index) {
      const object = array[index]
      const from = object.translate
      object.translate = to
      return [`translateY(${from}px)`, to]
    }
  })
}

const center = (accordion, height) => {
  const from = accordion.translate
  const to = Math.round(-height / 2)
  accordion.translate = to
  animate({
    ...timing,
    elements: accordion.element,
    transform: [`translateY(${from}px)`, to]
  })
}

const fold = async (content, direction = "normal") => {
  const scrollHeight = content.scrollHeight
  await animate({
    ...timing,
    direction,
    elements: content,
    opacity: [0, 1],
    maxHeight: ["0px", scrollHeight + "px"],
    transform: ["scaleY(0)", 1]
  })
}
const rotate = ({ lastElementChild: elements }, direction = "normal") =>
  animate({
    elements,
    direction,
    easing: "out-cubic",
    duration: 600,
    transform: ["rotate(0turn)", 0.5]
  })

const toggle = (accordion, buttons) => async ({ target }) => {
  const collapsing = accordion.element.querySelector(".open")
  const expanding = target.parentElement.nextElementSibling
  if (collapsing) await hide(accordion, buttons, collapsing)
  if (collapsing != expanding) show(accordion, buttons, expanding)
}

accordions.forEach(accordion => {
  const buttons = getButtons(accordion)
  buttons.forEach(({ element }) =>
    element.addEventListener("click", toggle(accordion, buttons))
  )
})

import animate from "https://cdn.jsdelivr.net/npm/animateplus@2/animateplus.js"

  

我的手风琴完整代码可以在CodePen上找到:

     

https://codepen.io/anon/pen/KJjYba

3 个答案:

答案 0 :(得分:1)

使用fold()方法对填充进行动画处理;将所需的测量值添加到paddingBottompaddingTop属性中。然后,您可以从CSS中的.open类规则中删除padding属性。

const fold = async (content, direction = "normal") => {
  const scrollHeight = content.scrollHeight;
  await animate({
    ...timing,
    direction,
    elements: content,
    opacity: [0, 1],
    maxHeight: ["0px", scrollHeight + "px"],
    paddingTop: ["0em", "2em"],
    paddingBottom: ["0em", "2em"],
    transform: ["scaleY(0)", 1]
  });
};

CodePen

答案 1 :(得分:1)

您必须将paddingBottomfold添加到const fold = async (content, direction = "normal") => { const scrollHeight = content.scrollHeight await animate({ ...timing, direction, elements: content, opacity: [0, 1], maxHeight: ['0px', scrollHeight + 'px'], transform: ["scaleY(0)", 1], paddingTop: ['0em', '2em'], // add this paddingBottom: ['0em', '2em'] // add this }) } 函数中:

script = page.at("//head/script[3]").text.sub(/window\.pageData=/, "") #=> "{JsonDataType}"

Code pen

答案 2 :(得分:-1)

您可以将填充设置为新的子元素。因此,将内容包装在额外的budget中,如下所示:

div

然后,不对<dd><div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div></dd> 进行样式设置,而是对内部的.open进行样式化:

div

它不会用孩子跳。