通常给class绑定个对象,就可以动态的切换样式了。(data中定义isActive的true、false)
<div :class="{ active: isActive }">hello</div>
handleClick(){ this.isActive = !this.isActive }
<i class="iconfont " :class="[current=='0'?'class1':'class2']"></i>
推荐、不加{}就算是加字符串,最好绑定class用[]、{判断},style用{}
<span v-bind:style="{display:isActive ? 'block':'none'}">hello</span>
<div :style="{width:width,height:height}"></div>
v-bind:style="{样式名:'样式值'}" 必须是字符串形式
数组形式:
<div :class='["classify",current=="0" ? "active" : ""]' @click='current=0'>xx</div>
<div :class='["classify", isActive? "active" : ""]'>xx</div>
注意:数组中的classify如果不加引号的话,代表的是data中的一项,并不是类名,将classify加上双引号,变成字符串就可以变成类名
字符串拼接:
<div :class="'classify'+(current=='0'?' active':'')" @click='current=0'>xx</div>
注意:active前要加一个空格(必须有),字符串拼接时,两个字符串之间要有空格
<button class="tk" v-show="(active==0 || active==1) || active==2">退款</button>
<!-- class 绑定 -->
<div :class="{ red: isRed }"></div>
<div :class="[classA, classB]"></div>
<div :class="[classA, { classB: isB, classC: isC }]">
![样式](https://img.haomeiwen.com/i14350612/078410ae3ecaaf97.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<div :class="[{'active':1=='1'},{'active':2=='2'},{'active':3=='3'}]">789</div>
随机色
color16() { //十六进制颜色随机
var r = Math.floor(Math.random() * 256);
var g = Math.floor(Math.random() * 256);
var b = Math.floor(Math.random() * 256);
var color = '#' + r.toString(16) + g.toString(16) + b.toString(16);
return color;
},
网友评论