vue动态绑定class

一,v-bind:class 一个对象,以动态地切换 class:

<view class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }"> </view>
结果为:<div class="static active"></div>

二,样式动态绑定对象

<view v-bind:class="classObject"></view>
data: {
  classObject: {
    active: true,
    'text-danger': false
  }
}
结果同上

 

三,数组语法:把数组传给v-bind:class

<view v-bind:class="[activeClass, errorClass]"></view> 
data: {
  activeClass: 'active',
  errorClass: 'text-danger'
}
结果:<div class="active text-danger"></div>

 

四,三元表达式

<view v-bind:class="[isActive ? activeClass : '', errorClass]"></view>

五,绑定内联样式 

a.绑定变量

<view v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></view>
data: {
  activeColor: 'red',
  fontSize: 30
}

六,class拼接数据

<view :class="'item_'+status"></view>

 七,style拼接数据

有很多时候我们需要用到背景图拼接数据,url要加引号拼接,使用驼峰命名法,需要加花括号

<view :style = ' { backgroundImage : " url ( " + imgurl + " ) " } ' ></view>

参考:https://blog.csdn.net/weixin_44586666/article/details/105311904?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0.essearch_pc_relevant&spm=1001.2101.3001.4242

b.绑定对象

<div v-bind:style="styleObject"></div>
data: {
  styleObject: {
    color: 'red',
    fontSize: '13px'
  }
}

c.数组语法绑定多个样式对象

<div v-bind:style="[baseStyles, overridingStyles]"></div>
<view :style="[typelistStyle,{'top': `${topBarHeight}px`}]"></view>

d.多重值,兼容浏览器

会渲染数组中浏览器支持的前缀的值,如果无兼容问题,会渲染最后一个
<div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>

 

posted @ 2021-08-26 14:34  424174余  阅读(212)  评论(0编辑  收藏  举报