vue动态绑定style样式写法(教你绑定style属性的5种方法)

 分类:vue教程时间:2022-12-05 07:30:01点击:

vue动态绑定style样式写法,要注意:

1、凡是有-的style属性名都要变成驼峰式,比如font-size要变成fontSize

2、除了绑定值/变量,其他的属性名的值要用引号括起来,比如backgroundColor:'#00a2ff'而不是 backgroundColor:#00a2ff

vue动态绑定style属性的5种方法如下:


一、对象方法

  • html :style="{ color: activeColor, fontSize: fontSize + 'px' }"

  • html :style="{color:(index==0?conFontColor:'#000')}"

二、数组方法

  • html :style="[baseStyles, overridingStyles]"

  • html :style="[{color:(index==0?conFontColor:'#000')},{fontSize:'20px'}]"

三、三目运算符方法

  • html :style="{color:(index==0?conFontColor:'#000')}"

  • html :style="[{color:(index==0?conFontColor:'#000')},{fontSize:'20px'}]"

四、多重值方法

此时,浏览器会根据运行支持情况进行选择

  • html :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"

五、绑定data对象方法

  • html :style="styleObject"

data() {
    return{
      styleObject: {
        color: 'red',
        fontSize: '13px'
      }  
    }
}


除注明外的文章,均为来源:老汤博客,转载请保留本文地址!
原文地址: