Ale*_*lex 10 html javascript css vue.js vuejs2
我正在使用Vue.js,我想要更改CSS类属性.使用该类的HTML代码如下:
<div class="fillTimerBar"></div>
和CSS代码:
.fillTimerBar {
width: 100%;
height: 8px;
}
从那里我想width使用computedVue组件中的属性更改类属性.
哪个是正确的方式,如果有的话?
Mih*_*nut 16
你必须使用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';
}
}
})Run Code Online (Sandbox Code Playgroud)
#myDiv{
background-color:red;
height:200px;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
要更改类中的属性,您可以使用 CSS 自定义属性:
.fillTimerBar {
--width: 100%;
width: var(--width);
height: 8px;
}
Run Code Online (Sandbox Code Playgroud)
在 Vue 中,您可以将 CSS 变量绑定到样式:
<div class="fillTimerBar" :style="`--width: ${computedWidth}`"></div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15631 次 |
| 最近记录: |