Vue 第三讲(v-bind 属性)

类属性

v-bind:class 属性绑定单个类

  • 使用 CSS 样式定义类(class_one)颜色为(blueviolet)
  • 创建 Vue 对象,并在配置项(data)里面定义变量(classname)
  • 将 Vue 中的变量(classname)的值属性(v-bind:class)传递给标签中的属性(class)中
  • 特别注意:其中属性(v-bind:class)可以简写成(:class)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.js"></script>
    <style>
        .class_one{
            color:darksalmon;
        }
    </style>
</head>
<body>
    <div id="box">
        <h1 v-bind:class="classname">Hello SUSU</h1>
    </div>
    <script>
        new Vue({
            el:"#box",
            data:{
                classname:"class_one",
            },
        })
    </script>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
v-bind:class 属性绑定多个类

  • 特别注意:此处是数组传参
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.js"></script>
    <style>
        .class_one{
            color:darksalmon;
        }
        .class_two{
            font-size: 100px;
        }
    </style>
</head>
<body>
    <div id="box">
        <h1 v-bind:class="[classname1,classname2]">Hello SUSU</h1>
    </div>
    <script>
        new Vue({
            el:"#box",
            data:{
                classname1:"class_one",
                classname2:"class_two",
            },
        })
    </script>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
通过对象的方式绑定 class 属性

  • 此处我们可以理解为 CSS 样式里面定义了二个类(class_one,class_two)
  • 在 Vue 里面定义了二个变量(classname1,classname2),而且此处的变量是布尔值
  • v-bind:class 属性绑定的时候,语法 v-bind:class="{类1:变量1,类2:变量2}" ,实质上是借用变量的布尔值来判定类的属性是否应用在该标签上面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.js"></script>
    <style>
        .class_one{
            color:darksalmon;
        }
        .class_two{
            font-size: 100px;
        }
    </style>
</head>
<body>
    <div id="box">
        <h1 v-bind:class="{class_one:classname1,class_two:classname2}">Hello SUSU</h1>
    </div>
    <script>
        new Vue({
            el:"#box",
            data:{
                classname1:true,
                classname2:false,
            },
        })
    </script>
</body>
</html>
  • 在浏览器中打开(此处变量1的布尔值为真,故只绑定了类1在标签上)

在这里插入图片描述

样式属性

v-bind:style 属性绑定单个样式

  • v-bind:style 属性绑定样式的时候,需要指定该变量对应的样式名
  • 有二种指定方式,分别是在标签中指定和在定义变量的同时指定
  • v-bind:style="{'color':color1}" 在标签中指定
  • color2:{color:"pink"} 在定义变量的同时指定
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.js"></script>
</head>
<body>
    <div id="box">
        <h1 v-bind:style="{'color':color1}">Hello SUSU</h1>
        <h1 v-bind:style="color2">Hello KEKE</h1>
    </div>
    <script>
        new Vue({
            el:"#box",
            data:{
                color1:"red",
                color2:{color:"pink"},
            },
        })
    </script>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述
v-bind:style 属性绑定多个样式

  • 通过数组传递多个参数
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.js"></script>
</head>
<body>
    <div id="box">
        <h1 v-bind:style="[{'color':color1,'fontSize':fontsize1}]">Hello SUSU</h1>
        <h1 v-bind:style="[color2,fontsize2]">Hello KEKE</h1>
    </div>
    <script>
        new Vue({
            el:"#box",
            data:{
                color1:"pink",
                fontsize1:"99px",
                color2:{color:"red"},
                fontsize2:{fontSize:"66px"},
            },
        })
    </script>
</body>
</html>
  • 在浏览器中打开

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是我来晚了!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值