使用Vue.js更改CSS类属性

亚历克斯:

我正在使用Vue.js,并且想更改CSS类属性。使用该类的HTML代码如下:

<div class="fillTimerBar"></div>

和CSS代码:

.fillTimerBar {
        width: 100%;
        height: 8px;
 }

从那里,我想width使用computedVue组件中属性更改class属性

哪一种是正确的方式(如果有)?

Mihai Alexandru-Ionut:

您必须使用v-bind:style指令。

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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章