当前位置:网站首页>动态样式绑定--style 和 class
动态样式绑定--style 和 class
2022-06-30 20:00:00 【小刘先生很努力】
一 :动态 style 写法
写之前注意的是:
凡是有-的style属性名都要变成驼峰式,比如font-size要变成fontSize
除了绑定值,其他的属性名的值要用引号括起来,例如
backgroundColor:#00a2ff 要写成 backgroundColor:‘#00a2ff’。
一 1 对象 形式
当我们没有取创建单独的css文件时,直接在代码中写
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
<div :style="{color:(index == 1 ? conFontColor:'#000')}"></div>
或者
<div
:style="{
boxSizing:'border-box',
height:29+'px',
top:(valch-1)*29+'px',
background:chose(val.subjectCode),
borderLeft:' 10px ' + ' solid ' + chose1(val.subjectCode
)}" > /<div>
一 2 数组形式
<div :style="[baseStyles, overridingStyles]"></div>
<div :style="[{color:(index == 1 ? conFontColor:'#000')},{fontSize:'18px'}]"></div>
一 3 三元运算符
<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>
<!-- 写法一 -->
<div :style="[{float: id === '12' ? 'left:'right}]"></div>
<!-- 写法二 -->
<div :style="float: nameList.length === 20 ? 'height:64px' : 'height:32px' "></div>
<!-- 写法三 -->
<div :style="{border:( nameId ===item.id ?'2px solid #4C78FF': 'red')}"></div>
一 4 绑定data对象
<div :style="styleObject"></div>
<script>
data() {
return{
// 在data中定义初始样式值
styleObject: {
color: 'red',
fontSize: '14px'
}
}
}
</scrip>
二 : class写法
二 1 对象形式
<!-- isActive —判定值是: true/false -->
<div :class="{ 'active': isActive }">{
{name}}</div>
二 2 判断是否绑定一个active
<div :class="{'active' : isActive == -2}" >{
{name}}</div>
<div :class="{'active' : isActive == item.nameId}" >{
{item.name}}</div>
二 3 绑定并判断多个
二3.1第一种 用逗号隔开的
<div :class="{ 'active': isActive, 'user': isUser }"></div>
二3.2 放在data里面
<div :class="classObject">{
{name}}</div>
<script>
data() {
return {
classObject:{
active: true,
user:false
}
}
}
</script>
二3.3使用computed计算属性
<div :class="classObject">{
{name}}</div>
<script>
data() {
return {
isActive: true,
isUser: false
}
},
computed: {
classObject: function () {
return {
active: this.isActive,
user:this.isUser
}
}
}
</script>
二 4 数组形式
二4.1单纯的数组
<div :class="[isActive,isUser]">{
{name}}</div>
<script>
data() {
return{
isActive:'active',
isUser:'user'
}
}
</script>
二 4.2数组与三元运算符结合判断选择需要的class
注意:三元运算符后面的 冒号,
两边的class需要加上单引号,否则不能正确渲染
:class = " [ isActive ? 'active' : ' '] "
或者
:class = "[ isActive1 ? 'active' : ' ' ] "
或者
:class= " [ isActiveindex ? 'active' : ' ' ] "
或者
:class=" [ isActive == index ? 'active' : 'otherActiveClass' ] "
二4.3数组对象结合动态判断
//前面这个active在对象里面可以不加单引号,后面这个sort要加单引号、
:class="[
{ active: isActive }, 'sort'
]"
或者
:class="[
{ active: isActive1 }, ‘sort’
]"
或者
:class="[
{ active: isActiveindex }, ‘sort’
]"
边栏推荐
- 项目经理是领导吗?可以批评指责成员吗?
- 【ICLR 2021】半监督目标检测:Unbiased Teacher For Semi-Supervised Object Detection
- Build your own website (20)
- 计网 | 【五 传输层、六 应用层】知识点及例题
- Go language identifier and package name specification
- mysql登录出现1045错误修改方法[通俗易懂]
- 股票开户优惠链接,我如何才能得到?另外,手机开户安全么?
- 新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
- Taihu Lake "China's healthy agricultural products · mobile phone live broadcast" enters Taihu Lake
- Primary school, session 3 - afternoon: Web_ sessionlfi
猜你喜欢

消灭Bug,开发者不可不知的几款Bug探索测试神器。

CV+Deep Learning——网络架构Pytorch复现系列——basenets(BackBones)(一)

Torchdrug -- drug attribute prediction

Implementation principle of PostgreSQL heap table storage engine
![[try to hack] windows system account security](/img/2b/e6e999313e3ae4e1cbf4bfa02daef0.png)
[try to hack] windows system account security

漏洞扫描工具大全,妈妈再也不用担心我挖不到漏洞了

NLP 论文领读|文本生成模型退化怎么办?SimCTG 告诉你答案

What is the difference between tolocal8bit and toutf8() in QT

Cv+deep learning network architecture pytoch recurrence series basenets (backbones) (I)

MySQL master-slave synchronization
随机推荐
太湖 “中国健康农产品·手机直播万里行”走进太湖
To eliminate bugs, developers must know several bug exploration and testing artifacts.
1. Introduction to generating countermeasures network
Redis ziplist 压缩列表的源码解析
Exness: the final value of US GDP unexpectedly accelerated to shrink by 1.6%
[ICLR 2021] semi supervised object detection: unbiased teacher for semi supervised object detection
pytorch实现FLOPs和Params的计算
C语言:hashTable
以全栈全功能解决方案,应对多样工具复杂环境DevOps落地难题
新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
Static classes use @resource annotation injection
QT QStringList用法
All the important spark summit features were released here last night (with ultra clear video attached)
PostgreSQL heap堆表 存储引擎实现原理
Network planning | [five transport layers and six application layers] knowledge points and examples
Lingyun going to sea | 10 leap &huawei cloud: jointly helping Africa with inclusive financial services
杰理之关于长按开机检测抬起问题【篇】
Jerry's question about long press boot detection [chapter]
Perl转换文件的编码类型
Tensorflow2.4 implementation of repvgg