当前位置:网站首页>Dynamic style binding --style and class
Dynamic style binding --style and class
2022-06-30 20:35:00 【Mr. Liu works hard】
One : dynamic style How to write it
Note before writing :
All have - Of style Attribute names have to be humped , such as font-size To become fontSize
Except binding value , The values of other attribute names should be enclosed in quotation marks , for example
backgroundColor:#00a2ff To write backgroundColor:‘#00a2ff’.
One 1 object form
When we don't create separate css When you file , Write... Directly in the code
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
<div :style="{color:(index == 1 ? conFontColor:'#000')}"></div>
perhaps
<div
:style="{
boxSizing:'border-box',
height:29+'px',
top:(valch-1)*29+'px',
background:chose(val.subjectCode),
borderLeft:' 10px ' + ' solid ' + chose1(val.subjectCode
)}" > /<div>
One 2 Array form
<div :style="[baseStyles, overridingStyles]"></div>
<div :style="[{color:(index == 1 ? conFontColor:'#000')},{fontSize:'18px'}]"></div>
One 3 Ternary operator
<div :style="{color:(index == 1 ? conFontColor:'#000')}"></div>
<div :style="[{color:(index == 1 ? conFontColor:'#000')},{fontSize:'18px'}]"></div>
<div
:style="item.layerName === activeLayerName?'font-weight:700' : 'font-weight:400'">
</div>
<!-- Writing a -->
<div :style="[{float: id === '12' ? 'left:'right}]"></div>
<!-- Write two -->
<div :style="float: nameList.length === 20 ? 'height:64px' : 'height:32px' "></div>
<!-- Write three -->
<div :style="{border:( nameId ===item.id ?'2px solid #4C78FF': 'red')}"></div>
One 4 binding data object
<div :style="styleObject"></div>
<script>
data() {
return{
// stay data Define the initial style values in
styleObject: {
color: 'red',
fontSize: '14px'
}
}
}
</scrip>
Two : class How to write it
Two 1 object form
<!-- isActive — Decision value is : true/false -->
<div :class="{ 'active': isActive }">{
{name}}</div>
Two 2 Determine whether to bind a active
<div :class="{'active' : isActive == -2}" >{
{name}}</div>
<div :class="{'active' : isActive == item.nameId}" >{
{item.name}}</div>
Two 3 Bind and judge multiple
Two 3.1 The first one is Separated by commas
<div :class="{ 'active': isActive, 'user': isUser }"></div>
Two 3.2 Put it in data Inside
<div :class="classObject">{
{name}}</div>
<script>
data() {
return {
classObject:{
active: true,
user:false
}
}
}
</script>
Two 3.3 Use computed Compute properties
<div :class="classObject">{
{name}}</div>
<script>
data() {
return {
isActive: true,
isUser: false
}
},
computed: {
classObject: function () {
return {
active: this.isActive,
user:this.isUser
}
}
}
</script>
Two 4 Array form
Two 4.1 Simple arrays
<div :class="[isActive,isUser]">{
{name}}</div>
<script>
data() {
return{
isActive:'active',
isUser:'user'
}
}
</script>
Two 4.2 Array and ternary operator are combined to judge and select the required class
Be careful : After the ternary operator The colon ,
On both sides class You need to put single quotes , Otherwise, it will not render correctly
:class = " [ isActive ? 'active' : ' '] "
perhaps
:class = "[ isActive1 ? 'active' : ' ' ] "
perhaps
:class= " [ isActiveindex ? 'active' : ' ' ] "
perhaps
:class=" [ isActive == index ? 'active' : 'otherActiveClass' ] "
Two 4.3 Array objects combined with dynamic judgment
// This is the front. active You can put no single quotation marks inside the object , This one at the back sort Use single quotes 、
:class="[
{ active: isActive }, 'sort'
]"
perhaps
:class="[
{ active: isActive1 }, ‘sort’
]"
perhaps
:class="[
{ active: isActiveindex }, ‘sort’
]"
边栏推荐
- 杰理之触摸按键识别流程【篇】
- Maya House Modeling
- 25:第三章:开发通行证服务:8:【注册/登录】接口:接收并校验“手机号和验证码”参数;(重点需要知道【利用redis来暂存数据,获取数据的】的应用场景)(使用到了【@Valid注解】参数校验)
- Lambda 表达式原理分析学习(2022.06.23)
- Halcon knowledge: check the measurement objects [1]
- Pytorch implements the calculation of flops and params
- 第299场
- 大神详解开源 BUFF 增益攻略丨直播
- Common questions and answering skills of project manager interview
- Game 81 biweekly
猜你喜欢
随机推荐
Lingyun going to sea | 10 leap &huawei cloud: jointly helping Africa with inclusive financial services
哈夫曼树(一)基本概念与C语言实现
QT qstringlist usage
Implementation principle of PostgreSQL heap table storage engine
Exness: the final value of US GDP unexpectedly accelerated to shrink by 1.6%
QT QStringList用法
Evolution of screen display technology
mysql登录出现1045错误修改方法[通俗易懂]
杰理之关于长按复位【篇】
exness:美GDP终值意外加速萎缩1.6%
谈谈内联函数
AVL平衡二叉树(一) - 概念与C语言实现
PM这样汇报工作,老板心甘情愿给你加薪
Heartbeat uses NFS to make MySQL highly available based on CRM
漏洞扫描工具大全,妈妈再也不用担心我挖不到漏洞了
NLP skill tree learning route - (I) route overview
1045 error occurred in MySQL login. Modification method [easy to understand]
What bank card do you need to open an account online? In addition, is it safe to open an account online now?
项目经理不应该犯的错误
Lumiprobe染料 NHS 酯丨BDP FL NHS 酯研究



![Halcon knowledge: check the measurement objects [1]](/img/0a/3a12e281fcb201d8d11b25dac4145a.png)
![Jerry's touch key recognition process [chapter]](/img/3e/bb73c735d0a7c7a26989c65a432dad.png)




