挑战百日学习计划(Vue)_v-bind:class

样式绑定分两种:

  1. v-bind:class =“”
  2. v-bind:style=“”
    表达式的结果可以是数组也可以是对象,但是都是vue中的变量
    直接定义class的名字则不生效

一:对象赋值

<html>
<head>
<style>
.active{
   width:100px;
   height:100px;
   backgroud:Red;
}
</style>
</head>
<body>
  <div id="app">
       <div v-bind:class="{active:isture}"></div>
  </div>
  <script>
        new  Vue({
                 el:"#app",
                 data:{
                  istrue:true
                    }
 
 })
  </script>
</body>
</html>

运行结果:在这里插入图片描述
v-bind:class=对象
对象既:active=【键】 istrue=【值】
也可以绑定多个对象:

<div v-bind:class="{active:istrue,isround}"></div>

<script>
new Vue({
   el:"#app",
   data:{
        istrue:true,
        isround:true
   }
})
</script>

绑定多个参数可以进行切换效果

二:变量赋值

<style>
.active{width:100px;height:100px;background:Red;}
</style>
<div v-bind:class="myclass"></div>
<script>
  new Vue({
        el:"#app",
        data:{
          myclass:"active"
        }
 })
</script>

运行结果和上图一致!

三:构造一个对象返回,其实还是对象赋值
1.html 部分

<style>
.active{width:100px;height:100px;}
.mcolor{backgroud:green;}
</style>
<div v-bind:class="objclass"></div>

Vue代码部分

new Vue({
    el:"#app",
    data:{
    },
    computed:{
       objclass:function(){
              return:{
                active:true,
                mcolor:true,
              }
            }
     }
})

运行结果如图:
在这里插入图片描述

所以 如果用对象的表达则是
【键】:【值】
【键】=classname
【值】=判断运算值 true false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值