Flash AS3教程:动态文本滚动条

2019-10-08 20:44:59于丽

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);
}
/**
* 文本滚动事件
*/
private function textScroll(event : Event) : void {
//判断滑块儿是否显示,并根据文本内容多少定义滑块高度
if(scrollText.maxScrollV != 1) {
pole_sprite.visible = true;
up_btn.enabled = true;
down_btn.enabled = true;
//定义一个高度因子,此因子随加载文本的增多,将无限趋向于1
var heightVar : 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;
}
}
/**
* 滑块滚动