首页 > 编程技术 > js

vue中的style样式如何动态绑定

发布时间:2022-4-20 19:23 作者:酷jjs

style样式如何动态绑定

(1) html中:

 <div class="videoMa" ref="videoMa" style="width:100%;height:100%;">
 <div id="playWnd" class="playWnd"  :style="{width:videoBox.width+ 'px',height:videoBox.height+ 'px'}"></div>
 </div>

(2) data中:

 videoBox:{
       width:800,
       height:500,
     }

(3)mounted中:

  mounted() {
      this.videoBox.width=this.$refs.videoMa.offsetWidth;
      this.videoBox.height=this.$refs.videoMa.offsetHeight;
 }

动态设置style样式

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

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

html :style="{ color: activeColor, fontSize: fontSize + 'px' }"
html :style="{display:(activeName=='first'?'flex':'none')}"

html :style="[baseStyles, overridingStyles]"
html :style="[{display:(activeName=='first'?'flex':'none')},{fontSize:'20px'}]"

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

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/qq_32584661/article/details/99672560

标签:[!--infotagslink--]

您可能感兴趣的文章: