## WeX5數據綁定05:css綁定
css綁定(bind-css)為元素添加或刪除class,從而改變元素的樣式。css綁定非常有用,可以根據數據動態改變元素的樣式,比如負值用紅色來顯示。另外還有一種更直接的改變樣式的方法,即通過style綁定。
### 靜態class的css綁定
示例代碼:
//.w片段
//< 是<的轉義符</pre>
<div bind-css="{ profitWarning: currentProfit.get() < 0 }">
Profit Information
</div>
//.js片段
//初始正值,"profitWarning" class不會被應用到div上
this.currentProfit = justep.Bind.observable(15000);
//設置負值,"profitWarning" class將會被應用到div上
this.currentProfit.set(-50);
上面這段代碼,根據currentProfit的值會動態改變樣式。如果currentProfit<0,就會在div添加樣式class “profitWarning”,否則就會移除該class。
綁定規則:
靜態class的css綁定通常都是需要判斷完成的,如果表達式的返回值為true,則添加該class,如果為false,則移除該class。
### 動態class的css綁定
如果css綁定的值是字符串,則css綁定將會直接把該字符串值作為樣式設置到元素class上。
//.w片段
<div bind-css="profitStatus">Profit Information</div>
//.js片段
this.currentProfit = justep.Bind.observable(15000);
this.currentProfit.set(-50);
this.profitStatus = justep.Bind.computed(function() {
return this.currentProfit.get() < 0 ? "profitWarning" : "profitPositive";
}, this);
上面例子profitStatus屬性返回的字符串”profitWarning” 或 “profitPositive”會直接作為class設置到元素上,這就是動態class的css綁定。這個特性非常靈活強大,在某些需要需求場景下非常有用。
### 綁定多個class
css綁定可以同時綁定多個class,每個class之間用半角逗號隔開,示例代碼如下:
//.w片段
<div bind-css="{ profitWarning: currentProfit.get() < 0, majorHighlight: isSevere }">
### 綁定不符合標識符規則的class
//.w片段
<div bind-css="{ my-class: someValue }">...</div>
my-class不符合javascript標識符規則,導致綁定出錯。在這種情況下,可以在class用單引號引起即可,代碼如下:
//.w片段
<div bind-css="{ 'my-class': someValue }">...</div>
### 在固定的class之后追加綁定class
//.w片段
<a component="$UI/system/components/justep/button/button"
class="btn" bind-css="{ 'btn-danger': currentProfit.get() < 0 }"
label="button" xid="button1">
<i/>
<span/>
</a>
上面示例代碼標簽有固定的class ‘btn’,通過bind-css動態追加和移除’btn-danger’ class.
- 快速入門
- 第一個應用
- WeX5產品能力和技術
- wex5技術理念
- WeX5可以怎么玩?
- WeX5和BeX5比較
- UI2開發
- UI2前端框架基礎01:應用和頁面
- UI2框架基礎02:框架結構圖和目錄
- 組建基礎
- 編程基礎
- js引用
- css、text引用
- 設置資源依賴
- 代碼調試
- 數據組件
- Data組件基礎01:列、初始化加載狀態、行對象和游標
- Data組件基礎02:規則、數據遍歷查找
- Data組件基礎03:CRUD
- Data組件基礎04:Tree、主從數據、更新模式
- Data組件基礎05:再談Data組件新增,查詢,保存
- Data組件的JSON數據格式
- WeX5 & BeX5 頁面框架核心之數據綁定
- 數據綁定屬性系列
- 初識綁定
- visible綁定
- text綁定
- html綁定
- css綁定
- 頁面布局
- 頁面樣式
- 樣式基礎
- 添加自定義圖標(iconfont)
- 常用組件
- bar組件
- contents組件
- 前端路由和頁面跳轉
- 路由模塊
- 頁面跳轉
- 部署和發布
- 三種部署方式
- Web app部署
- UIServer的緩存機制
- 自定義組件開發
- 組件運行時開發案例
- 組件設計時開發案例
- 組件設計時開發參考
- 屬性編輯器配置和開發
- 自定義向導開發(waiting)
- 第三方庫集成
- 集成Echarts
- 集成百度和高德地圖
- App開發
- 打包
- App打包基礎和常見問題
- App打包原理和目錄結構
- App打包過程詳解
- App打包服務器環境搭建
- 蘋果證書申請 使用
- Android和IOS的本地應用圖標規范
- Android和IOS的本地App如何安裝(apk&ipa)
- 蘋果App部署HTTPS進行在線下載安裝
- 調試
- Android和IOS真機調試
- 插件
- 如何使用和擴展cordova插件
- cordova插件開發
- 常用cordovar插件
- SQLite插件
- 極光推送(JPush)插件
- 微信支付入門教程
- 微信、支付寶支付開發
- 服務端開發
- App與服務端交互原理
- 輕量級Baas(視頻)(文字) (.net版)
- Data組件的JSON數據格式11
- 微信服務號集成(視頻)
- 擴展學習資料
- bootstrap
- Knockoutjs
- JQuery
- requirejs
- phonegap/cordova