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

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 :px' }"
html :first'?'flex':'none')}"
  • 数组
html :
html :first'?'flex':'none')},{fontSize:'20px'}]"
  • 三目运算符
html :#ddd')}"
html :#ddd')},{fontSize:'22px'}]"
  • 多重值
html :-webkit-box', '-ms-flexbox', 'flex'] }"


推荐 收藏
评论(0) 阅读(835)
刷新评论 返回顶部
外星人最新博客