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

Hidden overhead of Unix/Linux forks

Table of contents 1. The origin of fork 2. Early ...

MySQL slow query log configuration and usage tutorial

Preface MySQL slow query log is a function that w...

Native JavaScript carousel implementation method

This article shares the implementation method of ...

Which loop is the fastest in JavaScript?

Knowing which for loop or iterator is right for o...

The Complete List of MIME Types

What is MIME TYPE? 1. First, we need to understan...

Vue implements login type switching

This article example shares the specific code of ...

How to encapsulate axios in Vue project (unified management of http requests)

1. Requirements When using the Vue.js framework t...

Vue implements a simple shopping cart example

This article example shares the specific code of ...

CSS implements six adaptive two-column layout methods

HTML structure <body> <div class="w...

Methods to enhance access control security in Linux kernel

background Some time ago, our project team was he...

Three ways to prevent MySQL from inserting duplicate data

Create a new table CREATE TABLE `person` ( `id` i...

MySQL configuration master-slave server (one master and multiple slaves)

Table of contents Ideas Host Configuration Modify...