8.15. Responding to Scroll Events

Problem

You want to have some actions performed when a text field’s contents are scrolled.

Solution

Define the text field’s onScroller( ) event handler method. Alternatively, you can use a listener object.

Discussion

When a text field is scrolled vertically or horizontally (meaning that the scroll or hscroll property has been changed either by your custom ActionScript code or by a scrollbar), the onScroller( ) method is automatically invoked. By default, a text field’s onScroller( ) method is undefined, but you can assign it a reference to a function:

myTextField.onScroller = function (  ) {
  trace("text is scrolled");
};

You can also create listener objects for a text field that are notified when the text is scrolled. Listener objects can be any kind of object, such as another text field, a movie clip, or a custom object type. You should define an onScroller( ) method for the listener object, then register the object to the text field using the addListener( ) method. Call the addListener( ) method from the text field and pass it a parameter that references the listener object (the object with onScroller( ) defined). For example:

myListener = new Object(  );
myListener.onScroller = function (  ) {
  // Actions go here.
};
myTextField.addListener(myListener);

You can add multiple listeners to one text field. Working with listeners is a powerful technique because it allows you to define actions that should occur on multiple objects when a text field is changed ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.