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

Using Docker to create static website applications (multiple ways)

There are many servers that can host static websi...

Detailed explanation of viewing and setting SQL Mode in MySQL

Viewing and Setting SQL Mode in MySQL MySQL can r...

Complete steps to implement face recognition login in Ubuntu

1. Install Howdy: howdy project address sudo add-...

Detailed explanation of several methods of deduplication in Javascript array

Table of contents Array deduplication 1 Double-la...

Detailed tutorial on installing mysql 8.0.20 on CentOS7.8

1. Install MySQL software Download and install My...

Multiple solutions for cross-domain reasons in web development

Table of contents Cross-domain reasons JSONP Ngin...

Installation tutorial of mysql8.0rpm on centos7

First, download the diagram 1. First uninstall th...

CSS sample code to achieve circular gradient progress bar effect

Implementation ideas The outermost is a big circl...

How to deploy LNMP architecture in docker

Environmental requirements: IP hostname 192.168.1...

MySQL 8.0.15 installation and configuration graphic tutorial

This article records the installation and configu...

Implementation steps for installing Redis container in Docker

Table of contents Install Redis on Docker 1. Find...

Several popular website navigation directions in the future

<br />This is not only an era of information...

A brief discussion on browser compatibility issues in JavaScript

Browser compatibility is the most important part ...