1. Ternary operator judgment<text :style="{color:state?'#ff9933':'#ff0000'}">hello world </text> <script> export default { data() { return { state: true } } } </script> 2. Dynamically set class2.1 Mainly used for: When clicking in a loop list, the corresponding element is highlighted; (the first element is highlighted by default) <template> <div class="wrapper" v-for="(item,index) in houseList" :key="index" @click="rightTap(index)"> <div class="houseTitle" :class="{'active' : index === rightIndex}"> {{item.name}} </div> </div> </template> <script> export default { data() { return { rightIndex:0, houseList:[] }; }, methods:{ rightTap(index){ this.rightIndex = index } } } </script> <style lang="scss" scoped> .wrapper{ width: 118px; height: 60px; margin: 6px auto 0 auto; .houseTitle{ font-size: 22px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .active{ color:#2a7ffa; background-color: pink; } } </style> 2.2 Mainly used for: setting corresponding styles for specific values; <div :class="activeId === item.id?'activeStyle':'machineBar'" v-for="(item,index) in List" :key="index" @click="clickEvent"> <p>{{item.name}}</p> </div> 3. Method judgment3.1 Mainly used for: setting corresponding styles for different data values; <template> <div v-for="(item,index) in houseList" :key="index"> <div :style="getStyle(item.status)">{{item.name}}</div> </div> </template> <script> export default { data(){ return { houseList:[ { id:1, name:1, status:'a' },{ id:2, name:2, status:'b' },{ id:3, name:3, status:'c' } ] } }, methods:{ getStyle(e){ console.log('value',e) if(e === 'a'){ return { width:'60px', height:'60px', background:'yellow', margin: '10px auto' } }else if(e === 'b'){ return { width:'60px', height:'60px', background:'red', margin: '10px auto' } }else if(e === 'c'){ return { width:'60px', height:'60px', background:'pink', margin: '10px auto' } } } } } </script> 3.2 Mainly used for: displaying corresponding styles under multiple element events; <template> <div class="homeWrap" :class="{'select': selected === 1,'click': clicked === 1}" @click="handleClick(1)" @mousedown="menuOnSelect(1)"> Home page </template> <script> export default { return { selected: 0, clicked: 0 } methods:{ menuOnSelect(value){ this.selected = value; }, handleClick(value){ this.selected = 0 this.clicked = value } } } </script> <style lang="stylus" scoped> .homeWrap.select background red .homeWrap.click background yellow </style> 4. Array Binding<div :class="[isActive,isSort]"></div> // Combine the array with the ternary operator to determine the required class <div class="item" :class="[item.name? 'lg':'sm']"></div> <div class="item" :class="[item.age<18? 'gray':'']"></div> // Array object combined with <div :class="[{ active: isActive }, 'sort']"></div> data() { return { isActive:'active', isSort:'sort' } } // css .active{ /*Write the first style you need to set here*/ } .sort{ /*Write the second style that needs to be set here*/ } 5. Computed property name method combined with es6 object<div :class="classObject"></div> export default { data(){ return { isActive:true } }, computed:{ classObject() { return { class_a:this.isActive, class_b:!this.isActive // This needs to be modified and filled in according to the situation of your own project} } } } // css .class_a{ /*Write the first style you need to set here*/ } .class_b{ /*Write the second style that needs to be set here*/ } The above is the detailed content of the various methods of Vue to achieve dynamic styles. For more information about Vue to achieve dynamic styles, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Linux remote control windows system program (three methods)
>>: A magical MySQL deadlock troubleshooting record
Introduction to common Dockerfile instructions in...
This article describes how to install php7 + ngin...
Pre-installation work: Make sure vmware workstati...
1. Basic knowledge: Http Header User-Agent User A...
Table of contents Preface Creating Components Sum...
Preface This article introduces a tutorial on how...
Business scenario requirements and implementation...
In the front-end design draft, you can often see ...
1. Overview of SQLException When an error occurs ...
Table of contents 1. Description 2. Installation ...
Table of contents 1. Backup 1.1 Fully prepared 1....
Table of contents Preface analyze Data Total Repe...
Because we were going to build a website, in addi...
First install the dependent packages to avoid pro...
<br />In the page, typesetting is achieved b...