用AS2寫的一個動態載入CSS樣式的類

來源:互聯網
上載者:User
以前看了ultrashock的一篇文章,所以動手寫了個類,感覺用起來方便多了

/****************************************** CssStyle* 引用外部樣式表到組件* eg:* var cssStyle:CssStyle = new CssStyle("style.css");* 將樣式應用到_root.myCombox這個對象* cssStyle.applyTo(_root.myCombox,"combox");* 將樣式應用到所有的ComboBox對象* cssStyle.applyTo(_global.styles.ComboBox,"combox");* 將樣式應用所有的組件* cssStyle.applyTo((_global.style,"combox");*******************************/class CssStyle{ // 樣式表的路徑 private var cssPath:String; private var isLoaded:Boolean = false; private var cssResult; public function CssStyle(_cssPath:String) { this.cssPath = _cssPath; this.inital(); } // applyTo方法 // _component: 要應用樣式的組件或mc對象 // _style:應用的樣式名稱 public function applyTo(_component:Object, _style:String):Void { var component:Object = _component; var style:String = _style; var self:Object = this; if (self.cssResult == undefined) { var cssData:LoadVars = new LoadVars(); cssData.load(this.cssPath); cssData.onData = function(raw) { self.cssResult = new TextField.StyleSheet(); self.cssResult.parseCSS(raw); self.applyCSS(component, self.cssResult._css[style]); }; } else { self.applyCSS(component, self.cssResult._css[style]); } } // 把樣式表內容應用到指定的組件 private function applyToComponent(targetObj:Object, styleObj:Object) { for (var i in styleObj) { if (styleObj[i].substr(0, 1) == "#") { styleObj[i] = "0x" + styleObj[i].substr(1); } targetObj.setStyle(i, styleObj[i]); } } // 把樣式表組件和對象傳遞到應用函數 private function applyCSS(comp, cssObj) { applyToComponent(comp, cssObj); } // 初始化 private function inital():Void { var self:Object = this; var cssData:LoadVars = new LoadVars(); cssData.load(this.cssPath); cssData.onData = function(raw) { self.cssResult = new TextField.StyleSheet(); self.cssResult.parseCSS(raw); }; }}
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.