【问题标题】:Vue - how to animate table row changeVue - 如何为表格行更改设置动画
【发布时间】:2016-11-23 11:49:50
【问题描述】:

当添加新行或删除行时,我在表格上运行动画。

但我想为更改的行设置动画 - 它们不会被添加或删除,但绑定到该行的数据会发生变化。

这可能吗?

【问题讨论】:

标签: vue.js


【解决方案1】:

您可以按照@craig_h 的建议查看状态转换,或者只是设置一个常规的 JavaScript 事件来监视动画结束。

要使用第二种方法,您可以为每行数据添加一个新参数 changed: false,然后在更改时将其设置为 true。然后可以将一个类添加到“已更改”行。然后让您的 CSS 在该行具有“已更改”类时触发动画。现在您需要做的就是监听该行上的 'animationend' 事件并将更改的参数重置为 false。比如:

html - 行元素

<table>
  <tr
    ref="rows"
    :class="{ changed: row.changed }"
    v-for="(row, index) in rows">
    <td><input v-model="row.title" type="text"></td>
    <td>
      <button @click="saveRowEdits(index)">save</button>
    </td>
    ...

组件

data () {
  return {
    rows: [
      { title: 'foo', changed: false },
      { title: 'bar', changed: false },
    ],
    ...
  }
},
methods: {
  saveRowEdits (index) {

    // get the rows DOM el
    const row = this.$refs.rows[index]

    // watch for animationend
    const callback = () => {
      row.removeEventListener("animationend", callback);
      this.rows[index].changed = false
    }

    row.addEventListener("animationend", callback, false)

    // update param
    this.rows[index].changed = true

  },
  ...

CSS

row.changed {
  animation: changed-row 1s ...

【讨论】:

  • 这听起来是个好方法。这些行实际上是从一个充满物体的巨大物体中渲染出来的。我可能做的是有一个简单的“changedRows”数组,并在计算类上检查该行是否在该数组中。感谢您的帮助。
  • 是的,当然可以根据您的需要进行调整。很高兴为您提供帮助
猜你喜欢
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-16
  • 2014-05-20
  • 2021-04-13
相关资源
最近更新 更多