最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue實(shí)現(xiàn)動(dòng)態(tài)樣式的多種方法匯總

 更新時(shí)間:2021年06月19日 08:49:04   作者:明天也要努力  
本文要給大家介紹Vue實(shí)現(xiàn)動(dòng)態(tài)樣式的多種方法,下面給大家?guī)韼讉€(gè)案列,需要的朋友可以借鑒研究一下。

1. 三元運(yùn)算符判斷

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

2. 動(dòng)態(tài)設(shè)置class

2.1 主要運(yùn)用于:實(shí)現(xiàn)循環(huán)列表中點(diǎn)擊時(shí),相應(yīng)的元素高亮;(默認(rèn)首個(gè)元素高亮)

<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 主要運(yùn)用于:為特定數(shù)值設(shè)置相應(yīng)樣式;

  <div 
    :class="activeId === item.id?'activeStyle':'machineBar'" 
    v-for="(item,index) in List" :key="index" @click="clickEvent">    
      <p>{{item.name}}</p>    
  </div>

3. 方法判斷

3.1 主要運(yùn)用于:為不同的數(shù)據(jù)值設(shè)置相應(yīng)的樣式;

<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('值',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 主要運(yùn)用于:在元素多從事件下,顯示相應(yīng)的樣式;

<template>
  <div 
      class="homeWrap" :class="{'select': selected === 1,'click': clicked === 1}" 
      @click="handleClick(1)" @mousedown="menuOnSelect(1)">
     主頁(yè)
  </div>   
</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. 數(shù)組綁定

<div :class="[isActive,isSort]"></div>
// 數(shù)組與三元運(yùn)算符結(jié)合判斷選擇需要的class
<div class="item" :class="[item.name? 'lg':'sm']"></div>
<div class="item" :class="[item.age<18? 'gray':'']"></div>
// 數(shù)組對(duì)象結(jié)合
<div :class="[{ active: isActive }, 'sort']"></div>

data() {
  return{
    isActive:'active',
    isSort:'sort'
 }
}
// css
.active{
    /*這里寫需要設(shè)置的第一種樣式*/
  } 
.sort{
    /*這里寫需要設(shè)置的第二種樣式*/
  }

5. computed結(jié)合es6對(duì)象的計(jì)算屬性名方法

 <div :class="classObject"></div>
 
  export default {
    data(){
      return{
        isActive:true
      }
    },
    computed:{
      classObject() {
        return{
          class_a:this.isActive,
          class_b:!this.isActive
        //  這里要結(jié)合自身項(xiàng)目情況修改填寫
        }
      }
    }
  }
 
// css
.class_a{
    /*這里寫需要設(shè)置的第一種樣式*/
}
 
.class_b{
   /*這里寫需要設(shè)置的第二種樣式*/
 }

以上就是Vue實(shí)現(xiàn)動(dòng)態(tài)樣式的多種方法匯總的詳細(xì)內(nèi)容,更多關(guān)于Vue實(shí)現(xiàn)動(dòng)態(tài)樣式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

广平县| 泰宁县| 休宁县| 武乡县| 炉霍县| 德江县| 巩义市| 中阳县| 冕宁县| 从化市| 高唐县| 临沂市| 鄯善县| 句容市| 常州市| 石泉县| 宁化县| 山阴县| 光泽县| 会东县| 曲松县| 海宁市| 高青县| 新河县| 明水县| 黑河市| 右玉县| 武安市| 台南县| 衡阳县| 潼关县| 北川| 徐州市| 运城市| 贡山| 文安县| 濮阳市| 平南县| 鲁山县| 馆陶县| 新建县|