vue之样式绑定这些你知道吗

您可将以下代码c,v到编辑器打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <style>
        /* 对象语法  */
        
        .active {
            border: 2px solid pink;
            width: 200px;
            height: 200px;
        }
        
        .error {
            background-color: aqua;
        }
        /* 数组语法 */
        
        .active2 {
            border: 2px solid rgb(47, 27, 223);
            width: 200px;
            height: 200px;
        }
        
        .error2 {
            background-color: rgb(216, 29, 160);
        }
        /* 结合使用 */
        
        .test {
            color: #13e713;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- 对象语法active为类名,isActive控制是否显示 -->
        <div :class="{active:isActive,error:isError}">对象语法</div>
        <button @click="qiehuan">对象语法切换</button>
        <hr>
        <!-- 数组语法 -->
        <div :class="[activeClass,errorClass]">数组语法</div>
        <button @click="shuzu">数组语法切换</button>
        <hr>
        <!-- 结合使用a类名不会覆盖 -->
        <div :class="[activeClass,errorClass,{test:isTest}]" class="a">结合使用</div>
        <button @click="shuzu">结合使用</button>
        <hr>
        <!-- style内联样式 -->
        <div :style="{border:a,width:b,height:c}">style内联样式</div>
    </div>
    <script type="text/javascript">
        var vm = new Vue({
            el: '#app',
            data: {
                //对象语法可以简化为obj{  isActive: true, isError: true,}
                isActive: true,
                isError: true,
                // 数组语法active2:类名,activeClass变量名,也可以简化为activeClass:['active2','error2']
                activeClass: 'active2',
                errorClass: 'error2',
                // 结合使用
                isTest: true,
                // style内联样式
                a: '1px solid blue',
                b: '200px',
                c: '200px'
            },
            methods: {
                // 对象语法切换
                qiehuan: function() {
                    this.isActive = !this.isActive;
                },
                // 数组语法的切换
                shuzu: function() {
                    this.activeClass = '';
                }
            }
        })
    </script>
</body>

</html>

遇见即是缘分,不吝烦请赐教

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值