class 与 style 是 html 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。
以下例子,把class样式设置在style标签中,vue实例中只存在一个布尔值isActive ,用v-bind:class=”{ active: isActive }”的方式绑定样式,根据布尔值来决定是否渲染。
<style>.active {
width: 100px;
height: 100px;
background: green;}</style><p id="app">
<p v-bind:class="{ active: flag}"></p></p><script>new Vue({
el: '#app',
data: {
flag: true
}
})
</script>绑定多个class
v-bind:class="{ active: isActive, 'text-danger': hasError }">以对象形式绑定多个class
<p id="app">
<p v-bind:class="classObject"></p></p><script>new Vue({
el: '#app',
data: {
classObject: {
active: true,
'text-danger': true
}
}
})
</script>以数组的方式绑定样式
立即学习“前端免费学习笔记(深入)”;
<p id="app">
<p v-bind:class="[activeClass, errorClass]"></p></p><script>new Vue({
el: '#app',
data: {
activeClass: 'active',
errorClass: 'text-danger'
}
})
</script>内联样式的绑定
<p id="app">
<p v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鸟教程</p></p><script>new Vue({
el: '#app',
data: {
activeColor: 'green',
fontSize: 30
}
})用对象方式绑定内联样式
<p id="app">
<p v-bind:style="styleObject">菜鸟教程</p></p><script>new Vue({
el: '#app',
data: {
styleObject: {
color: 'green',
fontSize: '30px'
}
}
})
</script>以上就是vue.js样式绑定问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号