Skip to main content

How to dynamically bind css and style styles in Vue

 This article mainly introduces the method of dynamically binding CSS styles in the Vue framework. By sharing simple examples, we gradually understand the value transfer and binding data in Vue. The introduction is very detailed and has certain reference value. Friends who need it can refer to it. First of all, let's talk about dynamic binding. Relatively, everyone knows static binding. For static binding, you can directly add class="". Dynamic binding must be based on a certain operation of the user to dynamically modify the style, which is more flexible. convenient.

Mainly introduces three binding methods, string, array, object; the specific code is as follows.

    <style>
        .one{
            color: pink;
        }
        .two{
            color:  red;
        }
        .three{
            border: 1px solid;
        }
        .four{
            width: 100px;
            height: 200px;
            background-color: aquamarine;
        }
        .five{
            font-size: 32px;
        }
    </style>
</head>

<body>
    <div id="app">
        <p class="one" v-bind:class="twoStyle">change</p>
        <p class="one" v-bind:class="arr">change</p>
        <p class="one" v-bind:class="objColor">change</p>
        <h2 :style="FSizetwo">FFONT-SIZE{{FSizetwo}}</h2>
        <h2 :style="FSizethree">FFONT-SIZE</h2>
        <button @click="changeStyle">click1</button>
        <button v-on:click="randStyle()">click2</button>
        <button v-on:click="arrStyle()">click3</button><br/>
        input p4 font-size:<input type="text" v-model:value="FontSize">
    </div>
    <script>
     Vue.config.productionTip=false
        var vm = new Vue({
            el: '#app',
            data: {
                twoStyle:"two",
                objColor:{
                    two:false,
                    five:true
                },
                FontSize:'16px',
                arr:[],
/*                 FSizeone:{
                    fontSize:"fontSize:"+this.FontSize//undefined????Why???
                } */
            },
            methods: {
                changeStyle(){
                    this.twoStyle="three";
                },
                randStyle(){
                    const arr=["three","four","five"];
                    const index=(Math.random()*3)|0;
                    this.twoStyle=arr[index];
                },
                arrStyle(){
                    this.arr=["three","four","five"];
                }
            },
            computed:{
                    FSizetwo(){
                        return "fontSize:"+this.FontSize
                    },
                    FSizethree(){
                        return {
                            fontSize:this.FontSize,
                            color:"red"
                        }
                    }
                }
        });
    </script>

Comments

Popular posts from this blog

vue routing global guard beforeEach and afterEach

 Global routing front guard (beforeEach) This function is used the most. Its function is to perform permission-related verification before routing jumps. This function contains three parameters: to: the object of the target route that is about to enter; from: the route that the current route is leaving; next: confirm the release. It can be used to log in and register, to determine whether there is a token before logging in, and release if it exists. , if it does not exist, it will not be released. The post routing guard (afterEach), its role is to trigger after the routing jump.

ES6 arrow functions

 In ES6, in addition to the new features of let and const, arrow functions are the most frequently used new features. But this feature is not unique to ES6. Arrow functions, as the name suggests, are functions defined using arrows (=>) and belong to a class of anonymous functions. It allows us to write smaller function syntaxes. The code of arrow functions is simpler and more flexible to write.