VUE学习(五)-Class 与 Style 绑定

VUE学习(五)-Class 与 Style 绑定

    在应用界面中, 某个(些)元素的样式是变化的,class/style 绑定就是专门用来实现动态样式效果的技术

1 class  绑定

     (1):class='xxx'

     (2)表达式xxx是字符串: 'classA'

     (3)表达式xxx是对象: {classA:isA, classB: isB}

     (4)表达式xxx是数组: ['classA', 'classB']

2  style  绑定

     (1):style="{ color, fontSize}"

     (2):style="{ color: activeColor, fontSize: fontSize + 'px' }"

     (3)其中 activeColor/fontSize 是 data 属性

3 示例

   <style>
        .classA {
            color: red;
        }
        .classB {
            background: blue;
        }
        .classC {
            font-size: 20px;
        }
   </style> 
   <div id="app">
        <h3>1. class 绑定: :class='xxx'</h3>
        <p><span :class="a">表达式是字符串: 'classA'</span></p>
        <p :class="{classA: isA, classB: isB}">表达式是对象: {classA:isA, classB: isB}</p>
        <p :class="['classA', 'classC']"> 表达式是数组: ['classA', 'classB']</p>

        <h3>2. style 绑定</h3>
        <p :style="{ color, fontSize}">style="{ color, fontSize}"</p>
        <p :style="{ color: activeColor, fontSize: fontSizeVal +'px' }">style="{ color: activeColor, fontSize: fontSize +'px' }"</p>
        
        <button @click="update">更新</button>
    </div>

    <script type="text/javascript" src="../plugins/js/vue.js"></script>
    <script type="text/javascript">
        var vm = new Vue({
            el: '#app',
            data: {
                a:'classA',
                isA:true,
                isB:false,
                color:'red',
                fontSize: '20px',
                activeColor:'red',
                fontSizeVal: '20'
            },
            methods:{
                update () {
                    this.a = 'classC'
                    this.isA = false
                    this.isB = true
                    this.color = 'blue'
                    this.activeColor = 'blue'
                    this.fontSize = '30px'
                    this.fontSizeVal = '30'
                }
            }
        })
    </script>

    注:style="{ color, fontSize}"采用该种形式时,属性名称必须是CSS属性名称

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

丨Anna丨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值