Vue樣式綁定的幾種實(shí)現(xiàn)方式
前言
樣式綁定在vue中屬于一種很常見的操作。在之前博客中針對樣式的綁定操作,介紹了一個(gè)指令v-bind
。縮寫為:xxx
。
往期回顧
先簡單回顧下最開始綁定標(biāo)簽樣式的操作,當(dāng)時(shí)是采取指定標(biāo)簽的class屬性作為樣式的修改。
<template> <h1>標(biāo)簽動(dòng)態(tài)屬性</h1> <!-- <div v-bind:class="dynamicClass"></div> <br/> <div :class="dynamicClass"></div> <br/> <div :class="!isShow?'green':'red'"></div> <br/> --> <div v-bind="dynamicMoreBind"></div> </template>
<script > export default{ data(){ return{ dynamicClass:"appClass", isShow:true, // 同標(biāo)簽 多屬性值綁定 可以采取封裝對象的形式實(shí)現(xiàn) dynamicMoreBind:{ id:"moreId", class:"appClass" } } } } </script>
<style> .appClass{ color:aqua; border: solid 1px; height: 10%; width: 20%; } .green{ color: green; border: solid 1px; height: 10%; width: 20%; } .red{ color: red; border: solid 1px; height: 10%; width: 20%; } </style>
但使用拼接字符串的方式進(jìn)行復(fù)雜樣式的綁定,是很容易出現(xiàn)問題的。
因此,vue為class
的v-bind
指令做了功能增強(qiáng),除了使用字符串之外,表達(dá)式的值也可以是對象或數(shù)組。
綁定對象
v-bind
指令,動(dòng)態(tài)綁定元素屬性的時(shí)候,可以采取如下的方式進(jìn)行對象信息的綁定。
<template> <h1>綁定樣式class</h1> <hr> <h2>綁定對象</h2> <div :class="{active:isActive,'text-danger':hasError}">6666666</div> </template>
<script> export default{ data(){ return{ isActive:true, // 如果為true 則使用 .active ,否則不使用 hasError:true } } } </script>
<style> .text-danger{ color: red; } .active{ font-size: 50px } </style>
其中,:class="{active:isActive,'text-danger':hasError}"
中,對象的各個(gè)屬性值的含義如下:
- 當(dāng)變量 isActive 為 true 時(shí),則會(huì)使用 active 樣式
- 當(dāng)變量 hasError 為 true 時(shí),則會(huì)使用 text-danger 樣式
效果如下所示:
若其中某個(gè)樣式的值為false,顯示效果如下:
綁定對象的另一種寫法
上述的綁定操作中,如果出現(xiàn)很多的對象屬性時(shí),采取該方式依舊不能很好的閱讀代碼和維護(hù)。可以將對象定義在tata區(qū)中。
如下所示:
<template> <h1>綁定樣式class</h1> <hr> <h2>綁定對象</h2> <!-- <div :class="{active:isActive,'text-danger':hasError}">6666666</div> --> <div :class="objClass">7777777</div> </template>
<script> export default{ data(){ return{ isActive:false, // 如果為true 則使用 .active ,否則不使用 hasError:true, objClass:{ 'active':true, // 如果為true 則使用 .active ,否則不使用 'text-danger':true } } } } </script>
<style> .text-danger{ color: red; } .active{ font-size: 50px } </style>
綁定數(shù)組
數(shù)組樣式數(shù)據(jù),綁定class。如果以其他的語言比如Java來看這個(gè)問題,其實(shí)數(shù)組也是對象的一種形式。
前端代碼如下所示:
<template> <h1>綁定樣式class</h1> <hr> <h2>綁定對象</h2> <!-- <div :class="{active:isActive,'text-danger':hasError}">6666666</div> --> <div :class="objClass">7777777</div> <h2>綁定數(shù)組</h2> <div :class="['active','text-danger']">數(shù)組樣式數(shù)據(jù)綁定1</div> <div :class="arryActClass">數(shù)組樣式數(shù)據(jù)綁定2</div> </template>
<script> export default{ data(){ return{ isActive:false, // 如果為true 則使用 .active ,否則不使用 hasError:true, objClass:{ 'active':true, // 如果為true 則使用 .active ,否則不使用 'text-danger':true }, arryActClass:['active','text-danger'] } } } </script>
<style> .text-danger{ color: red; } .active{ font-size: 50px } </style>
展示效果如下:
:class
綁定數(shù)組類型數(shù)據(jù)的寫法,這兩種效果是一樣的。
在數(shù)組對象的寫法中,還支持三目表達(dá)式的語法,如下所示:
<template> <h1>綁定樣式class</h1> <hr> <h2>綁定對象</h2> <!-- <div :class="{active:isActive,'text-danger':hasError}">6666666</div> --> <div :class="objClass">7777777</div> <h2>綁定數(shù)組</h2> <div :class="['active','text-danger']">數(shù)組樣式數(shù)據(jù)綁定1</div> <div :class="arryActClass">數(shù)組樣式數(shù)據(jù)綁定2</div> <div :class="[isActive?'active text-danger':'green']">數(shù)組樣式數(shù)據(jù)綁定3</div> </template>
<script> export default{ data(){ return{ isActive:false, // 如果為true 則使用 .active ,否則不使用 hasError:true, objClass:{ 'active':true, // 如果為true 則使用 .active ,否則不使用 'text-danger':true }, arryActClass:['active','text-danger'] } } } </script>
<style> .text-danger{ color: red; } .active{ font-size: 50px } .green{ color: green; } </style>
數(shù)組與對象的嵌套
數(shù)組與對象嵌套的方式,也能進(jìn)行標(biāo)簽樣式的綁定。但這里需要注意一個(gè)細(xì)節(jié)。
只能是數(shù)組嵌套對象。
主要寫法如下所示:
:class="['active',{green:isGreen}]"
案例如下所示:
<template> <h1>綁定樣式class</h1> <hr> <h2>綁定對象</h2> <!-- <div :class="{active:isActive,'text-danger':hasError}">6666666</div> --> <div :class="objClass">7777777</div> <h2>綁定數(shù)組</h2> <div :class="['active','text-danger']">數(shù)組樣式數(shù)據(jù)綁定1</div> <div :class="arryActClass">數(shù)組樣式數(shù)據(jù)綁定2</div> <div :class="[isActive?'active text-danger':'green']">數(shù)組樣式數(shù)據(jù)綁定3</div> <h2>數(shù)組嵌套對象</h2> <!-- isGreen 變量值為true時(shí),使用 green 樣式 --> <div :class="['active',{green:isGreen}]">數(shù)組嵌套對象</div> </template>
<script> export default{ data(){ return{ isActive:false, // 如果為true 則使用 .active ,否則不使用 hasError:true, objClass:{ 'active':true, // 如果為true 則使用 .active ,否則不使用 'text-danger':true }, arryActClass:['active','text-danger'], isGreen:true } } } </script>
<style> .text-danger{ color: red; } .active{ font-size: 50px } .green{ color: green; } </style>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js-div滾動(dòng)條隱藏但有滾動(dòng)效果的實(shí)現(xiàn)方法
下面小編就為大家分享一篇vue.js-div滾動(dòng)條隱藏但有滾動(dòng)效果的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03基于vue+uniapp直播項(xiàng)目實(shí)現(xiàn)uni-app仿抖音/陌陌直播室功能
uni-liveShow是一個(gè)基于vue+uni-app技術(shù)開發(fā)的集小視頻/IM聊天/直播等功能于一體的微直播項(xiàng)目。本文通過實(shí)例圖文的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-11-11vue實(shí)現(xiàn)登錄時(shí)的圖片驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)登錄時(shí)的圖片驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Vue代理請求數(shù)據(jù)出現(xiàn)404問題及解決
這篇文章主要介紹了Vue代理請求數(shù)據(jù)出現(xiàn)404的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07element-plus 如何設(shè)置 el-date-picker 彈出框位置
el-date-picker 組件會(huì)自動(dòng)根據(jù)空間范圍進(jìn)行選擇比較好的彈出位置,但特定情況下,它自動(dòng)計(jì)算出的彈出位置并不符合我們的實(shí)際需求,故需要我們手動(dòng)設(shè)置,這篇文章主要介紹了element-plus 如何設(shè)置 el-date-picker 彈出框位置,需要的朋友可以參考下2024-07-07vue3中reactive的對象清空所引發(fā)的問題解決方案(清空不了和清空之后再去賦值就賦值不了)
在使用reactive定義的變量時(shí),直接賦值會(huì)失去響應(yīng)式,為了清空?filters并確保響應(yīng)式,可以使用Object.assign({},?filters)或者遍歷對象逐個(gè)清除屬性,本文介紹vue3中reactive的對象清空所引發(fā)的問題解決方案(清空不了和清空之后再去賦值就賦值不了),感興趣的朋友一起看看吧2025-02-02解決$store.getters調(diào)用不執(zhí)行的問題
今天小編就為大家分享一篇解決$store.getters調(diào)用不執(zhí)行的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue 全家桶實(shí)現(xiàn)移動(dòng)端酷狗音樂功能
這篇文章主要介紹了Vue 全家桶實(shí)現(xiàn)移動(dòng)端酷狗音樂功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11Vue中import from的來源及省略后綴與加載文件夾問題
這篇文章主要介紹了Vue中import from的來源--省略后綴與加載文件夾,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02