用AS2写的一个动态载入CSS样式的类
以前看了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); }; } }
posted on 2004-04-29 08:50 HelloSnoopy 阅读(1045) 评论(0) 编辑 收藏 举报