Vue中Class和Style實(shí)現(xiàn)v-bind綁定的幾種用法
項(xiàng)目開(kāi)發(fā)中給元素添加/刪除 class 是非常常見(jiàn)的行為之一, 例如網(wǎng)站導(dǎo)航都會(huì)給選中項(xiàng)添加一個(gè) active 類(lèi)用來(lái)區(qū)別選與未選中的樣式,除了導(dǎo)航之外其他很多地方也都會(huì)用到這種方式處理選中與未選中。
除了設(shè)置 class 我們?cè)陧?xiàng)目中也經(jīng)常設(shè)置元素的內(nèi)聯(lián)樣式 style,在 jquery 時(shí)代我們大多數(shù)都是利用 addClass 與 removeClass 結(jié)合使用來(lái)處理 class 的添加/刪除,利用 css() 方法設(shè)置/獲取元素的內(nèi)聯(lián)樣式。
那么在 vue 中 我們?nèi)绾翁幚磉@類(lèi)的效果呢?在 vue 中我們可以利用 v-bind 指令綁定我們的 class 與 style,接下來(lái)我們看看 vue 中給我們提供了哪些綁定它們的方式。
對(duì)象語(yǔ)法綁定 Class
Tab 頁(yè)的切換是我們最常見(jiàn)的一個(gè)效果之一,如何讓選中的標(biāo)題高亮,常用的方式就是動(dòng)態(tài)切換 class 。
<div id="app"> <div class="button-group"> <button v-for="(tab, index) in tabs" v-bind:key="index" v-bind:class="{active: currentTab === tab}" v-on:click="currentTab = tab" >{{tab}}</button> </div> <component v-bind:is="currentTabComponent"></component> </div> <script> Vue.component("tab1", { "template": "<p>這里是標(biāo)簽頁(yè)1</p>" }); Vue.component("tab2", { "template": "<p>這里是標(biāo)簽頁(yè)2</p>" }); Vue.component("tab3", { "template": "<p>這里是標(biāo)簽頁(yè)3</p>" }); var vm = new Vue({ el: "#app", data: { currentTab: "tab1", tabs: ["tab1", "tab2", "tab3"] }, computed: { currentTabComponent() { return this.currentTab; } } }); </script>
從例子中我們看到 active 這個(gè) class 是否存在取決于后面的表達(dá)式是真值或者假值,當(dāng)為真值時(shí) active 類(lèi)被添加到元素上否則沒(méi)有。
我們不僅可以添加一個(gè) class,我們還可以同時(shí)添加多個(gè) class,并且還可以與原有的 class 共存。
<button class="btn" v-bind:class="{'btn-primary': isPrimary, active: isActive}" ></button> <script> var vm = new Vue({ el: "#app", data: { isPrimary: true, isActive: true } }); </script>
渲染結(jié)果為:
<button class="btn btn-primary active"></button>
我們也可以直接綁定一個(gè)數(shù)據(jù)對(duì)象
<button class="btn" v-bind:class="activePrimary"></button> <script> var vm = new Vue({ el: "#app", data: { activePrimary: { 'btn-primary': true, active: true } } }); </script>
渲染結(jié)果與上面相同
<button class="btn btn-primary active"></button>
除此之外,我們還可以使用計(jì)算屬性去綁定元素的 class
<button v-bind:class="activeClass"></button> <script> var vm = new Vue({ el: "#app", data: { isActive: true }, computed: { activeClass() { return { active: this.isActive } } } }); </script>
數(shù)組語(yǔ)法綁定 Class
Vue 中還支持我們給元素利用數(shù)組的方式添加 class,我們修改上面對(duì)象添加 class 的例子。
<button class="btn" v-bind:class="[primary, active]"></button> <script> var vm = new Vue({ el: "#app", data: { primary: 'btn-primary', active: 'btn-active' } }); </script>
上面方式我們綁定了固定不變的,如果我們需要?jiǎng)討B(tài)的切換 class 怎么辦呢? 我們可以利用三元表達(dá)式或者在數(shù)組中使用對(duì)象語(yǔ)法。
//三元表達(dá)式 <button v-bind:class="[isActive ? active : '', primary]"></button> <script> var vm = new Vue({ el: "#app", data: { isActive: true, primary: 'btn-primary', active: 'btn-active' } }); </script> //數(shù)組中使用對(duì)象語(yǔ)法 <button v-bind:class="[{active: isActive}, primary]"></button> <script> var vm = new Vue({ el: "#app", data: { isActive: true, primary: 'btn-primary' } }); </script>
對(duì)象語(yǔ)法綁定 Style
綁定內(nèi)聯(lián)樣式時(shí)的對(duì)象語(yǔ)法,看起來(lái)非常像 css,但他其實(shí)是一個(gè) Javascript 對(duì)象,我們可以使用駝峰式或者短橫線分隔命名。
<div v-bind:style="{color: colorStyle, backgroundColor: background}"> 對(duì)象語(yǔ)法 </div> <script> var vm = new Vue({ el: "#app", data: { colorStyle: 'red', background: 'blue' } }); </script>
與 class 類(lèi)似我們也可以使用數(shù)據(jù)對(duì)象的方式綁定。
<div v-bind:style="style">對(duì)象語(yǔ)法</div> <script> var vm = new Vue({ el: "#app", data: { style: { color: 'red', backgroundColor: 'blue' } } }); </script>
數(shù)組語(yǔ)法綁定 Style
Vue 允許我們同時(shí)綁定多個(gè)樣式對(duì)象作用于同一個(gè)對(duì)象上。
<div v-bind:style="[style, fontStyle]">對(duì)象語(yǔ)法</div> <script> var vm = new Vue({ el: "#app", data: { style: { color: 'red', backgroundColor: 'blue' }, fontStyle: { fontSize: '18px' } } }); </script>
到此這篇關(guān)于Vue中Class和Style實(shí)現(xiàn)v-bind綁定的幾種用法的文章就介紹到這了,更多相關(guān)Vue v-bind綁定用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Vue項(xiàng)目中,防止頁(yè)面被縮放和放大示例
今天小編就為大家分享一篇在Vue項(xiàng)目中,防止頁(yè)面被縮放和放大示例,具有很好的參考 價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10詳解解決Vue相同路由參數(shù)不同不會(huì)刷新的問(wèn)題
這篇文章主要介紹了詳解解決Vue相同路由參數(shù)不同不會(huì)刷新的問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Vue.js實(shí)戰(zhàn)之組件的進(jìn)階
組件(Component)是 Vue.js 最強(qiáng)大的功能之一,之前的文章都只是用到了基本的封裝功能,這次將介紹一些更強(qiáng)大的擴(kuò)展。這篇文章主要介紹了Vue.js實(shí)戰(zhàn)之組件進(jìn)階的相關(guān)資料,需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2017-04-04在vue項(xiàng)目中使用sass語(yǔ)法問(wèn)題
sass是一個(gè)最初由Hampton Catlin設(shè)計(jì)并由Natalie Weizenbaum開(kāi)發(fā)的層疊樣式表語(yǔ)言。這篇文章主要介紹了在vue項(xiàng)目中使用sass語(yǔ)法,需要的朋友可以參考下2019-07-07vuex如何在非組件中調(diào)用mutations方法
這篇文章主要介紹了vuex如何在非組件中調(diào)用mutations方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Vue標(biāo)簽屬性動(dòng)態(tài)傳參并拼接字符串的操作方法
這篇文章主要介紹了Vue標(biāo)簽屬性動(dòng)態(tài)傳參并拼接字符串的操作方法,我們需要根據(jù)傳入值的類(lèi)型,在placeholder屬性賦值"請(qǐng)輸入長(zhǎng)度",“請(qǐng)輸入寬度”,"請(qǐng)輸入厚度"等提示字符,本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11