美文网首页
Vue中的样式绑定

Vue中的样式绑定

作者: 云凡的云凡 | 来源:发表于2020-10-08 00:06 被阅读0次
    1.class的对象绑定
    image.png
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>8.Vue中的样式绑定</title>
        <script src="./vue.js"></script>
        <style>
            .activated {
                color: red;
            }
        </style>
    </head>
    
    <body>
        <div id="app">
            <h1 :class="{activated:isActivated}" @click="handleH1Click">hello world</h1>
        </div>
        <script>
            var vm = new Vue({
                el: '#app',
                data: {
                    isActivated: true
                },
                methods: {
                    handleH1Click() {
                        this.isActivated = !this.isActivated
                    }
                },
            })
        </script>
    </body>
    
    </html>
    
    2.class和数组绑定,数组的值是变量,class上会显示变量对应的内容.动态的向一个元素添加或者删除不同的类。从而实现页面效果的变更。
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>8.Vue中的样式绑定</title>
        <script src="./vue.js"></script>
        <style>
            .activated {
                color: red;
            }
        </style>
    </head>
    
    <body>
        <div id="app">
            <h1 :class="[activated,activatedOne]" @click="handleH1Click">hello world</h1>
        </div>
        <script>
            var vm = new Vue({
                el: '#app',
                data: {
                    activated: '',
                    activatedOne:'activated-one'
                },
                methods: {
                    handleH1Click() {
                        this.activated = this.activated === "activated" ? "" : "activated"
                    }
                },
            })
        </script>
    </body>
    
    </html>
    
    3.style改变样式(通过对象或者数组)
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>8.Vue中的样式绑定</title>
        <script src="./vue.js"></script>
    </head>
    
    <body>
        <div id="app">
            <!-- <h1 :style="styleObj" @click="handleClick">hello world</h1> -->
            <h1 :style="[styleObj,{fontSize:'100px'}]" @click="handleClick">hello world</h1>
        </div>
        <script>
            var vm = new Vue({
                el: '#app',
                data: {
                    styleObj: {
                        color: "black"
                    }
                },
                methods: {
                    handleClick: function () {
                        this.styleObj.color = this.styleObj.color = "black" ? "red" : "black"
                    }
                },
            })
        </script>
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:Vue中的样式绑定

          本文链接:https://www.haomeiwen.com/subject/pvwqpktx.html