as3自定义滚动条制作方法:
package
{
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.geom.*;
/**
* ...滚动条组件
* @author qxq
*/
public class Scrollbar extends Sprite
{
[Embed(source="../../../../../assets/images/skins/bar_darg.png")]
private const RulerAsset:Class;
[Embed(source="../../../../../assets/images/skins/sliderTrack.png")]
private const SliderTrack:Class;
static public const MARGIN_LEFT:uint = 3;
private var _1124:Number;
public var target:Sprite;
public var background:ScaleBitmap;
//public var _filter:BlurFilter;
private var _11210:Number;
private var _1133:Number;
private var rect:Rectangle;
public var maskmc:Sprite;
//public var area:Sprite;
public var ruler:Sprite;
public var _1137:uint;
public function Scrollbar(target:Sprite, rect:Rectangle)
{
this.target = target;
this.rect = rect;
addEventListener(Event.ADDED_TO_STAGE, onAddtoStage);
return;
} // end function
public function onAddtoStage(param1:Event = null):void
{
_init();
//_filter = new BlurFilter(0, 0, 1);
//this.target.filters = new Array(_filter);
this.target.mask = this.maskmc;
//this.target.cacheAsBitmap = true;
this._11210 = background.y;
this._1133 = background.y + background.height - ruler.height;
this.ruler.buttonMode = true;
this._1124 = target.y;
//this._11210 = 0;
this._1133 = this.background.height - this.ruler.height;
ruler.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
target.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
this.addEventListener(Event.ENTER_FRAME, onEnterframe);
return;
} // end function
private function onMouseUp(param1:MouseEvent):void
{
ruler.stopDrag();
return;
} // end function
public function reflush():void
{
var _loc_1:Number;
var _loc_2:Number;
var _loc_3:Number;
var _loc_4:Number;
var _loc_5:Number;
var _loc_6:Number;
var _loc_7:Number;
var _loc_8:Number;
_1137 = 100 / _1133 * ruler.y;
_loc_1 = 0;
_loc_2 = target.height - maskmc.height / 2 + 20;
ruler.visible = target.height >= maskmc.height;
_loc_4 = _1124 - (_loc_2 - maskmc.height / 2) / 100 * _1137;
_loc_5 = target.y;
_loc_6 = _loc_4;
if (_loc_5 != _loc_6)
{
_loc_7 = _loc_6 - _loc_5;
_loc_5 = _loc_5 + _loc_7 / 4;
//_loc_8 = Math.abs(_loc_7) / 8;
//_filter.blurY = _loc_8 / 2;
//target.filters = new Array(_filter);
}
target.y = _loc_5;
return;
} // end function
private function onEnterframe(param1:Event):void
{
reflush();
}
private function _112f(param1:int):void
{
var _loc_2:Number;
var _loc_3:Number;
_loc_2 = (-param1) * 10;
if (_loc_2 > 0)
{
_loc_3 = Math.min(_1133, ruler.y + _loc_2);
}
if (_loc_2 < 0)
{
_loc_3 = Math.max(_11210, ruler.y + _loc_2);
}
ruler.y = _loc_3;
reflush();
}
private function onMouseWheel(param1:MouseEvent):void
{
if (this.target.hitTestPoint(stage.mouseX, stage.mouseY, false))
{
_112f(param1.delta);
}
return;
} // end function
public function _init():void
{
target.y = 0;
// 1>
const bmp:Bitmap = new SliderTrack;
background = new ScaleBitmap(bmp.bitmapData);
background.height = rect.height;
background.x = rect.width + MARGIN_LEFT;
background.scale9Grid = new Rectangle(4, 6, 2, 277);
addChild(background);
// 2>
ruler = new Sprite();
ruler.addChild(new RulerAsset);
addChild(ruler);
ruler.y = 0;
ruler.x = rect.width + MARGIN_LEFT;
// 3> Make mask mc;
maskmc = new Sprite;
maskmc.graphics.beginFill(0x000000);
maskmc.graphics.drawRect(0, 0, rect.width, rect.height);
maskmc.graphics.endFill();
addChild(maskmc);
return;
} // end function
private function onMouseDown(param1:MouseEvent):void
{
ruler.startDrag(false, new Rectangle(ruler.x, _11210, 0, _1133));
trace(new Rectangle(ruler.x + ruler.width / 2, _11210, 0, _1133));
} // end function
}
}