Online Demo
Package File Download
CopyCode The Code is as follows: Package com. qoolu. Component
{
Import flash. Events. mouseevent;
Import flash. Events. event;
Import flash. display. simplebutton;
Import flash. Text. textfield;
Import flash. display. Sprite;
Import flash. utils. gettimer;
Import flash. Geom. rectangle;
/**
* @ Author lonely volcano: [url] http://www.huoshan.org [/url]
* @ Version V5 [08.3.15]
* Dynamic text scroll bar
*/
Public class scrollbar extends sprite {
// ================= Attributes of this class ====================
/// Interface Element
Private var scrolltext: textfield;
Private var scrollbar_sprite: SPRITE;
Private var up_btn: simplebutton;
Private var down_btn: simplebutton;
Private var pole_sprite: SPRITE;
Private var bg_sprite: SPRITE;
//// Initial data
Private var polestartheight: number;
Private var polestarty: number;
Private var totalpixels: number;
Private var isselect: Boolean;
//// The Time of the scroll up or down button
Private var puttime: number;
/**
* @ Param scrolltext_fc: scrolling text box
* @ Param scrollbarmc_fc: the scroll bar element on the stage and proxy of this class.
* @ Param height_fc: the height of the scroll bar.
* @ Param width_fc: Scroll Bar Width
*/
Public Function scrollbar (scrolltext_fc: textfield, scrollbarmc_fc: Sprite, height_fc: uint = 0, width_fc: uint = 0 ){
// ------ Scroll bar _ Sprite, scroll bar button and slider MC, initialized by the scrolling text field
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 "));
// ------ Availability Control
Pole_sprite.visible = false;
Up_btn.enabled = false;
Down_btn.enabled = false;
// ------ Initialize other attributes
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 );
// ------ Register the listener
// Text scroll and scroll wheel
Scrolltext. addeventlistener (event. Scroll, textscroll );
Scrolltext. addeventlistener (mouseevent. mouse_wheel, mousewheel );
// Scroll up button
Up_btn.addeventlistener (mouseevent. mouse_down, upbtn );
Up_btn.stage.addeventlistener (mouseevent. mouse_up, upbtnup );
// Scroll down button
Down_btn.addeventlistener (mouseevent. mouse_down, downbtn );
Down_btn.stage.addeventlistener (mouseevent. mouse_up, downbtnup );
// Slider
Pole_sprite.addeventlistener (mouseevent. mouse_down, polesprite );
Pole_sprite.stage.addeventlistener (mouseevent. mouse_up, poleup );
// Click the slider background
Bg_sprite.addeventlistener (mouseevent. mouse_down, bgdown );
}
/**
* Text rolling event
*/
Private function textscroll (Event: Event): void {
// Determine whether the slider is displayed and define the height of the slider based on the content of the text.
If (scrolltext. maxscrollv! = 1 ){
Pole_sprite.visible = true;
Up_btn.enabled = true;
Down_btn.enabled = true;
// Define a height factor. As the number of loaded texts increases, the factor tends infinitely to 1.
VaR heightvar: Number = 1-(scrolltext. maxscrollv-1)/scrolltext. maxscrollv;
// Initialize the height of the slider Based on the height factor.
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;
}
}
/**
* Slider scroll
*/
Private function polesprite (Event: mouseevent): void {
// Cancel the text box scroll listening first, because the slider position is set during text scrolling, and the text position is adjusted by the slider, so a conflict occurs.
Scrolltext. removeeventlistener (event. Scroll, textscroll );
// Listen to the stage. This ensures that when you drag the slider, the mouse stops dragging any position on the stage.
Scrollbar_sprite.stage.addeventlistener (mouseevent. mouse_up, poleup );
// Limit the drag range
VaR dragrect: rectangle = new rectangle (pole_sprite.x, polestarty, 0, totalpixels );
Pole_sprite.startdrag (false, dragrect );
Scrollbar_sprite.addeventlistener (event. enter_frame, poledown );
}
Private function poledown (Event: Event): void {
// Obtain the slider position in time during the Rolling Process
VaR nowposition: Number = math. Floor (pole_sprite.y );
// Make the text scroll with the scroll bar. Why do we add 1 here? Can we see that the scroll attribute value is positive, that is, it deletes the decimal part instead of rounding it?
Scrolltext. scrollv = (scrolltext. maxscrollv-1) * (nowposition-polestarty)/totalpixels + 2;
// Error Correction
VaR unitpixels: Number = totalpixels/(scrolltext. maxscrollv-1 );
If (nowposition-polestarty) <unitpixels ){
Scrolltext. scrollv = (scrolltext. maxscrollv-1) * (nowposition-polestarty)/totalpixels;
}
}
Private function poleup (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 );
}
/**
* Click the slider background
*/
Private function bgdown (Event: mouseevent): void {
VaR nowposition: number;
If (scrollbar_sprite.mousey-up_btn.y) <(pole_sprite.height/2 )){
Nowposition = math. Floor (up_btn.y + up_btn.height );
} Else if (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;
VaR unitpixels: Number = totalpixels/(scrolltext. maxscrollv-1 );
If (nowposition-polestarty) <unitpixels ){
Scrolltext. scrollv = (scrolltext. maxscrollv-1) * (nowposition-polestarty)/totalpixels + 1;
}
}
/**
* Scroll down button
*/
Private function downbtn (Event: mouseevent): void {
Scrolltext. scrollv ++;
Pole_sprite.y = math. Floor (polestarty + totalpixels * (scrolltext. scrollv-1)/(scrolltext. maxscrollv-1 ));
// Continuous scrolling when the time when the mouse is pressed on the button is greater than the set time
Puttime = gettimer ();
Scrollbar_sprite.addeventlistener (event. enter_frame, downbtndown );
}
Private function downbtndown (Event: Event): void {
If (gettimer ()-puttime & gt; 500 ){
Scrolltext. scrollv ++;
Pole_sprite.y = math. Floor (polestarty + totalpixels * (scrolltext. scrollv-1)/(scrolltext. maxscrollv-1 ));
}
}
Private function downbtnup (Event: mouseevent): void {
Scrollbar_sprite.removeeventlistener (event. enter_frame, downbtndown );
}
/**
* Scroll up button
*/
Private function upbtn (Event: mouseevent): void {
Scrolltext. scrollv --;
Pole_sprite.y = math. Floor (polestarty + totalpixels * (scrolltext. scrollv-1)/(scrolltext. maxscrollv-1 ));
// Continuous scrolling when the time when the mouse is pressed on the button is greater than the set time
Puttime = gettimer ();
Scrollbar_sprite.addeventlistener (event. enter_frame, upbtndown );
}
Private function upbtndown (Event: Event): void {
If (gettimer ()-puttime & gt; 500 ){
Scrolltext. scrollv --;
Pole_sprite.y = math. Floor (polestarty + totalpixels * (scrolltext. scrollv-1)/(scrolltext. maxscrollv-1 ));
}
}
Private function upbtnup (Event: mouseevent): void {
Scrollbar_sprite.removeeventlistener (event. enter_frame, upbtndown );
}
/**
* Scroll wheel event
*/
Private function mousewheel (Event: mouseevent): void {
If (isselect = false ){
Scrolltext. scrollv-= math. Floor (event. Delta/2 );
} Else if (isselect = true ){
Event. Delta = 1;
}
Pole_sprite.y = math. Floor (polestarty + totalpixels * (scrolltext. scrollv-1)/(scrolltext. maxscrollv-1 ));
}
}
}