Class
<head> <style> .page { height: 200px; width: 200px; } .active { background: skyblue } </style></head><body> <div id="app"> <!-- 通过对象形式判断 --> <div class="page" :class="{active:isTrue}"></div> <!-- 直接放置对象 --> <div class="page" :class=objActive></div> <!-- 放置数组 --> <div class="page" :class=arryActive></div> <!-- 数组对象混合 --> <div class="page" :class=objAndArray></div> </div> <script type="text/javascript"> let app = new Vue({ el: '#app', data: { isTrue: true, objActive: { "active": true, "color": true //"-"非法必须使用字符串 }, arryActive: ['active', 'color'], objAndArray: ['active', { abc: true}] } }); </script></body>
Style
<body>
<div id="app">
<!-- 通过对象+变量拼接 -->
<div :style="{background:'red',margin:marginWidth+'px'}" style=" width:100px;height: 100px;"></div>
<!-- 放置对象 -->
<div :style="styleObj" style=" width:100px;height: 100px;"></div>
<!-- 放置数组 -->
<div :style="styleArry" style=" width:100px;height: 100px;"></div>
</div>
<script type="text/javascript">
let app = new Vue({
el: '#app',
data: {
marginWidth: 10,
styleObj: {
'background-color': 'blue',
height: '200px'
},
styleArry: [{
'background-color': 'blue',
height: '200px'
}, {
'margin-top': '30px'
}]
}
});
</script>
</body>
动态添加style|class
<div
class="item"
v-for="(c1, index) in categoryList"
:key="c1.categoryId"
>
<h3
@mouseenter="changeIndex(index)"
:class="{ cur: currentIndex === index }"
>