Summary of various methods for Vue to achieve dynamic styles

Summary of various methods for Vue to achieve dynamic styles

1. Ternary operator judgment

<text :style="{color:state?'#ff9933':'#ff0000'}">hello world </text>
<script>
export default {
	data() {
		return {
			state: true
		}
	}
}
</script>

2. Dynamically set class

2.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 judgment

3.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:
  • Detailed explanation of the method code for dynamically binding class and style in Vue
  • Summary of Vue.js style dynamic binding implementation
  • Vue implements conditional judgment and dynamic binding style method
  • How to dynamically bind style in Vue

<<:  Linux remote control windows system program (three methods)

>>:  A magical MySQL deadlock troubleshooting record

Recommend

Code to display the contents of a txt book on a web page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

Native JS to implement click number game

Native JS implements the click number game for yo...

Web Design Help: Web Font Size Data Reference

<br />The content is reproduced from the Int...

Detailed explanation of script debugging mechanism in bash

Run the script in debug mode You can run the enti...

Object-Oriented Programming with XHTML and CSS

<br />If only XHTML and CSS were object-orie...

Brief analysis of the MySQL character set causing database recovery errors

Importing data with incorrect MySQL character set...

Detailed summary of mysql sql statements to create tables

mysql create table sql statement Common SQL state...

Records of using ssh commands on Windows 8

1. Open the virtual machine and git bash window a...

A brief discussion on the $notify points of element

My original intention was to encapsulate the $not...

404 error occurs when accessing the homepage of tomcat started in Docker mode

Scenario: When starting tomcat in docker (version...

Implementation of local migration of docker images

I've been learning Docker recently, and I oft...

nginx+tomcat example of accessing the project through the domain name

I was curious about how to access the project usi...