没有setTimeout的vue单击动画

nag*_*hun 1 javascript animation timeout vue.js

我希望div闪烁,以防用户单击它。有没有手动运行setTimeout的解决方案?

使用setTimeout的解决方案:

app.html

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>

<style>
div { transition: background-color 1s; }
div.flashing { background-color: green; transition: none; }
</style>

<div id="app" :class="{'flashing':flashing}" v-on:click="flash">flash when clicked</div>
Run Code Online (Sandbox Code Playgroud)

app.js

const data = { flashing: false }

new Vue({
  el: '#app',
  data,
  methods: { flash }
})

function flash() {
  data.flashing = true;
  setTimeout(() => data.flashing = false, 100);
}
Run Code Online (Sandbox Code Playgroud)

Js Fiddle:https : //jsfiddle.net/ang3ukg2/

Jos*_*yon 6

Christopher的答案类似,但在某种程度上对Vue来说很惯用。这使用通过绑定类和animationend事件应用的CSS动画。

var demo = new Vue({
  el: '#demo',
  data: {
    animated: false
  },
  methods: {
    animate() {
      this.animated = true
    }
  }
})
Run Code Online (Sandbox Code Playgroud)
<link href="https://unpkg.com/animate.css@3.5.2/animate.min.css" rel="stylesheet" />
<script src="https://unpkg.com/vue@2.2.4/dist/vue.min.js"></script>
<div id="demo">
  <h1 :class="{'bounce animated': animated}" @animationend="animated = false">
    Animate Test
  </h1>
  <button @click="animate">
    Animate
  </button>
</div>
Run Code Online (Sandbox Code Playgroud)

所有信贷罗伯特Kirsz谁提出了另外一个问题注释的解决方案

  • 这应该是公认的答案,因为它解决了在动画结束时删除类的问题。 (2认同)