在线演示
打包文件下载
复制代码 代码如下:
packagecom.qoolu.component
{
importflash.events.MouseEvent;
importflash.events.Event;
importflash.display.SimpleButton;
importflash.text.TextField;
importflash.display.Sprite;
importflash.utils.getTimer;
importflash.geom.Rectangle;
/**
*@author寂寞火山:[url]http://www.huoshan.org[/url]
*@versionV5[08.3.15]
*动态文本滚动条
*/
publicclassScrollBarextendsSprite{
//=============本类属性==============
////接口元件
privatevarscrollText:TextField;
privatevarscrollBar_sprite:Sprite;
privatevarup_btn:SimpleButton;
privatevardown_btn:SimpleButton;
privatevarpole_sprite:Sprite;
privatevarbg_sprite:Sprite;
////初始数据
privatevarpoleStartHeight:Number;
privatevarpoleStartY:Number;
privatevartotalPixels:Number;
privatevarisSelect:Boolean;
////上下滚动按钮按钮下时间
privatevarputTime:Number;
/**
*@paramscrollText_fc:被滚动的文本框
*@paramscrollBarMc_fc:舞台上与本类所代理的滚动条元件
*@paramheight_fc:滚动条高
*@paramwidth_fc:滚动条宽
*/
publicfunctionScrollBar(scrollText_fc:TextField,scrollBarMc_fc:Sprite,height_fc:uint=0,width_fc:uint=0){
//——————滚动条_sprite,滚动条按钮和滑块mc,被滚动的文本域初始化
scrollText=scrollText_fc;
scrollBar_sprite=scrollBarMc_fc;
up_btn=SimpleButton(scrollBar_sprite.getChildByName("up_btn"));
down_btn=SimpleButton(scrollBar_sprite.getChildByName("down_btn"));
pole_sprite=Sprite(scrollBar_sprite.getChildByName("pole_mc"));
bg_sprite=Sprite(scrollBar_sprite.getChildByName("bg_mc"));
//——————可用性控制
pole_sprite.visible=false;
up_btn.enabled=false;
down_btn.enabled=false;
//——————其他属性初始化
bg_sprite.useHandCursor=false;
isSelect=scrollText.selectable;
if(height_fc==0){
bg_sprite.height=scrollText.height;
}else{
bg_sprite.height=height_fc;
}
if(width_fc!=0){
bg_sprite.width=width_fc+2;
pole_sprite.width=width_fc;
up_btn.width=up_btn.height=down_btn.width=down_btn.height=width_fc;
}
down_btn.y=bg_sprite.y+bg_sprite.height-down_btn.height-1;
poleStartHeight=Math.floor(down_btn.y-up_btn.y-up_btn.height);
poleStartY=pole_sprite.y=Math.floor(up_btn.y+up_btn.height);
//——————注册侦听器
//文本滚动与鼠标滚轮
scrollText.addEventListener(Event.SCROLL,textScroll);
scrollText.addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheel);
//上滚动按钮
up_btn.addEventListener(MouseEvent.MOUSE_DOWN,upBtn);
up_btn.stage.addEventListener(MouseEvent.MOUSE_UP,upBtnUp);
//下滚动按钮
down_btn.addEventListener(MouseEvent.MOUSE_DOWN,downBtn);
down_btn.stage.addEventListener(MouseEvent.MOUSE_UP,downBtnUp);
//滑块
pole_sprite.addEventListener(MouseEvent.MOUSE_DOWN,poleSprite);
pole_sprite.stage.addEventListener(MouseEvent.MOUSE_UP,poleUp);
//滑块背景点击
bg_sprite.addEventListener(MouseEvent.MOUSE_DOWN,bgDown);
}
/**
*文本滚动事件
*/
privatefunctiontextScroll(event:Event):void{
//判断滑块儿是否显示,并根据文本内容多少定义滑块高度
if(scrollText.maxScrollV!=1){
pole_sprite.visible=true;
up_btn.enabled=true;
down_btn.enabled=true;
//定义一个高度因子,此因子随加载文本的增多,将无限趋向于1
varheightVar:Number=1-(scrollText.maxScrollV-1)/scrollText.maxScrollV;
//根据高度因子初始化滑块的高度
pole_sprite.height=Math.floor(poleStartHeight*Math.pow(heightVar,1/3));
totalPixels=Math.floor(down_btn.y-up_btn.y-up_btn.height-pole_sprite.height);
pole_sprite.y=Math.floor(poleStartY+totalPixels*(scrollText.scrollV-1)/(scrollText.maxScrollV-1));
}else{
pole_sprite.visible=false;
up_btn.enabled=false;
down_btn.enabled=false;
}
}
/**
*滑块滚动
*/
privatefunctionpoleSprite(event:MouseEvent):void{
//首先取消文本框滚动侦听,因为文本滚动的时候会设置滑块的位置,而此时是通过滑块调整文本的位置,所以会产生冲突
scrollText.removeEventListener(Event.SCROLL,textScroll);
//监听舞台,这样可以保证拖动滑竿的时候,鼠标在舞台的任意位置松手,都会停止拖动
scrollBar_sprite.stage.addEventListener(MouseEvent.MOUSE_UP,poleUp);
//限定拖动范围
vardragRect:Rectangle=newRectangle(pole_sprite.x,poleStartY,0,totalPixels);
pole_sprite.startDrag(false,dragRect);
scrollBar_sprite.addEventListener(Event.ENTER_FRAME,poleDown);
}
privatefunctionpoleDown(event:Event):void{
//在滚动过程中及时获得滑块所处位置
varnowPosition:Number=Math.floor(pole_sprite.y);
//使文本随滚动条滚动,这里为什么要加1,可见scroll属性值应该是取正的,也就是说它会删除小数部分,而非采用四舍五入制?
scrollText.scrollV=(scrollText.maxScrollV-1)*(nowPosition-poleStartY)/totalPixels+2;
//误差校正
varunitPixels:Number=totalPixels/(scrollText.maxScrollV-1);
if((nowPosition-poleStartY)<unitPixels){
scrollText.scrollV=(scrollText.maxScrollV-1)*(nowPosition-poleStartY)/totalPixels;
}
}
privatefunctionpoleUp(event:MouseEvent):void{
pole_sprite.stopDrag();
scrollBar_sprite.removeEventListener(Event.ENTER_FRAME,poleDown);
scrollBar_sprite.stage.removeEventListener(MouseEvent.MOUSE_UP,poleUp);
scrollText.addEventListener(Event.SCROLL,textScroll);
}
/**
*滑块背景点击
*/
privatefunctionbgDown(event:MouseEvent):void{
varnowPosition:Number;
if((scrollBar_sprite.mouseY-up_btn.y)<(pole_sprite.height/2)){
nowPosition=Math.floor(up_btn.y+up_btn.height);
}elseif((down_btn.y-scrollBar_sprite.mouseY)<pole_sprite.height/2){
nowPosition=Math.floor(down_btn.y-pole_sprite.height);
}else{
nowPosition=scrollBar_sprite.mouseY-pole_sprite.height/2;
}
pole_sprite.y=nowPosition;
scrollText.scrollV=(scrollText.maxScrollV-1)*(nowPosition-poleStartY)/totalPixels+2;
varunitPixels:Number=totalPixels/(scrollText.maxScrollV-1);
if((nowPosition-poleStartY)<unitPixels){
scrollText.scrollV=(scrollText.maxScrollV-1)*(nowPosition-poleStartY)/totalPixels+1;
}
}
/**
*下滚动按钮
*/
privatefunctiondownBtn(event:MouseEvent):void{
scrollText.scrollV++;
pole_sprite.y=Math.floor(poleStartY+totalPixels*(scrollText.scrollV-1)/(scrollText.maxScrollV-1));
//当鼠标在按钮上按下的时间大于设定时间时,连续滚动
putTime=getTimer();
scrollBar_sprite.addEventListener(Event.ENTER_FRAME,downBtnDown);
}
privatefunctiondownBtnDown(event:Event):void{
if(getTimer()-putTime>500){
scrollText.scrollV++;
pole_sprite.y=Math.floor(poleStartY+totalPixels*(scrollText.scrollV-1)/(scrollText.maxScrollV-1));
}
}
privatefunctiondownBtnUp(event:MouseEvent):void{
scrollBar_sprite.removeEventListener(Event.ENTER_FRAME,downBtnDown);
}
/**
*上滚动按钮
*/
privatefunctionupBtn(event:MouseEvent):void{
scrollText.scrollV--;
pole_sprite.y=Math.floor(poleStartY+totalPixels*(scrollText.scrollV-1)/(scrollText.maxScrollV-1));
//当鼠标在按钮上按下的时间大于设定时间时,连续滚动
putTime=getTimer();
scrollBar_sprite.addEventListener(Event.ENTER_FRAME,upBtnDown);
}
privatefunctionupBtnDown(event:Event):void{
if(getTimer()-putTime>500){
scrollText.scrollV--;
pole_sprite.y=Math.floor(poleStartY+totalPixels*(scrollText.scrollV-1)/(scrollText.maxScrollV-1));
}
}
privatefunctionupBtnUp(event:MouseEvent):void{
scrollBar_sprite.removeEventListener(Event.ENTER_FRAME,upBtnDown);
}
/**
*鼠标滚轮事件
*/
privatefunctionmouseWheel(event:MouseEvent):void{
if(isSelect==false){
scrollText.scrollV-=Math.floor(event.delta/2);
}elseif(isSelect==true){
event.delta=1;
}
pole_sprite.y=Math.floor(poleStartY+totalPixels*(scrollText.scrollV-1)/(scrollText.maxScrollV-1));
}
}
}