Vue.js中class與style的增強綁定實現(xiàn)方法
在web前端應(yīng)用中,操作元素的class列表的內(nèi)聯(lián)樣式style是數(shù)據(jù)綁定style是數(shù)據(jù)綁定的一個常見需求,因為它們都是attribute,所有可以用v-bind處理它們,但若樣式復(fù)雜,則需要書寫長串的樣式代碼,這樣一來,字符串拼接就比較麻煩。因此,在將v-bind用于class和style時,Vue.js做了專門的增強,表達式結(jié)果的類型除了字符串之外,還可以是對象或數(shù)組。
一、v-bind綁定class屬性
若想使用類樣式(即以類名定義元素的樣式,類樣式一般以"."號開頭命令),可以通過v-bind指令綁定class屬性實現(xiàn):
(1)綁定class樣式,字符串寫法
適用于:樣式的類名不確定,需要動態(tài)指定
<div id="root">
<!-- 綁定class樣式,字符串寫法,適用于:樣式的類名不確定,需要動態(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增強綁定",
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ù)組寫法
適用于:要綁定的樣式個數(shù)不確定,名字也不確定
<div id="root">
<!-- 綁定class樣式,數(shù)組寫法,適用于:要綁定的樣式個數(shù)不確定,名字也不確定 -->
<div class="basic" v-bind:class="classarr">
{{name}}
</div>
</div>
<script>
const vm = new Vue({
el: '#root',
data: {
name: "class和style增強綁定",
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樣式,對象寫法
適用于:要綁定的樣式個數(shù)和名字也確定,需要動態(tài)顯示
<div id="root">
<!-- 綁定class樣式,對象寫法,適用于:要綁定的樣式個數(shù)和名字也確定,需要動態(tài)顯示 -->
<div class="basic" v-bind:class="classobj">
{{name}}
</div>
</div>
<script>
const vm = new Vue({
el: '#root',
data: {
name: "class和style增強綁定",
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
通過內(nèi)聯(lián)(style)綁定給DOM元素示例:
(1)綁定style樣式---對象形式
<div id="root">
綁定style樣式----對象形式
<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增強綁定",
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增強綁定",
stylearr: [
{width: "300px"},
{height: "100px"},
{border: "1px solid black"},
{backgroundColor:"red"},
{fontSize:"20px"}
],
},
methods: {
}
});
</script>執(zhí)行結(jié)果:

到此這篇關(guān)于Vue.js中class與style的增強綁定的文章就介紹到這了,更多相關(guān)Vue.js class與style綁定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
iview tabs 頂部導(dǎo)航欄和模塊切換欄的示例代碼
這篇文章主要介紹了iview tabs 頂部導(dǎo)航欄和模塊切換欄的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
vue3.0報錯Cannot?find?module‘worker_threads‘的解決辦法
這篇文章介紹了vue3.0報錯Cannot?find?module‘worker_threads‘的解決辦法。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
詳解vue-cli+element-ui樹形表格(多級表格折騰小計)
這篇文章主要介紹了vue-cli + element-ui 樹形表格(多級表格折騰小計),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04

