欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue、uniapp中動(dòng)態(tài)添加綁定style、class?9種實(shí)現(xiàn)方法

 更新時(shí)間:2023年09月02日 10:34:58   作者:一花一world  
這篇文章主要介紹了vue、uniapp中動(dòng)態(tài)添加綁定style、class?9種方法實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

9種方法介紹

1.直接使用靜態(tài)class和style屬性:

  • 使用場(chǎng)景:當(dāng)class和style屬性是固定不變的時(shí)候,可以直接在模板中寫死。
  • 優(yōu)點(diǎn):簡(jiǎn)單直接,沒(méi)有額外的計(jì)算和邏輯。
  • 缺點(diǎn):無(wú)法根據(jù)條件動(dòng)態(tài)修改class和style。

2.使用v-bind動(dòng)態(tài)綁定class和style屬性:

  • 使用場(chǎng)景:當(dāng)class和style屬性需要根據(jù)組件的data或props屬性動(dòng)態(tài)變化時(shí),可以使用v-bind來(lái)動(dòng)態(tài)綁定。
  • 優(yōu)點(diǎn):可以根據(jù)條件動(dòng)態(tài)修改class和style。
  • 缺點(diǎn):需要在模板中寫表達(dá)式,有一定的復(fù)雜度。

3.使用計(jì)算屬性來(lái)動(dòng)態(tài)生成class和style對(duì)象:

  • 使用場(chǎng)景:當(dāng)class和style屬性的計(jì)算邏輯比較復(fù)雜時(shí),可以使用計(jì)算屬性來(lái)生成class和style對(duì)象。
  • 優(yōu)點(diǎn):代碼可讀性好,邏輯清晰。
  • 缺點(diǎn):需要定義額外的計(jì)算屬性。

4.使用動(dòng)態(tài)綁定的class和style屬性:

  • 使用場(chǎng)景:當(dāng)class和style屬性的計(jì)算邏輯比較簡(jiǎn)單時(shí),可以直接在模板中使用表達(dá)式來(lái)動(dòng)態(tài)生成class和style字符串。
  • 優(yōu)點(diǎn):簡(jiǎn)潔明了,沒(méi)有額外的計(jì)算屬性。
  • 缺點(diǎn):邏輯稍微復(fù)雜時(shí),可讀性會(huì)變差。

5.使用數(shù)組語(yǔ)法來(lái)動(dòng)態(tài)綁定class屬性:

  • 使用場(chǎng)景:當(dāng)class屬性需要根據(jù)多個(gè)條件動(dòng)態(tài)變化時(shí),可以使用數(shù)組語(yǔ)法來(lái)動(dòng)態(tài)綁定class屬性。
  • 優(yōu)點(diǎn):可以根據(jù)多個(gè)條件動(dòng)態(tài)修改class。
  • 缺點(diǎn):數(shù)組語(yǔ)法相對(duì)復(fù)雜,可讀性較差。

6.使用動(dòng)態(tài)綁定的style屬性:

  • 使用場(chǎng)景:當(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.使用對(duì)象語(yǔ)法動(dòng)態(tài)綁定class屬性:

  • 使用場(chǎng)景:當(dāng)class屬性需要根據(jù)多個(gè)條件動(dòng)態(tài)變化時(shí),可以使用對(duì)象語(yǔ)法來(lái)動(dòng)態(tài)綁定class屬性。
  • 優(yōu)點(diǎn):可以根據(jù)多個(gè)條件動(dòng)態(tài)修改class。
  • 缺點(diǎn):對(duì)象語(yǔ)法相對(duì)復(fù)雜,可讀性較差。

8.使用計(jì)算屬性動(dòng)態(tài)綁定style屬性:

  • 使用場(chǎng)景:當(dāng)style屬性的計(jì)算邏輯比較復(fù)雜時(shí),可以使用計(jì)算屬性來(lái)動(dòng)態(tài)綁定style屬性。
  • 優(yōu)點(diǎn):代碼可讀性好,邏輯清晰。
  • 缺點(diǎn):需要定義額外的計(jì)算屬性。

9.使用動(dòng)態(tài)綁定的class和style屬性,通過(guò)在模板中使用數(shù)組和對(duì)象語(yǔ)法來(lái)動(dòng)態(tài)生成class和style字符串:

  • 使用場(chǎng)景:當(dāng)class和style屬性需要根據(jù)多個(gè)條件動(dòng)態(tài)變化時(shí),可以使用數(shù)組和對(duì)象語(yǔ)法來(lái)動(dòng)態(tài)綁定class和style屬性。
  • 優(yōu)點(diǎn):可以根據(jù)多個(gè)條件動(dòng)態(tài)修改class和style。
  • 缺點(diǎn):語(yǔ)法相對(duì)復(fù)雜,可讀性較差。

具體實(shí)現(xiàn)

在Uniapp和Vue中,可以使用以下9種方法來(lái)動(dòng)態(tài)添加綁定style和class:

1.使用v-bind指令(或簡(jiǎn)寫為:class:style)來(lái)動(dòng)態(tài)綁定class和style屬性??梢酝ㄟ^(guò)計(jì)算屬性或直接在模板中使用三元表達(dá)式來(lái)根據(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指令(或簡(jiǎn)寫為:class:style)來(lái)動(dòng)態(tài)綁定class和style對(duì)象。可以在data中定義一個(gè)對(duì)象,根據(jù)條件動(dòng)態(tài)設(shè)置class和style對(duì)象的屬性。

<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ì)算屬性來(lái)動(dòng)態(tài)生成class和style對(duì)象??梢愿鶕?jù)組件的data或props屬性計(jì)算出class和style對(duì)象,并在模板中使用計(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屬性,通過(guò)在模板中使用表達(dá)式來(lái)動(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ù)組語(yǔ)法來(lái)動(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屬性,通過(guò)在模板中使用對(duì)象語(yǔ)法來(lái)動(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.使用對(duì)象語(yǔ)法動(dòng)態(tài)綁定class屬性??梢栽赿ata中定義一個(gè)對(duì)象,根據(jù)條件動(dòng)態(tài)設(shè)置class對(duì)象的屬性。

<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屬性,通過(guò)在模板中使用數(shù)組和對(duì)象語(yǔ)法來(lái)動(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Nuxt.js之自動(dòng)路由原理的實(shí)現(xiàn)方法

    Nuxt.js之自動(dòng)路由原理的實(shí)現(xiàn)方法

    這篇文章主要介紹了Nuxt.js之自動(dòng)路由原理的實(shí)現(xiàn)方法,nuxt.js會(huì)根據(jù)pages目錄結(jié)構(gòu)自動(dòng)生成vue-router模塊的路由配置。非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-11-11
  • vue render函數(shù)動(dòng)態(tài)加載img的src路徑操作

    vue render函數(shù)動(dòng)態(tài)加載img的src路徑操作

    這篇文章主要介紹了vue render函數(shù)動(dòng)態(tài)加載img的src路徑操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 如何使用vite搭建vue3項(xiàng)目詳解

    如何使用vite搭建vue3項(xiàng)目詳解

    Vite 是一個(gè)面向現(xiàn)代瀏覽器的更輕,更快的web應(yīng)用開(kāi)發(fā)工具,下面這篇文章主要給大家介紹了關(guān)于如何使用vite搭建vue3項(xiàng)目的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • 使用命令行工具npm新創(chuàng)建一個(gè)vue項(xiàng)目的方法

    使用命令行工具npm新創(chuàng)建一個(gè)vue項(xiàng)目的方法

    Vue.js 提供一個(gè)官方命令行工具,可用于快速搭建大型單頁(yè)應(yīng)用。下面小編給大家分享使用命令行工具npm新創(chuàng)建一個(gè)vue項(xiàng)目的方法,需要的朋友參考下吧
    2017-12-12
  • vue echarts實(shí)現(xiàn)橫向柱狀圖

    vue echarts實(shí)現(xiàn)橫向柱狀圖

    這篇文章主要為大家詳細(xì)介紹了vue echarts實(shí)現(xiàn)橫向柱狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue prop傳值類型檢驗(yàn)方式

    vue prop傳值類型檢驗(yàn)方式

    這篇文章主要介紹了vue prop傳值類型檢驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • 深入淺析vue組件間事件傳遞

    深入淺析vue組件間事件傳遞

    最近的工作需要用到vue,所以最近接觸最多的就是vue,下面小編給大家介紹下vue組件間事件傳遞,需要的朋友參考下吧
    2017-12-12
  • 使用vuex存儲(chǔ)用戶信息到localStorage的實(shí)例

    使用vuex存儲(chǔ)用戶信息到localStorage的實(shí)例

    今天小編就為大家分享一篇使用vuex存儲(chǔ)用戶信息到localStorage的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Vue如何獲取下拉框中選中的value值和label值

    Vue如何獲取下拉框中選中的value值和label值

    這篇文章主要介紹了Vue如何獲取下拉框中選中的value值和label值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue實(shí)現(xiàn)水波漣漪效果的點(diǎn)擊反饋指令

    vue實(shí)現(xiàn)水波漣漪效果的點(diǎn)擊反饋指令

    鼠標(biāo)移入時(shí)的小手、鼠標(biāo)點(diǎn)擊時(shí)按鈕下壓彈起的動(dòng)畫、觸屏應(yīng)用點(diǎn)擊時(shí)的屏幕震動(dòng),這些效果也被統(tǒng)稱為點(diǎn)擊反饋,雖然看似是應(yīng)用中的細(xì)枝末節(jié),但是只要稍微投入一點(diǎn)點(diǎn)心思,帶來(lái)的用戶體驗(yàn)提升是十分明顯的,這里作者為小伙伴們推薦一種作者最喜歡的點(diǎn)擊反饋效果。
    2021-05-05

最新評(píng)論