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
Table of contents 1. The origin of fork 2. Early ...
1. Install the virtual machine hyper-v that comes...
Preface MySQL slow query log is a function that w...
This article shares the implementation method of ...
Knowing which for loop or iterator is right for o...
What is MIME TYPE? 1. First, we need to understan...
This article example shares the specific code of ...
1. Requirements When using the Vue.js framework t...
This article example shares the specific code of ...
1. Introduction to Data Integrity 1. Introduction...
HTML structure <body> <div class="w...
background Some time ago, our project team was he...
Create a new table CREATE TABLE `person` ( `id` i...
Nginx: PV, UV, independent IP Everyone who makes ...
Table of contents Ideas Host Configuration Modify...