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

How to install php7 + nginx environment under centos6.6

This article describes how to install php7 + ngin...

Detailed steps to install CentOS7 system on VMWare virtual machine

Pre-installation work: Make sure vmware workstati...

Summarize the User-Agent of popular browsers

1. Basic knowledge: Http Header User-Agent User A...

How to use Vue3 asynchronous data loading component suspense

Table of contents Preface Creating Components Sum...

How to completely delete and uninstall MySQL in Windows 10

Preface This article introduces a tutorial on how...

Use mysql to record the http GET request data returned from the url

Business scenario requirements and implementation...

JDBC Exploration SQLException Analysis

1. Overview of SQLException When an error occurs ...

How to use Vue-router routing

Table of contents 1. Description 2. Installation ...

How to use Xtrabackup to back up and restore MySQL

Table of contents 1. Backup 1.1 Fully prepared 1....

A practical record of checking and processing duplicate MySQL records on site

Table of contents Preface analyze Data Total Repe...

64-bit CentOs7 source code installation mysql-5.6.35 process sharing

First install the dependent packages to avoid pro...

HTML Table Tag Tutorial (47): Nested Tables

<br />In the page, typesetting is achieved b...