写了个滚动条的类ZScrollBar.as
package zkl.controls{import com.greensock.TweenLite;import com.greensock.easing.Back;import flash.display.DisplayObject;import flash.display.DisplayObjectContainer;import flash.display.Sprite;import flash.display.Stage;import flash.events.Event;import flash.events.MouseEvent;import flash.geom.Rectangle;/** * 滚动条 * 约定滚动的控制的焦点都在左上角 * 滑块:可以被拖动的 * 滑道:滑块所在的拖动范围 * 内容:拖动滑块要触动的对象 * 使用实例: * import zkl.controls.ZScrollBar; * import zkl.controls.ZScrollBarDirection; * var bar:ZScrollBar = new ZScrollBar(); * bar._stage = this.stage; * bar._target = _target; * bar._slideBar = slideBarV; * bar._scrollBar = scrollBar; * bar._targetScrollLength = _target.height - mask.height; * bar.init(); * @author zkl */public class ZScrollBar extends Sprite {//所在舞台public var _stage:Stage;//被滚动的内容public var _target:DisplayObject;//内容滚动的距离public var _targetScrollLength:Number;//滚动条的滚动方向public var _direction:String = ZScrollBarDirection.VERTICAL;//滑块public var _scrollBar:Sprite;//滑道public var _slideBar:Sprite;//四个方向的滑块public var _upBar:Sprite;public var _downBar:Sprite;public var _leftBar:Sprite;public var _rightBar:Sprite;//滑块滚动的速度(只有设置的方向按钮,此值才有作用)public var _barSpeed:Number = 5;//滑块可以滑动的长度(有效的滑道)private var slideLength:Number;//滑块的滚动范围private var barDragRect:Rectangle;//对象的初始化坐标private var targetXY:Number = 0;//对象滚动的方向private var scrollDir:int;private static const UP:int = 0;private static const DOWN:int = 1;private static const LEFT:int = 2;private static const RIGHT:int = 3;public function ZScrollBar() {}/** * 初始化 */public function init():void {if (_stage == null)_stage = _target.stage;if (_direction == ZScrollBarDirection.VERTICAL) {//垂直//滑块的初始位置_scrollBar.x = _slideBar.x + (_slideBar.width - _scrollBar.width) / 2;_scrollBar.y = _slideBar.y;//有效的滑道长度slideLength = _slideBar.height - _scrollBar.height;//滑块的可拖动范围barDragRect = new Rectangle(_slideBar.x + (_slideBar.width - _scrollBar.width) / 2, _slideBar.y, 0, slideLength);//targetXY = _target.y;}else if (_direction == ZScrollBarDirection.HORIZONTAL) {//水平//滑块的初始位置_scrollBar.x = _slideBar.x;_scrollBar.y = _slideBar.y + (_slideBar.height - _scrollBar.height) / 2;//有效的滑道长度slideLength = _slideBar.width - _scrollBar.width;//滑块的可拖动范围barDragRect = new Rectangle(_slideBar.x, _slideBar.y + (_slideBar.height - _scrollBar.height) / 2, slideLength, 0);targetXY = _target.x;}else {throw new Error("direction的值设置有误,请参考ZScrollBarDirection.as");return;}setListener();}//设置监听private var tempBar:Sprite;private function setListener():void {//scrollBar_scrollBar.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);//dir barif (_upBar != null) {_upBar.addEventListener(MouseEvent.MOUSE_DOWN, dirDownHandler);}if (_downBar != null) {_downBar.addEventListener(MouseEvent.MOUSE_DOWN, dirDownHandler);}if (_leftBar != null) {_leftBar.addEventListener(MouseEvent.MOUSE_DOWN, dirDownHandler);}if (_rightBar != null) {_rightBar.addEventListener(MouseEvent.MOUSE_DOWN, dirDownHandler);}}//dir barprivate function dirDownHandler(e:MouseEvent):void {tempBar = e.currentTarget as Sprite;switch(tempBar) {case _upBar:scrollDir = UP;break;case _downBar:scrollDir = DOWN;break;case _leftBar:scrollDir = LEFT;break;case _rightBar:scrollDir = RIGHT;break;}tempBar.addEventListener(Event.ENTER_FRAME, dirEnterFrame);tempBar.addEventListener(MouseEvent.MOUSE_UP, dirUpHandler);tempBar.addEventListener(MouseEvent.MOUSE_OUT, dirUpHandler);}private function dirUpHandler(e:MouseEvent):void {tempBar.removeEventListener(Event.ENTER_FRAME, dirEnterFrame);tempBar.removeEventListener(MouseEvent.MOUSE_UP, dirUpHandler);tempBar.removeEventListener(MouseEvent.MOUSE_OUT, dirUpHandler);}private function dirEnterFrame(e:Event):void {switch(scrollDir) {case UP:_scrollBar.y = Math.max(_slideBar.y, _scrollBar.y - _barSpeed);break;case DOWN:_scrollBar.y = Math.min(_slideBar.y + slideLength, _scrollBar.y + _barSpeed);break;case LEFT:_scrollBar.x = Math.max(_slideBar.x, _scrollBar.x - _barSpeed);break;case RIGHT:_scrollBar.x = Math.min(_slideBar.x + slideLength, _scrollBar.x + _barSpeed);break;}updateTargetPos();}//dir bar end//scrollBarprivate function mouseDownHandler(e:MouseEvent):void {_stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);_scrollBar.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);}private function mouseMoveHandler(e:MouseEvent):void {_scrollBar.startDrag(false, barDragRect);_scrollBar.addEventListener(Event.ENTER_FRAME, scrollEnterFrameHandler);}private function mouseUpHandler(e:MouseEvent):void {_scrollBar.stopDrag();_scrollBar.removeEventListener(Event.ENTER_FRAME, scrollEnterFrameHandler);_scrollBar.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);_stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);}private function scrollEnterFrameHandler(e:Event):void {updateTargetPos();}//scrollBar end//updateprivate var percent:Number = 0;private function updateTargetPos():void {if (_direction == ZScrollBarDirection.VERTICAL) {percent = (_scrollBar.y - _slideBar.y) / slideLength;//_target.y = Math.round(targetXY - _targetScrollLength * percent);TweenLite.to(_target, 0.5, { y:Math.round(targetXY - _targetScrollLength * percent), ease:Back.easeOut } );}else if (_direction == ZScrollBarDirection.HORIZONTAL) {percent = (_scrollBar.x - _slideBar.x) / slideLength;//_target.x = Math.round(targetXY - _targetScrollLength * percent);TweenLite.to(_target, 0.5, { x:Math.round(targetXY - _targetScrollLength * percent), ease:Back.easeOut } );}}///////////////////////////////////////////////////////////}}