一.綁定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(^ 主站蜘蛛池模板: 六枝特区| 张家界市| 宝山区| 招远市| 宜昌市| 郁南县| 德惠市| 扶风县| 呼和浩特市| 荔波县| 雅安市| 达拉特旗| 裕民县| 新巴尔虎右旗| 汽车| 西藏| 清水县| 阜康市| 满城县| 自治县| 平凉市| 揭东县| 绩溪县| 五家渠市| 富裕县| 珠海市| 旬阳县| 东源县| 五家渠市| 霍邱县| 屏山县| 芦山县| 萍乡市| 邯郸市| 安泽县| 琼中| 宿松县| 定西市| 泰宁县| 石屏县| 阿克陶县|