javascript - 使用 Vue.js 更改 CSS 类属性

标签 javascript html css vue.js vuejs2

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

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

以及 CSS 代码:

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

从那里我想使用 Vue 组件中的 computed 属性更改 width 类属性。

如果有的话,哪种方法是正确的?

最佳答案

你必须使用 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/vue@2.4.3/dist/vue.js"></script>

<div id="example">
  <div id="myDiv" v-bind:style="{ width: computedWidth }"></div>
  <button v-on:click="changeWidth()">Change</button>
</div>

关于javascript - 使用 Vue.js 更改 CSS 类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46195802/

相关文章:

HTML 未找到样式表信息

html - 网站 div 不会从左向右拉伸(stretch),如何修复 css?

javascript - 使用AJAX加载元素时重新加载JS函数

javascript - 全宽图像 slider

javascript - 使用 Javascript/Jquery 根据当前日期和天数计算日期

javascript - 如何禁用对 jQuery.DataTables init 的排序?

javascript - MathJax 未正确显示动态添加(使用 js)矩阵

javascript - float 元素到底部

javascript - 加载 css 和 js

javascript - fullPage.js,内容 div 未正确放置