项目中经常用到滚动条,adobe给的组件,样式改起来真不方便,而且体积也挺大,不如自己写,参考了下老外的博文,原文地址:
http://theflashconnection.com/content/flash-as3-scrollbar-class,老外写技术博还是蛮清楚的,就是对我来说可能废话多了点,今儿个难得有闲有心情整理下,为以后不时之需吧。
1.除startDrag以外的拖放操作,代码如下:
[code]
import flash.events.MouseEvent;

var xOffset:Number;
var yOffset:Number;

thumb.buttonMode = true;
thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumb_onMouseDown);
function thumb_onMouseDown(event:MouseEvent):void {
xOffset = mouseX - thumb.x;
yOffset = mouseY - thumb.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}

function stage_onMouseMove(event:MouseEvent):void {
thumb.x = mouseX - xOffset;
thumb.y = mouseY - yOffset;
event.updateAfterEvent();
}

function stage_onMouseUp(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}
[/code]

2.限制拖动范围
[code]
import flash.events.MouseEvent;

var yOffset:Number;
var topLimit:Number = track.y;
var thumbRange:Number = track.height - thumb.height;
var bottomLimit:Number = track.y + thumbRange;

thumb.buttonMode = true;
thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumb_onMouseDown);
function thumb_onMouseDown(event:MouseEvent):void {
yOffset = mouseY - thumb.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}

function stage_onMouseMove(event:MouseEvent):void {
thumb.y = mouseY - yOffset;
//restrict the movement of the thumb:
if(thumb.y < topLimit) { thumb.y = topLimit; } if(thumb.y > bottomLimit) {
thumb.y = bottomLimit;
}
event.updateAfterEvent();
}

function stage_onMouseUp(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}
[/code]

3.计算拖动百分比
[code]
import flash.events.MouseEvent;

var yOffset:Number;
var topLimit:Number = track.y;
var thumbRange:Number = track.height - thumb.height;
var bottomLimit:Number = track.y + thumbRange;
//new variable: scrollPercent!
var scrollPercent:Number = 0;
myText.text = "0";
circle.alpha = 0;

thumb.buttonMode = true;
thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumb_onMouseDown);
function thumb_onMouseDown(event:MouseEvent):void {
yOffset = mouseY - thumb.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}

function stage_onMouseMove(event:MouseEvent):void {
thumb.y = mouseY - yOffset;
//restrict the movement of the thumb:
if(thumb.y < topLimit) { thumb.y = topLimit; } if(thumb.y > bottomLimit) {
thumb.y = bottomLimit;
}
//calculate scrollPercent and make it do stuff:
scrollPercent = (thumb.y - track.y) / thumbRange;
myText.text = String(scrollPercent);
circle.alpha = scrollPercent;

event.updateAfterEvent();
}

function stage_onMouseUp(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}
[/code]
4.使content在window范围内随滚动条滚动
[code]
import flash.events.MouseEvent;

var yOffset:Number;
var topLimit:Number = track.y;
var thumbRange:Number = track.height - thumb.height;
var bottomLimit:Number = track.y + thumbRange;
//new variable: scrollPercent!
var scrollPercent:Number = 0;
var contentRange:Number = content.height - window.height;

content.mask = window;
thumb.buttonMode = true;
thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumb_onMouseDown);
function thumb_onMouseDown(event:MouseEvent):void {
yOffset = mouseY - thumb.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}

function stage_onMouseMove(event:MouseEvent):void {
thumb.y = mouseY - yOffset;
//restrict the movement of the thumb:
if(thumb.y < topLimit) { thumb.y = topLimit; } if(thumb.y > bottomLimit) {
thumb.y = bottomLimit;
}
//calculate scrollPercent and make it do stuff:
scrollPercent = (thumb.y - track.y) / thumbRange;
content.y = window.y - (scrollPercent * contentRange);
event.updateAfterEvent();
}

function stage_onMouseUp(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}
[/code]
5.增加上下按钮
[code]
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Timer;

var yOffset:Number;
var topLimit:Number = track.y;
var thumbRange:Number = track.height - thumb.height;
var bottomLimit:Number = track.y + thumbRange;
//new variable: scrollPercent!
var scrollPercent:Number = 0;
var contentRange:Number = content.height - window.height;
var speed:Number = 0.01;

content.mask = window;
thumb.buttonMode = true;
thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumb_onMouseDown);
function thumb_onMouseDown(event:MouseEvent):void {
yOffset = mouseY - thumb.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}

function stage_onMouseMove(event:MouseEvent):void {
thumb.y = mouseY - yOffset;
//restrict the movement of the thumb:
if(thumb.y < topLimit) { thumb.y = topLimit; } if(thumb.y > bottomLimit) {
thumb.y = bottomLimit;
}
//calculate scrollPercent and make it do stuff:
scrollPercent = (thumb.y - track.y) / thumbRange;
content.y = window.y - (scrollPercent * contentRange);
event.updateAfterEvent();
}

function stage_onMouseUp(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
}

down.buttonMode = true;
down.addEventListener(MouseEvent.MOUSE_DOWN, down_onMouseDown);
function down_onMouseDown(event:MouseEvent):void {
stage.addEventListener(Event.ENTER_FRAME, scrollDown);
stage.addEventListener(MouseEvent.MOUSE_UP, stopScrollingDown);
}
function scrollDown(event:Event):void {
scrollPercent += speed;
if(scrollPercent > 1) {
scrollPercent = 1;
}
content.y = window.y - (scrollPercent * contentRange);
thumb.y = scrollPercent * thumbRange + track.y;
}
function stopScrollingDown(event:MouseEvent):void {
stage.removeEventListener(Event.ENTER_FRAME, scrollDown);
stage.removeEventListener(MouseEvent.MOUSE_UP, stopScrollingDown);
}

up.buttonMode = true;
up.addEventListener(MouseEvent.MOUSE_DOWN, up_onMouseDown);
function up_onMouseDown(event:MouseEvent):void {
stage.addEventListener(Event.ENTER_FRAME, scrollUp)
stage.addEventListener(MouseEvent.MOUSE_UP, stopScrollingUp);
}
function scrollUp(event:Event):void {
scrollPercent -= speed;
if(scrollPercent < 0) { scrollPercent = 0; } content.y = window.y - (scrollPercent * contentRange); thumb.y = scrollPercent * thumbRange + track.y; } function stopScrollingUp(event:MouseEvent):void { stage.removeEventListener(Event.ENTER_FRAME, scrollUp) stage.removeEventListener(MouseEvent.MOUSE_UP, stopScrollingUp); } [/code]