绑定class、style内联

2018-09-24  本文已影响0人  真的吗_a951

对象语法:

<!--html-->
<button :class="{bgColor : isRed,bg2 : isPink}" @click="change">点我换颜色</button>
//vue.js
var app = new Vue({
            el: '#root',
            data: {
                isRed: true,
                isPink: false
            },
            methods: {
                change: function(){
                    if(this.isRed)  {
                        this.isRed = false
                        this.isPink = true
                    }else{
                        this.isRed = true
                        this.isPink = false
                    }
                    
                }
            }
        })
<!--html-->
<div id="root">
        <div :style="{'color':color,'fontSize':fontSize + 'px'}">1234565</div>
    </div>
//vue.js
var app = new Vue({
            el: '#root',
            data: {
                color: 'pink',
                fontSize: 18
            }
        })

数组语法:

<!--html-->
<div :class="[style1,style2]">数组语法</div>
//vue.js
var app = new Vue({
            el: '#root',
            data: {
                style1: 'style1',
                style2: 'style2'
            }
})
/*css*/
.style1 {
    font-size: 20px;
}
.style2 {
    color: yellowgreen;
}
<!--html-->
    <div id="root">
        <div :style ="[style1,style2]">数组</div>
    </div>
//vue.js
var app = new Vue({
            el: '#root',
            data: {
                style1: {
                    color: 'yellowgreen',
                    fontSize: 18 + 'px'
                },
                style2: {
                    backgroundColor: '#ccc',
                }
            },
        })
上一篇 下一篇

猜你喜欢

热点阅读