vuejs绑定class和style样式写四个按钮
时间: 2023-06-19 13:04:05 浏览: 87
可以这样实现:
```html
<template>
<div>
<button :class="{ active: activeIndex === 1 }" @click="activeIndex = 1">Button 1</button>
<button :class="{ active: activeIndex === 2 }" @click="activeIndex = 2">Button 2</button>
<button :class="{ active: activeIndex === 3 }" @click="activeIndex = 3">Button 3</button>
<button :class="{ active: activeIndex === 4 }" @click="activeIndex = 4">Button 4</button>
<div :style="`background-color: ${color}; font-size: ${fontSize}px;`">
Style Me!
</div>
</div>
</template>
<script>
export default {
data() {
return {
activeIndex: 1,
color: 'red',
fontSize: 16,
};
},
};
</script>
<style>
.active {
background-color: blue;
color: white;
}
div {
padding: 20px;
text-align: center;
}
</style>
```
在上面的代码中,我们使用`:class`和`:style`指令来绑定类和样式。`:class`指令使用对象语法,根据`activeIndex`的值来动态添加或删除`active`类。`:style`指令使用模板字符串,根据`color`和`fontSize`的值来动态计算样式。
阅读全文