Binding Class Method 1: Object syntax: pass an object to v-bind:class to dynamically switch classes .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: .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 .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:
|
<<: Example code for implementing dotted border scrolling effect with CSS
>>: Solution to the problem that Docker cannot stop or delete container services
Using Dockerfile allows users to create custom im...
1.html part Copy code The code is as follows: <...
Around version 0.6, privileged was introduced to ...
This article shares the specific code of js to im...
This article shares the specific code of js to ac...
1. Preparation Install Tomcat on Linux system, us...
Networks usage tutorial Official website docker-c...
The mysql on the server is installed with version...
1: Introduction to syslog.conf For different type...
Preface Any application that can be written in Ja...
Common usage of Regexp in Mysql Fuzzy matching, c...
Case 1 vue-cli builds the vue3 project, uploads t...
Table of contents render.js part create-context.j...
Since I have changed to a new computer, all the e...
In the vertical direction, you can set the row al...