Change CSS class property with Vue.js
You have to use v-bind:style
directive.
var vm = new Vue({
el: '#example',
data: {
width:'200px'
},
computed: {
computedWidth: function () {
return this.width;
}
},
methods: {
changeWidth: function (event) {
this.width='100px';
}
}
})
#myDiv{
background-color:red;
height:200px;
}
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="example">
<div id="myDiv" v-bind:style="{ width: computedWidth }"></div>
<button v-on:click="changeWidth()">Change</button>
</div>
To change a property inside a class, you can use CSS custom properties:
.fillTimerBar {
--width: 100%;
width: var(--width);
height: 8px;
}
In Vue, you can bind CSS variables to the style:
<div class="fillTimerBar" :style="`--width: ${computedWidth}`"></div>