vue 动态class

403 阅读1分钟

三个条件判断: 1=>  101=色卡、2=>102=板布、3=>103=大货

let type = that.orderType == '1' ? '101': (that.orderType == '2' ? '102' : '103')

字符串拼接

:style="`height: ${dataVheight}px;`"

动态div背景

:style="{ 'background-image':'url(' + data.img +')'}"

字符串、对象、数组形式三元

  \
    字符串
  :type="i == 0 ? 'success' : ''"

  对象
  :style="{display:isActive ? 'block':'none'}"
  :style="{'margin-right': isActive ? '100px' : '' }"
  :class="{'active':n == i}"

 数组
  :class="['classify',current=='0' ? 'active' : '']"

点击事件三元:

  <el-button type="primary" @click="edit == 'mod' ? sureModify() : submit()">提 交</el-button>

 购物车按钮的三元

  <div bindtap="pay">{{edit ? '删除' : '去结算'}}</div>

style写多个样式

:style="{left: 390 + 'px',color: brandnamesum == 16 ? 'red' : ''}"
1.  \
    通常给class绑定个对象,就可以动态的切换样式了。(data中定义isActive的truefalse)
1.  <div :class="{'active':isActive==-1}">hello</div>
1.  <div :class="{ active: isActive }">hello</div>
1.  handleClick(){ this.isActive = !this.isActive }
1.
1.  若要绑定多个class,需要逗号隔开就行:(这里的activeTwo加不加引号都可以,也一样都能渲染,如下)
1.  <div class="activeOne" v-bind:class="{ activeTwo: isActive, 'activeThree': hasError }"></div>
1.
1.  <i class="iconfont " :class="[current=='0'?'class1':'class2']"></i>
1.  推荐、不加{}就算是加字符串,最好绑定class用[]、{判断},style用{}
1.
1.  <span v-bind:style="{display:isActive ? 'block':'none'}">hello</span>
1.  <div :style="{width:width,height:height}"></div>
1.  v-bind:style="{样式名:'样式值'}" 必须是字符串形式
1.
1.
1.  数组形式:
1.  <div :class="['classify',current=='0' ? 'active' : '']" @click='current=0'>xx</div>
1.  <div :class="['classify', isActive? 'active' : '']">xx</div>
1.
1.  注意:数组中的classify如果不加引号的话,代表的是data中的一项,并不是类名,将classify加上双引号,变成字符串就可以变成类名
1.
1.  字符串拼接:
1.  <div :class="'classify'+(current=='0'?' active':'')" @click='current=0'>xx</div>
1.  注意:active前要加一个空格(必须有),字符串拼接时,两个字符串之间要有空格
1.
1.  <button class="tk" v-show="(active==0 || active==1) || active==2">退款</button>