vue.js学习(7)样式绑定

vue.js学习——样式绑定

这里主要是使用v-bind标签
直接贴代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
  <style>
  .active{
    width:100px;
    height:100px;
    background:blue;
  }
  </style>
</head>
<body>
  <div id="app">
    <div v-bind:class="{active:isactive}"></div>
  </div>
  <p id="info">
<script type="text/javascript">
  var vm = new Vue({
  el: '#app',
  data: {
    isactive:true
  },
});
  </script>
</body>
</html>

运行结果如下:
在这里插入图片描述
下面对代码进行解释:

   <div v-bind:class="{active:isactive}"></div>

这里的active表示style里面的.active样式,isactive为它的属性值

   isactive:true

在这里将active的属性值标为true,则表示显示此样式

拓展1 样式覆盖

若将上述代码进行修改后如下:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
  <style>
  .active{
    width:100px;
    height:100px;
    background:blue;
  }
  .end{
    background: red;
  }
  </style>
</head>
<body>
  <div id="app">
    <div v-bind:class="{active:isactive, 'end':end1}"></div>
  </div>
  <p id="info">
<script type="text/javascript">
  var vm = new Vue({
  el: '#app',
  data: {
    isactive:true,
    end1:true
  },
});
  </script>
</body>
</html>

则显示结果如下所示:
在这里插入图片描述
这是由于end类背景颜色覆盖掉了active类的颜色,同理,可以加第三个style来覆盖end类

拓展2 使用对象

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<style>
.active {
	width: 100px;
	height: 100px;
	background: green;
}
.text-danger {
	background: red;
}
</style>
</head>
<body>
<div id="app">
  <div v-bind:class="classObject"></div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    classObject: {
      active: true,
      'text-danger': true
    }
  }
})
</script>
</body>
</html>

运行结果和上述相同
在这里将v-bind的class归为一个对象,然后在对象中进行样式的开关

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值