vue、uniapp中動(dòng)態(tài)添加綁定style、class?9種實(shí)現(xiàn)方法
9種方法介紹
1.直接使用靜態(tài)class和style屬性:
- 使用場景:當(dāng)class和style屬性是固定不變的時(shí)候,可以直接在模板中寫死。
- 優(yōu)點(diǎn):簡單直接,沒有額外的計(jì)算和邏輯。
- 缺點(diǎn):無法根據(jù)條件動(dòng)態(tài)修改class和style。
2.使用v-bind動(dòng)態(tài)綁定class和style屬性:
- 使用場景:當(dāng)class和style屬性需要根據(jù)組件的data或props屬性動(dòng)態(tài)變化時(shí),可以使用v-bind來動(dòng)態(tài)綁定。
- 優(yōu)點(diǎn):可以根據(jù)條件動(dòng)態(tài)修改class和style。
- 缺點(diǎn):需要在模板中寫表達(dá)式,有一定的復(fù)雜度。
3.使用計(jì)算屬性來動(dòng)態(tài)生成class和style對象:
- 使用場景:當(dāng)class和style屬性的計(jì)算邏輯比較復(fù)雜時(shí),可以使用計(jì)算屬性來生成class和style對象。
- 優(yōu)點(diǎn):代碼可讀性好,邏輯清晰。
- 缺點(diǎn):需要定義額外的計(jì)算屬性。
4.使用動(dòng)態(tài)綁定的class和style屬性:
- 使用場景:當(dāng)class和style屬性的計(jì)算邏輯比較簡單時(shí),可以直接在模板中使用表達(dá)式來動(dòng)態(tài)生成class和style字符串。
- 優(yōu)點(diǎn):簡潔明了,沒有額外的計(jì)算屬性。
- 缺點(diǎn):邏輯稍微復(fù)雜時(shí),可讀性會(huì)變差。
5.使用數(shù)組語法來動(dòng)態(tài)綁定class屬性:
- 使用場景:當(dāng)class屬性需要根據(jù)多個(gè)條件動(dòng)態(tài)變化時(shí),可以使用數(shù)組語法來動(dòng)態(tài)綁定class屬性。
- 優(yōu)點(diǎn):可以根據(jù)多個(gè)條件動(dòng)態(tài)修改class。
- 缺點(diǎn):數(shù)組語法相對復(fù)雜,可讀性較差。
6.使用動(dòng)態(tài)綁定的style屬性:
- 使用場景:當(dāng)style屬性需要根據(jù)組件的data或props屬性動(dòng)態(tài)變化時(shí),可以使用動(dòng)態(tài)綁定的style屬性。
- 優(yōu)點(diǎn):可以根據(jù)條件動(dòng)態(tài)修改style。
- 缺點(diǎn):需要在模板中寫表達(dá)式,有一定的復(fù)雜度。
7.使用對象語法動(dòng)態(tài)綁定class屬性:
- 使用場景:當(dāng)class屬性需要根據(jù)多個(gè)條件動(dòng)態(tài)變化時(shí),可以使用對象語法來動(dòng)態(tài)綁定class屬性。
- 優(yōu)點(diǎn):可以根據(jù)多個(gè)條件動(dòng)態(tài)修改class。
- 缺點(diǎn):對象語法相對復(fù)雜,可讀性較差。
8.使用計(jì)算屬性動(dòng)態(tài)綁定style屬性:
- 使用場景:當(dāng)style屬性的計(jì)算邏輯比較復(fù)雜時(shí),可以使用計(jì)算屬性來動(dòng)態(tài)綁定style屬性。
- 優(yōu)點(diǎn):代碼可讀性好,邏輯清晰。
- 缺點(diǎn):需要定義額外的計(jì)算屬性。
9.使用動(dòng)態(tài)綁定的class和style屬性,通過在模板中使用數(shù)組和對象語法來動(dòng)態(tài)生成class和style字符串:
- 使用場景:當(dāng)class和style屬性需要根據(jù)多個(gè)條件動(dòng)態(tài)變化時(shí),可以使用數(shù)組和對象語法來動(dòng)態(tài)綁定class和style屬性。
- 優(yōu)點(diǎn):可以根據(jù)多個(gè)條件動(dòng)態(tài)修改class和style。
- 缺點(diǎn):語法相對復(fù)雜,可讀性較差。
具體實(shí)現(xiàn)
在Uniapp和Vue中,可以使用以下9種方法來動(dòng)態(tài)添加綁定style和class:
1.使用v-bind指令(或簡寫為:class和:style)來動(dòng)態(tài)綁定class和style屬性。可以通過計(jì)算屬性或直接在模板中使用三元表達(dá)式來根據(jù)條件動(dòng)態(tài)設(shè)置class和style屬性。
<template>
<div :class="{'active': isActive}" :style="{'color': textColor}">Hello World</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
textColor: 'red'
}
}
}
</script>2.使用v-bind指令(或簡寫為:class和:style)來動(dòng)態(tài)綁定class和style對象??梢栽赿ata中定義一個(gè)對象,根據(jù)條件動(dòng)態(tài)設(shè)置class和style對象的屬性。
<template>
<div :class="classObject" :style="styleObject">Hello World</div>
</template>
<script>
export default {
data() {
return {
classObject: {
active: true,
'text-color': true
},
styleObject: {
color: 'red',
fontSize: '16px'
}
}
}
}
</script>3.使用計(jì)算屬性來動(dòng)態(tài)生成class和style對象。可以根據(jù)組件的data或props屬性計(jì)算出class和style對象,并在模板中使用計(jì)算屬性。
<template>
<div :class="computedClass" :style="computedStyle">Hello World</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
textColor: 'red'
}
},
computed: {
computedClass() {
return {
active: this.isActive,
'text-color': this.textColor === 'red'
}
},
computedStyle() {
return {
color: this.textColor,
fontSize: '16px'
}
}
}
}
</script>4.使用動(dòng)態(tài)綁定的class和style屬性,通過在模板中使用表達(dá)式來動(dòng)態(tài)生成class和style字符串。
<template>
<div :class="'active ' + (isActive ? 'active' : '')" :style="'color: ' + textColor">Hello World</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
textColor: 'red'
}
}
}
</script>5.使用數(shù)組語法來動(dòng)態(tài)綁定class屬性??梢栽赿ata中定義一個(gè)數(shù)組,根據(jù)條件動(dòng)態(tài)設(shè)置class數(shù)組的元素。
<template>
<div :class="classArray">Hello World</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
isBold: true
}
},
computed: {
classArray() {
return ['active', {'bold': this.isBold}]
}
}
}
</script>6.使用動(dòng)態(tài)綁定的style屬性,通過在模板中使用對象語法來動(dòng)態(tài)生成style字符串。
<template>
<div :style="{ color: textColor, fontSize: fontSize + 'px' }">Hello World</div>
</template>
<script>
export default {
data() {
return {
textColor: 'red',
fontSize: 16
}
}
}
</script>7.使用對象語法動(dòng)態(tài)綁定class屬性??梢栽赿ata中定義一個(gè)對象,根據(jù)條件動(dòng)態(tài)設(shè)置class對象的屬性。
<template>
<div :class="classObject">Hello World</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
isBold: true
}
},
computed: {
classObject() {
return {
active: this.isActive,
bold: this.isBold
}
}
}
}
</script>8.使用計(jì)算屬性動(dòng)態(tài)綁定style屬性??梢愿鶕?jù)組件的data或props屬性計(jì)算出style字符串,并在模板中使用計(jì)算屬性。
<template>
<div :style="computedStyle">Hello World</div>
</template>
<script>
export default {
data() {
return {
textColor: 'red',
fontSize: 16
}
},
computed: {
computedStyle() {
return `color: ${this.textColor}; font-size: ${this.fontSize}px;`;
}
}
}
</script>9.使用動(dòng)態(tài)綁定的class和style屬性,通過在模板中使用數(shù)組和對象語法來動(dòng)態(tài)生成class和style字符串。
<template>
<div :class="['active', { 'bold': isBold }]">Hello World</div>
<div :style="[styleObject, { 'font-size': fontSize + 'px' }]">Hello World</div>
</template>
<script>
export default {
data() {
return {
isBold: true,
styleObject: {
color: 'red'
},
fontSize: 16
}
}
}
</script>到此這篇關(guān)于vue、uniapp中動(dòng)態(tài)添加綁定style、class 9種方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)uniapp動(dòng)態(tài)綁定style、class內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
談一談vue請求數(shù)據(jù)放在created好還是mounted里好
這篇文章主要介紹了談一談vue請求數(shù)據(jù)放在created好還是mounted里好的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue復(fù)合組件實(shí)現(xiàn)注冊表單功能
這篇文章主要為大家詳細(xì)介紹了vue復(fù)合組件實(shí)現(xiàn)注冊表單功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Element Table的row-class-name無效與動(dòng)態(tài)高亮顯示選中行背景色
這篇文章主要介紹了Element Table的row-class-name無效與動(dòng)態(tài)高亮顯示選中行背景色,本文詳細(xì)的介紹了解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
vue實(shí)現(xiàn)tab切換的3種方式及切換保持?jǐn)?shù)據(jù)狀態(tài)
這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)tab切換的3種方式及切換保持?jǐn)?shù)據(jù)狀態(tài)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
強(qiáng)制Vue重新渲染組件的5種方法小結(jié)
在開發(fā)Vue.js應(yīng)用程序時(shí),有時(shí)我們需要強(qiáng)制重新渲染組件以確保數(shù)據(jù)和視圖同步,下面就來介紹一下強(qiáng)制Vue重新渲染組件的5種方法,感興趣的可以了解一下2025-06-06
解決vue無法加載文件D:\Program Files\nodejs\node_global\vue.ps1,
這篇文章主要給大家介紹了關(guān)于解決vue無法加載文件D:\Program Files\nodejs\node_global\vue.ps1,因?yàn)樵诖讼到y(tǒng)上禁止運(yùn)行腳本的相關(guān)資料,這個(gè)報(bào)錯(cuò)是由于在系統(tǒng)上禁止運(yùn)行腳本導(dǎo)致的,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
iview實(shí)現(xiàn)動(dòng)態(tài)表單和自定義驗(yàn)證時(shí)間段重疊
這篇文章主要介紹了iview實(shí)現(xiàn)動(dòng)態(tài)表單和自定義驗(yàn)證時(shí)間段重疊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
uniapp+vue3實(shí)現(xiàn)上傳圖片組件封裝功能
這篇文章主要介紹了uniapp+vue3實(shí)現(xiàn)上傳圖片組件封裝功能,首先創(chuàng)建一個(gè)?components 文件在里面進(jìn)行組件的創(chuàng)建,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
Vue3實(shí)現(xiàn)動(dòng)態(tài)導(dǎo)入Excel表格數(shù)據(jù)的方法詳解
在開發(fā)工作過程中,我們會(huì)遇到各種各樣的表格數(shù)據(jù)導(dǎo)入,動(dòng)態(tài)數(shù)據(jù)導(dǎo)入可以減少人為操作,減少出錯(cuò)。本文為大家介紹了Vue3實(shí)現(xiàn)動(dòng)態(tài)導(dǎo)入Excel表格數(shù)據(jù)的方法,需要的可以參考一下2022-11-11

