Vue.js中class與style的增強(qiáng)綁定實(shí)現(xiàn)方法
在web前端應(yīng)用中,操作元素的class列表的內(nèi)聯(lián)樣式style是數(shù)據(jù)綁定style是數(shù)據(jù)綁定的一個(gè)常見(jiàn)需求,因?yàn)樗鼈兌际?strong>attribute,所有可以用v-bind處理它們,但若樣式復(fù)雜,則需要書寫長(zhǎng)串的樣式代碼,這樣一來(lái),字符串拼接就比較麻煩。因此,在將v-bind用于class和style時(shí),Vue.js做了專門的增強(qiáng),表達(dá)式結(jié)果的類型除了字符串之外,還可以是對(duì)象或數(shù)組。
一、v-bind綁定class屬性
若想使用類樣式(即以類名定義元素的樣式,類樣式一般以"."號(hào)開(kāi)頭命令),可以通過(guò)v-bind指令綁定class屬性實(shí)現(xiàn):
(1)綁定class樣式,字符串寫法
適用于:樣式的類名不確定,需要?jiǎng)討B(tài)指定
<div id="root"> <!-- 綁定class樣式,字符串寫法,適用于:樣式的類名不確定,需要?jiǎng)討B(tài)指定 --> <div class="basic" v-bind:class="moon" @click="doChange"> {{name}} </div> </div> <script> const vm = new Vue({ el: '#root', data: { name: "class和style增強(qiáng)綁定", moon: "normal" }, methods: { doChange() { var arr = ["happy", "sad", "normal"]; indexof = Math.floor(Math.random() * 3); this.moon = arr[indexof]; } } }); </script>
css樣式:
.happy { border: 4px solid red; background-color: rgba(255, 255, 0, 0.644); background: linear-gradient(30deg, yellow, pink, orange); } .sad { border: 4px dashed rgb(2, 197, 2); background-color: gray; } .normal { background-color: skyblue; }
執(zhí)行結(jié)果:
(2)綁定class樣式,數(shù)組寫法
適用于:要綁定的樣式個(gè)數(shù)不確定,名字也不確定
<div id="root"> <!-- 綁定class樣式,數(shù)組寫法,適用于:要綁定的樣式個(gè)數(shù)不確定,名字也不確定 --> <div class="basic" v-bind:class="classarr"> {{name}} </div> </div> <script> const vm = new Vue({ el: '#root', data: { name: "class和style增強(qiáng)綁定", classarr: ["text_1", "text_2", "text_3"], }, methods: { } }); </script>
css樣式:
.text_1 { background-color: yellowgreen; } .text_2 { font-size: 30px; text-shadow: 2px 2px 10px red; } .text_3 { border-radius: 20px; }
執(zhí)行結(jié)果:
(3)綁定class樣式,對(duì)象寫法
適用于:要綁定的樣式個(gè)數(shù)和名字也確定,需要?jiǎng)討B(tài)顯示
<div id="root"> <!-- 綁定class樣式,對(duì)象寫法,適用于:要綁定的樣式個(gè)數(shù)和名字也確定,需要?jiǎng)討B(tài)顯示 --> <div class="basic" v-bind:class="classobj"> {{name}} </div> </div> <script> const vm = new Vue({ el: '#root', data: { name: "class和style增強(qiáng)綁定", classobj: { text_1: false, text_2: true, text_3: false, }, }, methods: { } }); </script>
css樣式:
.text_1 { background-color: yellowgreen; } .text_2 { font-size: 30px; text-shadow: 2px 2px 10px red; } .text_3 { border-radius: 20px; }
執(zhí)行結(jié)果:
二、v-bind綁定內(nèi)聯(lián)樣式style
通過(guò)內(nèi)聯(lián)(style)綁定給DOM元素示例:
(1)綁定style樣式---對(duì)象形式
<div id="root"> 綁定style樣式----對(duì)象形式 <br><br> <div v-bind:style="styleobj" class="basic"> {{name}} </div><br><br> </div> <script> const vm = new Vue({ el: '#root', data: { name: "class和style增強(qiáng)綁定", styleobj: { width: "300px", height: "100px", border: "1px solid black", fontSize: "40px", backgroundColor: "blue" }, }, methods: { } }); </script>
執(zhí)行結(jié)果:
(2)綁定style樣式---數(shù)組寫法
<div id="root"> 綁定style樣式----數(shù)組寫法 <br><br> <div v-bind:style="stylearr" class="basic"> {{name}} </div> </div> <script> const vm = new Vue({ el: '#root', data: { name: "class和style增強(qiáng)綁定", stylearr: [ {width: "300px"}, {height: "100px"}, {border: "1px solid black"}, {backgroundColor:"red"}, {fontSize:"20px"} ], }, methods: { } }); </script>
執(zhí)行結(jié)果:
到此這篇關(guān)于Vue.js中class與style的增強(qiáng)綁定的文章就介紹到這了,更多相關(guān)Vue.js class與style綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
iview tabs 頂部導(dǎo)航欄和模塊切換欄的示例代碼
這篇文章主要介紹了iview tabs 頂部導(dǎo)航欄和模塊切換欄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03vue3.0報(bào)錯(cuò)Cannot?find?module‘worker_threads‘的解決辦法
這篇文章介紹了vue3.0報(bào)錯(cuò)Cannot?find?module‘worker_threads‘的解決辦法。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11詳解vue-cli+element-ui樹(shù)形表格(多級(jí)表格折騰小計(jì))
這篇文章主要介紹了vue-cli + element-ui 樹(shù)形表格(多級(jí)表格折騰小計(jì)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04