一.綁定Class屬性。
綁定數(shù)據(jù)用v-bind:命令,簡寫成:
語法:<div v-bind:class="{ active: isActive }"></div>。class后面的雙引號里接受一個對象字面量/對象引用/數(shù)組作為參數(shù),
這里,{active: isActive}是對象參數(shù),active是class名,isActive是一個布爾值。下面是一個例子:
綁定對象字面量
html:
<div id="classBind"> <span :class="{warning:isWarning,safe:isSafe}" v-on:click="toggle"> 狀態(tài):{{alert}}{{isSafe}} </span></div>
//jsvar app11=new Vue({ el:'#classBind', data:{ isWarning:true, alertList:['紅色警報','警報解除'], alert:'' }, computed:{ isSafe:function(){ return !this.isWarning; } }, methods:{ toggle:function(){ this.isWarning=!this.isWarning; this.alert= this.isWarning?this.alertList[0]:this.alertList[1]; } }});
css:
.warning{ color:#f24;}.safe{ color:#42b983;}
當(dāng)點擊狀態(tài)文字時,可以切換后面的文字和顏色
//狀態(tài):警報解除true
//狀態(tài):紅色警報false
綁定對象引用
這里綁定的對象可以寫到Vue實例的data里面,而在class="classObj ",雙引號中的class是對Vue實例中classObj對象的引用。classObj可以放在data中或者computed中,如果在computed中,則classObj所對應(yīng)的函數(shù)必須返回一個對象如下:
js:
var app11=new Vue({ el:'#classBind', data:{ isWarning:true, alertList:['紅色警報','警報解除'], alert:'' }, computed: { isSafe: function () { return !this.isWarning; }, classObj:function(){ return { warning: this.isWarning, safe:this.isSafe } } }, methods:{ toggle:function(){ this.isWarning=!this.isWarning; this.alert= this.isWarning?this.alertList[0]:this.alertList[1]; } }});
綁定數(shù)組
html:
<div v-bind:class="classArray" @click="removeClass()">去掉class</div>
js
data: {classArray:["big",'red']}methods:{removeClass:function(){ this.classArray.pop();}}
css:
.big{ font-size:2rem;}.red{ color:red; }
效果,點擊去掉class,會調(diào)用removeClass函數(shù),去掉classArray數(shù)組的最后一項,第一次,去掉'red',字體顏色由紅變黑,再點,去掉'big',字體變小。
二、綁定內(nèi)聯(lián)style
此時此刻,我一邊看著本頁旁邊的那個Vue api文檔學(xué),一邊到這里賣,裝逼的感覺真爽o(^ 主站蜘蛛池模板: 兰州市| 荣成市| 新兴县| 莱西市| 普宁市| 义马市| 金昌市| 松阳县| 定结县| 娄底市| 繁昌县| 尤溪县| 察哈| 金溪县| 玉树县| 肥西县| 沭阳县| 阜宁县| 屏东市| 皋兰县| 株洲县| 石泉县| 万源市| 丹江口市| 华容县| 威宁| 射阳县| 平顶山市| 肥西县| 邓州市| 兴隆县| 玛多县| 龙口市| 马龙县| 文登市| 上饶县| 广饶县| 平遥县| 兰西县| 专栏| 东城区|