Vue實(shí)現(xiàn)動(dòng)態(tài)樣式的多種方法匯總
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)文章
vue給input file綁定函數(shù)獲取當(dāng)前上傳的對(duì)象完美實(shí)現(xiàn)方法
這篇文章主要介紹了vue給input file綁定函數(shù)獲取當(dāng)前上傳的對(duì)象完美實(shí)現(xiàn)方法,需要的朋友可以參考下2017-12-12
在Vue中使用SQLite數(shù)據(jù)庫(kù)的基礎(chǔ)應(yīng)用詳解
這篇文章主要為大家詳細(xì)介紹了在Vue中使用SQLite數(shù)據(jù)庫(kù)的基礎(chǔ)應(yīng)用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
Vite結(jié)合Vue刪除指定環(huán)境的console.log問(wèn)題
這篇文章主要介紹了Vite結(jié)合Vue刪除指定環(huán)境的console.log問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
詳解.vue文件中監(jiān)聽input輸入事件(oninput)
本篇文章主要介紹了詳解.vue文件中監(jiān)聽input輸入事件(oninput),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
vue之如何配置默認(rèn)顯示頁(yè)面和默認(rèn)路由
這篇文章主要介紹了vue之如何配置默認(rèn)顯示頁(yè)面和默認(rèn)路由問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

