UIScrollBar.scroll

Availability

Flash Player 6 (6.0.79.0).

Edition

Flash MX 2004.

Usage

Usage 1:

var listenerObject:Object = new Object();
listenerObject.scroll = function(eventObject:Object) {
    // ...
};
scrollBarInstance.addEventListener("scroll", listenerObject)

Usage 2:

on (scroll) {
    // ...
}

Description

Event; broadcast to all registered listeners when the mouse is clicked (released) over the scroll bar. The UIScrollBar.scrollPosition property and the scroll bar's onscreen image are updated before this event is broadcast.

The first usage example uses a dispatcher/listener event model, in which the script is placed on a frame in the timeline that contains the component instance. A component instance (scrollBarInstance) dispatches an event (in this case, scroll) and the event is handled by a function, also called a handler, on a listener object (listenerObject) that you create. You define a method with the same name as the event on the listener object; the method is called when the event occurs. When the event occurs, it automatically passes an event object (eventObject) to the listener object method. The event object has properties that contain information about the event. You can use these properties to write code that handles the event. Finally, you call addEventListener() (see EventDispatcher.addEventListener()) on the component instance that broadcasts the event to register the listener with the instance. When the instance dispatches the event, the listener is called.

In addition to the normal properties of the event object (type and target), the event object for the scroll event includes a third property named direction. The direction property contains a string describing which way the scroll bar is oriented. The possible values for the direction property are vertical (the default) and horizontal.

For more information about the type and target event object properties, see Event objects.

The second usage example uses an on() handler and must be attached directly to a UIScrollBar component instance. The keyword this, used inside an on() handler attached to a component, refers to the component instance. For example, the following code, attached to the UIScrollBar component instance myUIScrollBarComponent, sends "_level0.myUIScrollBarComponent" to the Output panel:

on (scroll) {
    trace(this);
}

Example

The following example implements Usage 1 and creates a listener object called sbListener with a scroll event handler:

/**
 Requires:
  - UIScrollBar component in library
*/

this.createTextField("my_txt", 10, 10, 20, 200, 100);
my_txt.wordWrap = true;

this.createClassObject(mx.controls.UIScrollBar, "my_sb", 20);

// Set the target text field.
my_sb.setScrollTarget(my_txt);

// Size it to match the text field.
my_sb.setSize(16, my_txt._height); 

// Move it next to the text field.
my_sb.move(my_txt._x + my_txt._width, my_txt._y);

// Create listener object.
var sbListener:Object = new Object();
sbListener.scroll = function(evt_obj:Object){
   // Insert code to handle the "scroll" event.
   trace("text is scrolling");
}
// Add listener.
my_sb.addEventListener("scroll", sbListener);

// Load text to display and define onData handler.
var my_lv:LoadVars = new LoadVars();
my_lv.onData = function(src:String) {
    if (src != undefined) {
        my_txt.text = src;
    } else {
        my_txt.text = "Error loading text.";
    }
};
my_lv.load("http://www.helpexamples.com/flash/lorem.txt");

The following code implements Usage 2. The code is attached to the UIScrollBar component instance and sends a message to the Output panel when the user clicks the scroll bar. The on() handler must be attached directly to the UIScrollBar instance.

on (scroll) {
    trace("UIScrollBar component was clicked");
}