[Vuejs]-Animate width of div bound to data property in Vue

0👍

Add css transition like this:

transition-property: all;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
transition-duration: 1s;

And fix the binding:

<div class="progress-value" :style="'width: ' + result + '%'">

See this example

<template>
  <div class="progress">
    <div class="progress-value" :style="'width: ' + result + '%'">
      <h2>{{ result }}%</h2>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      result: 5
    }
  },
  mounted() {
    this.increment()
  },
  methods: {
    increment() {
      this.result += 10
      if (this.result < 95) {
        setTimeout(this.increment, 1000)
      }
    }
  }
}
</script>
<style scoped>
.progress {
  background: #ccc;
  border-radius: 100px;
  position: relative;
  padding: 5px 5px;
  margin: 5px 5px;
  height: 40px;
  width: auto;
}

.progress-value {
  transition-property: all;
  transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  transition-duration: 1s;
  background: #fff;
  height: 30px;
  text-align: center;
}
</style>

Leave a comment