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

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.

What is the difference between width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.