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

What is the difference between the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.