Vue基础知识梳理(二)

vue基础知识梳理(二)

1. vue中v-bind:class动态添加class

总结:动态添加class,需要给:class=“对象”,对象中value为true,则key作为class名添加;value为false,则添加失败

  • 方案一:直接在html结构中给class一个对象
//html
<div class="btn" :class="{wordColor:false,wordSize:true}" >点我</div>

//样式
.btn{
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    background: cornflowerblue;
}
.wordColor{
    color: crimson;
}
.wordSize{
    font-size: 24px;
}

效果
在这里插入图片描述

  • 方案二:使用计算属性返回对象
//html
 <div class="btn" :class="classname" >点我</div>

//计算属性
computed:{
	//返回一个对象给class,对象中属性值为true的属性,属性名被添加到class名中
    classname(){
        return{
            wordColor:true,
            wordSize:false
        }
    }
}
//样式
.btn{
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    background: cornflowerblue;
}
.wordColor{
    color: crimson;
}
.wordSize{
    font-size: 24px;
}

效果图
在这里插入图片描述

  • 动态添加style也是一样
动态style对象:<div :style="{ color: textColor, fontSize: '18px' }">&l
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值