0
点赞
收藏
分享

微信扫一扫

Vue.js 样式绑定

西特张 2022-02-19 阅读 178

lass 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。

Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。

class属性绑定

①使用属性值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue测试</title>
    <script src="js/vue.js"></script>
    <style type="text/css">
        .active {
            width: 100px;
            height: 100px;
            background: green;
        }

        .active2 {
            width: 100px;
            height: 100px;
            background: red;
        }
    </style>
</head>
<body>
<div id="app">
    第一种方式:
    <div v-bind:class="isActive?'active':'active2'"></div>
    第二种方式:
    <div v-bind:class="{ 'active2': isActive,'active':!isActive }"></div>
    <br>
    <button v-on:click="modifyClass">改变样式</button>
</div>
<script type="text/javascript">
    new Vue({
        el: '#app',
        data: {
            isActive: true
        },
        methods: {
            modifyClass: function () {
                this.isActive = !this.isActive;
            }
        }
    })
</script>
</body>
</html>

img

②使用对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue测试</title>
    <script src="js/vue.js"></script>
    <style type="text/css">
        .active {
            width: 100px;
            height: 100px;
            background: green;
        }

        .active2 {
            width: 100px;
            height: 100px;
            background: red;
        }
    </style>
</head>
<body>
<div id="app">
    <p>classObject: { 'active': {{classObject.active}}, 'active2': {{classObject.active2}} }</p>
    <div v-bind:class="classObject"></div>
    <br>
    <button v-on:click="modifyClass">改变样式</button>
</div>
<script type="text/javascript">
    new Vue({
        el: '#app',
        data: {
            classObject: {
                'active': true,
                'active2': false
            }
        },
        methods: {
            modifyClass: function () {
                this.classObject.active = !this.classObject.active;
                this.classObject.active2 = !this.classObject.active2;
            }
        }
    })
</script>
</body>
</html>

img

③数组语法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue测试</title>
    <script src="js/vue.js"></script>
    <style type="text/css">
        .active {
            width: 100px;
            height: 100px;
        }

        .active2 {
            background: green;
        }
    </style>
</head>
<body>
<div id="app">
    <p>class="{{activeClass}} {{active2Class}}"</p>
    <div v-bind:class="[activeClass,active2Class]"></div>
</div>
<script type="text/javascript">
    new Vue({
        el: '#app',
        data: {
            activeClass: 'active',
            active2Class: 'active2'
        }
    })
</script>
</body>
</html>

img

Vue.js style(内联样式)

①在 v-bind:style 直接设置样式

<div id="app">
    <p v-bind:style="{color:fontColor,fontSize:fontSize+'px'}">LeDao的博客</p>
    <button v-on:click="bigFontSize">增大字号</button>
    <button v-on:click="smallFontSize">减小字号</button>
</div>
<script type="text/javascript">
    new Vue({
        el: '#app',
        data: {
            fontColor: 'red',
            fontSize: 15
        },
        methods: {
            bigFontSize: function () {
                this.fontSize++;
            },
            smallFontSize: function () {
                this.fontSize--;
            }
        }
    })
</script>

img

②绑定一个对象

<div id="app">
    <p v-bind:style="styleObject">LeDao的博客:http://www.zoutl.cn</p>
</div>
<script type="text/javascript">
    new Vue({
        el: '#app',
        data: {
            styleObject: {
                color: 'red',
                fontSize: '33px'
            }
        }
    })
</script>

③绑定多个对象

<div id="app">
    <p v-bind:style="[style1,style2]">LeDao的博客</p>
</div>
<script type="text/javascript">
    new Vue({
        el:'#app',
        data:{
            style1:{
                'color':'green',
                'font-size':'33px'
            },
            style2:{
                'font-weight':'bold'
            }
        }
    })
</script>
举报

相关推荐

0 条评论