陈勇写的滚动条类及使用举例
2010-08-03 17:48
225 查看
function setContent(txt:String):void { _text.htmlText=txt;//传入文字内容 var isShow:Boolean=_text.textHeight>maskMC.height?true:false;//根据文本内容高度是否超过遮罩高度,判断是否需要显示滚动条 scrollBar=new ScrollBar(isShow,maskMC.height); scrollBar.x=maskMC.x+maskMC.width-scrollBar.width; scrollBar.y=maskMC.y; scrollBar.setHeight(maskMC.height); scrollBar.addEventListener(Event.CHANGE,onScrolling,false,0,true); addChild(scrollBar); } function onScrolling(e:Event):void { _text.y = maskMC.y-(_text.height-maskMc.height)*scrollBar.txt; }
package leo.controls { import flash.display.DisplayObject; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Rectangle; import gs.*; import com.pixelbreaker.ui.osx.MacMouseWheel; public class ScrollBar extends Sprite { private var _thumb:Sprite; private var _height:Number; private var _thumbColor:int=0xBEBEBE;// private var _thumbBgColor:int=0xDEDEDE;// private var _tweenSpeed:Number=0.5; private var _rect:Rectangle; private var _showed:Boolean; private var _scrollTo:Number=0; private var _wheelScrollSpeed:Number=2; public var wheelFocusTarget:DisplayObject; //背景底 private var _thumbBg:Sprite; public function ScrollBar(showed : Boolean,bgheight:Number) { //画一个背景底 _thumbBg=new Sprite(); _thumbBg.graphics.beginFill(_thumbBgColor); _thumbBg.graphics.drawRect(0, 0, 5, 10); _thumbBg.graphics.endFill(); _thumbBg.width=5; _thumbBg.height=bgheight; addChild(_thumbBg); // 画一个控制滚动条的bar _thumb = new Sprite(); _thumb.graphics.beginFill(_thumbColor); _thumb.graphics.drawRect(0, 0, 5, 10); _thumb.graphics.endFill(); _thumb.buttonMode=true; _thumb.height=10; _thumb.width=5; _thumb.addEventListener(MouseEvent.MOUSE_DOWN, _thumbDownHandler, false, 0, true); _thumb.addEventListener(MouseEvent.MOUSE_OVER, _thumbOverHandler, false, 0, true); _thumb.addEventListener(MouseEvent.MOUSE_OUT, _thumbOutHandler, false, 0, true); addEventListener(Event.ADDED_TO_STAGE, _addedToStageHandler, false, 0, true); addEventListener(Event.REMOVED_FROM_STAGE, _removedFromStageHandler, false, 0, true); //传一个参数控制其是否显示 visible=showed; alpha=showed?1:0; addChild(_thumb); _showed=showed; } private function _addedToStageHandler(event : Event):void { //添加中健滑动功能 MacMouseWheel.setup(stage); wheelFocusTarget=wheelFocusTarget?wheelFocusTarget:stage; wheelFocusTarget.addEventListener(MouseEvent.MOUSE_WHEEL, _stageMouseWheelHandler, false, 0, true); } private function _removedFromStageHandler(event : Event):void { stage.removeEventListener(MouseEvent.MOUSE_UP, _stageUpHandler); stage.removeEventListener(MouseEvent.MOUSE_MOVE, _stageMoveHandler); if (wheelFocusTarget) { wheelFocusTarget.removeEventListener(MouseEvent.MOUSE_WHEEL, _stageMouseWheelHandler); } } private function _thumbDownHandler(event : MouseEvent):void { //当高度值设置不为零且已经实例化_rect if (_height&&_rect) { //开始拖动 _thumb.startDrag(false, _rect); stage.addEventListener(MouseEvent.MOUSE_UP, _stageUpHandler, false, 0, true); stage.addEventListener(MouseEvent.MOUSE_MOVE, _stageMoveHandler, false, 0, true); removeEventListener(Event.ENTER_FRAME, _enterFrameHandler); } } private function _thumbOverHandler(event : MouseEvent):void{ TweenMax.to(_thumb, 1, {colorMatrixFilter:{colorize:0xff0000, amount:1}}); } private function _thumbOutHandler(event : MouseEvent):void{ TweenMax.to(_thumb, 1, {colorMatrixFilter:{colorize:0x00ff00, amount:1}}); } private function _stageMouseWheelHandler(event : MouseEvent):void { var _y : Number = _thumb.y - (event.delta * 3); _y=_y<0?0:_y; _y=_y>_rect.height?_rect.height:_y; _scrollTo=_y; if (! hasEventListener(Event.ENTER_FRAME)) { addEventListener(Event.ENTER_FRAME, _enterFrameHandler, false, 0, true); } } private function _enterFrameHandler(event : Event):void { var before:Number=_thumb.y; _thumb.y += (_scrollTo - _thumb.y) / _wheelScrollSpeed; dispatchEvent(new Event(Event.CHANGE)); if (Math.abs(_thumb.y-before)<0.008) { removeEventListener(Event.ENTER_FRAME, _enterFrameHandler); } } private function _stageUpHandler(event : MouseEvent):void { _thumb.stopDrag(); stage.removeEventListener(MouseEvent.MOUSE_MOVE, _stageMoveHandler); stage.removeEventListener(MouseEvent.MOUSE_UP, _stageUpHandler); } private function _stageMoveHandler(event : MouseEvent):void { //如果滚动条状态为显示,开始移动的时候就发送改变事件 if (showed) { dispatchEvent(new Event(Event.CHANGE)); } } private function _hideCompleteListener(event : Event):void { visible=false; } //此为设置文本的高度,即滑动条滑动的纵坐标距离为:_rect.height public function setHeight(value : Number):void { //要滑动,高度值不能小于或者等于零 _height=value; _rect=new Rectangle(0,0,0,_height-_thumb.height); } public function show():void { visible=true; _showed=true; TweenLite.to(this,_tweenSpeed,{alpha:1}); } public function hide():void { TweenLite.to(this,_tweenSpeed,{alpha:0,onComplete:_hideCompleteListener}); _showed=false; } //滑块纵坐标与可滑动区域的高度比,范围在0-1之间 public function get value():Number { return _thumb.y / _rect.height; } public function set value(_value : Number):void { _thumb.y=_rect.height*_value; } public function get showed():Boolean { return _showed; } } }
相关文章推荐
- 系统自带滚动条类 UIScrollBar 使用举例
- 系统自带滚动条类 UIScrollBar 使用举例
- 7种形式的Android Dialog使用举例
- 使用cJSON创建JSON字符串,举例详解。
- object和embed使用及举例
- c语言:宏的使用举例
- cocos2dx 举例说明 convertToNodeSpace 与 convertToWorldSpace 的使用
- c语言:宏里面参数不加括号容易出错,在使用时尽量加括号及举例
- XML解析简介及Xerces-C++简单使用举例
- 举例讲解Python编程中对线程锁的使用
- Java反射——对类内部函数、私有变量和构造函数的使用举例
- 举例讲解Android应用开发中OTTO框架的基本使用
- 使用DOS命令如何操作Windows防火墙(命令介绍及应用举例) .
- PHP使用json举例
- python-re使用举例
- select函数的使用举例
- 学习淘淘商城第七十四课(学习nginx配置及举例使用端口、域名来区分不同的虚拟主机)
- oracle 存储过程创建和使用举例
- curl 使用举例详解(二)
- PowerShell V2 高级函数使用举例(01)