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

Use of js array filter() method

 Arrays are frequently used in development, and processing data in arrays is one of the more common and important operations. Therefore, processing data in arrays during development is an important skill. Every developer Everyone should master the operations of arrays, especially for junior developers who have just entered the industry, so be sure to master the relevant skills. This article mainly shares some operations to filter the data in the array, about the use of the filter() method.

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.