How to implement Vue binding class and binding inline style

How to implement Vue binding class and binding inline style

Binding Class

Method 1:

Object syntax: pass an object to v-bind:class to dynamically switch classes
When the value corresponding to a key in the object is true, the key is added to the label as className

.box {
    width: 100px;
    height: 100px;
    background-color: gray;
}
.circle {
    border-radius: 50%;
}
<div id="app">
    <div class="box" @click="isCircle = !isCircle" :class="{circle:isCircle}"></div>
    <!--When isCircle is true, the class name of the div will be added with a circle-->
    <p>{{isCircle}}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{
            isCircle: false
        }
    })
</script>

Initially, the box is square. When clicked, it switches to a circle. Click again to switch to a square, and so on.

:class="{circle:isCircle}" can also be written to the computed property computed, returning this object

<div class="box" @click="isCircle = !isCircle" :class="divChange"></div>
computed:{
        divChange:function(){
            return {circle:this.isCircle}
        }
    }

Method 2:
Array syntax: pass an array to v-bind:class to apply a list of classes
CSS adds a blue background effect:

.blue {
    background-color: blue;
}
<div id="app">
    <div class="box" v-on:click="clickFun()" :class="[color,divChange()]"></div>
    <p>{{isCircle}}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{
            isCircle: false,
            color:""
        },
        methods:{
            divChange: function(){
                return {circle:this.isCircle}
            },
            clickFun: function(){
                this.isCircle = !this.isCircle;
                if (this.isCircle){
                    this.color = "blue"
                }else{
                    this.color = ""
                }
            }
        }
    })
</script>

Binding inline styles

Binding inline styles can also use object syntax or array syntax. Here is an example of a method that combines object syntax and calculated properties:

<div id="app">
 <!--Bind inline styles-->
    <div class="box" v-on:click="clickFun()" :style="divStyle"></div>
    <p>{{isCircle}}</p>
</div>

divStyle:

divStyle: function(){
    return {
        backgroundColor:this.color
    }
}

This way you can set the desired color to .box

Small demo:

1. When clicking the box, switch between circle and square - inline style binding
2. The initial box color is red. Click the start button to switch the color (red/blue) every second. Click the button again to cancel the color switch - class binding

.box {
    width: 100px;
    height: 100px;
    background-color: red;
}

.blue {
    background-color: blue;
}
<div id="app">
    <div class="box" :style="borderRadius" @click="changeStyle" :class="{blue: isBlue}"></div>
    <button @click="startClick">Start</button>
</div>
new Vue({
    el: "#app",
    data: {
        isCircle: false,
        borderRadius: { borderRadius: "50%" },
        isStart: false,
        isBlue: false,
        timer: null
    },

    methods: {
        changeStyle: function () {
            if (this.borderRadius.borderRadius == "0%") {
                this.borderRadius.borderRadius = "50%"
            } else {
                this.borderRadius.borderRadius = "0%"
            }
        },
        startClick: function () {
            this.isStart = !this.isStart;
        }
    },

    watch:
        isStart: function (val) {
            var vm = this;
            if (val) {
                console.log("Enable color switching")
                this.timer = setInterval(function () {
                    vm.isBlue = !vm.isBlue
                }, 1000)
            }else{
                console.log("Cancel timer, stop color switching")
                clearInterval(this.timer);
                this.timer = null;
            }

        }
    }
})

Effect:

This is the end of this article about how to implement Vue binding class and binding inline style. For more relevant Vue binding class and binding inline style content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of Vue.js style dynamic binding implementation
  • How to implement Vue style binding
  • Vue framework dynamic binding css style example analysis
  • Vue binding inline style problem
  • Detailed explanation of Vue's style binding

<<:  Example code for implementing dotted border scrolling effect with CSS

>>:  Solution to the problem that Docker cannot stop or delete container services

Recommend

Explanation of Dockerfile instructions and basic structure

Using Dockerfile allows users to create custom im...

Html+CSS floating advertisement strip implementation

1.html part Copy code The code is as follows: <...

A brief discussion on the role of the docker --privileged=true parameter

Around version 0.6, privileged was introduced to ...

js to achieve a simple lottery function

This article shares the specific code of js to im...

js to achieve image fade-in and fade-out effect

This article shares the specific code of js to ac...

Full process record of Nginx reverse proxy configuration

1. Preparation Install Tomcat on Linux system, us...

A brief discussion on docker-compose network settings

Networks usage tutorial Official website docker-c...

Detailed instructions for installing mysql5.7 database under centos7.2

The mysql on the server is installed with version...

Interpretation of syslogd and syslog.conf files under Linux

1: Introduction to syslog.conf For different type...

Don’t bother with JavaScript if you can do it with CSS

Preface Any application that can be written in Ja...

Common usage of regular expressions in Mysql

Common usage of Regexp in Mysql Fuzzy matching, c...

Deploy the Vue project on a Linux server

Case 1 vue-cli builds the vue3 project, uploads t...

This article helps you understand PReact10.5.13 source code

Table of contents render.js part create-context.j...

Basic installation tutorial of mysql decompression package

Since I have changed to a new computer, all the e...

HTML table tag tutorial (25): vertical alignment attribute VALIGN

In the vertical direction, you can set the row al...