SoFunction
Updated on 2025-04-14

Volcano Dynamic Text Scrollbar V5 [AS3 Version]


package 
{
    import ;    
    import ;    
    import ;    
    import ;    
    import ;
    import ;
    import ;
    /**
* @author Lonely Volcano:[url][/url]
     * @version V5 [08.3.15]
* Dynamic text scrollbar
     */
    public class ScrollBar extends Sprite {
//=================== This class attribute===================
////Interface components
        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;
/////Scroll up and down button time
        private var putTime : Number;
        /**
*       * @param scrollText_fc: The scrolled text box
*       * @param scrollBarMc_fc: The scrollbar component on the stage and the agent of this class
*       * @param height_fc: scrollbar high
*       * @param width_fc: scroll bar width
         */
        public function ScrollBar(scrollText_fc : TextField, scrollBarMc_fc : Sprite, height_fc : uint = 0,width_fc : uint = 0) {
//———————Scrollbar_sprite, scrollbar button and slider mc, initialized by scrolled 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;

//—————————Initialization of other attributes
            bg_sprite.useHandCursor = false;
            isSelect = ;
            if(height_fc == 0) {
                bg_sprite.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 = (down_btn.y - up_btn.y - up_btn.height);
            poleStartY = pole_sprite.y = (up_btn.y + up_btn.height);

//————————Register listener
//Text scrolling and mouse wheel
            (, textScroll);
            (MouseEvent.MOUSE_WHEEL, mouseWheel);
//On the scroll button
            up_btn.addEventListener(MouseEvent.MOUSE_DOWN, upBtn);
            up_btn.(MouseEvent.MOUSE_UP, upBtnUp);
//Down scroll button
            down_btn.addEventListener(MouseEvent.MOUSE_DOWN, downBtn);
            down_btn.(MouseEvent.MOUSE_UP, downBtnUp);
//slider
            pole_sprite.addEventListener(MouseEvent.MOUSE_DOWN, poleSprite);
            pole_sprite.(MouseEvent.MOUSE_UP, poleUp);
//Slider background click
            bg_sprite.addEventListener(MouseEvent.MOUSE_DOWN, bgDown);
        }
        /**
* Text scrolling event
         */
        private function textScroll(event : Event) : void {
//Defend whether the slider is displayed and define the slider height according to the text content
            if( != 1) {
                pole_sprite.visible = true;
                up_btn.enabled = true;
                down_btn.enabled = true;
//Define a height factor, which will infinitely tend to 1 as the number of loaded text increases.
                var heightVar : Number = 1 - ( - 1) / ;
//Initialize the height of the slider according to the height factor
                pole_sprite.height = (poleStartHeight * (heightVar, 1 / 3));
                totalPixels = (down_btn.y - up_btn.y - up_btn.height - pole_sprite.height);
                pole_sprite.y = (poleStartY + totalPixels * ( - 1) / ( - 1));
            }else {
                pole_sprite.visible = false;
                up_btn.enabled = false;
                down_btn.enabled = false;
            }
        }
        /**
* Slider scrolling
         */
        private function poleSprite(event : MouseEvent) : void {
//First cancel the text box scroll listening, because the position of the slider will be set when the text is scrolled, and the position of the text is adjusted through the slider, so there will be conflicts.
            (, textScroll);
// Monitor the stage, which ensures that when dragging the slider, the mouse will stop dragging when the mouse is released at any position on the stage.
            scrollBar_sprite.(MouseEvent.MOUSE_UP, poleUp);
//Limit 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 {
//Get the slider position in time during the scrolling process
            var nowPosition : Number = (pole_sprite.y);
//Make the text scroll with the scroll bar, why add 1 here? It can be seen that the scroll attribute value should be positive, that is, it will delete the decimal part instead of rounding?
             = ( - 1) * (nowPosition - poleStartY) / totalPixels + 2;
//Error correction
            var unitPixels : Number = totalPixels / ( - 1);
            if((nowPosition - poleStartY) < unitPixels) {
                 = ( - 1) * (nowPosition - poleStartY) / totalPixels;
            }
        }
        private function poleUp(event : MouseEvent) : void {
            pole_sprite.stopDrag();
            scrollBar_sprite.removeEventListener(Event.ENTER_FRAME, poleDown);
            scrollBar_sprite.(MouseEvent.MOUSE_UP, poleUp);
            (, textScroll);
        }
        /**
* Slider background click
         */
        private function bgDown(event : MouseEvent) : void {    
            var nowPosition : Number;
            if((scrollBar_sprite.mouseY - up_btn.y) < (pole_sprite.height / 2)) {
                nowPosition = (up_btn.y + up_btn.height);
            }else if((down_btn.y - scrollBar_sprite.mouseY) < pole_sprite.height / 2) {
                nowPosition = (down_btn.y - pole_sprite.height);
            }else {
                nowPosition = scrollBar_sprite.mouseY - pole_sprite.height / 2;
            }
            pole_sprite.y = nowPosition;
             = ( - 1) * (nowPosition - poleStartY) / totalPixels + 2;
            var unitPixels : Number = totalPixels / ( - 1);
            if((nowPosition - poleStartY) < unitPixels) {
                 = ( - 1) * (nowPosition - poleStartY) / totalPixels + 1;
            }
        }
        /**
* Down scroll button
         */
        private function downBtn(event : MouseEvent) : void {
            ++;
            pole_sprite.y = (poleStartY + totalPixels * ( - 1) / ( - 1));
//Scroll continuously when the time 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 > 500) {
                ++;
                pole_sprite.y = (poleStartY + totalPixels * ( - 1) / ( - 1));
            }
        }    
        private function downBtnUp(event : MouseEvent) : void {
            scrollBar_sprite.removeEventListener(Event.ENTER_FRAME, downBtnDown);
        }
        /**
* Up the scroll button
         */
        private function upBtn(event : MouseEvent) : void {
            --;
            pole_sprite.y = (poleStartY + totalPixels * ( - 1) / ( - 1));
//Scroll continuously when the time 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 > 500) {
                --;
                pole_sprite.y = (poleStartY + totalPixels * ( - 1) / ( - 1));
            }
        }
        private function upBtnUp(event : MouseEvent) : void {
            scrollBar_sprite.removeEventListener(Event.ENTER_FRAME, upBtnDown);
        }
        /**
* Mouse wheel event
         */
        private function mouseWheel(event : MouseEvent) : void {
            if(isSelect == false) {
                 -= ( / 2);
            }else if(isSelect == true) {
                 = 1;
            }
            pole_sprite.y = (poleStartY + totalPixels * ( - 1) / ( - 1));
        }
    }
}