AS2: AxisScroller
View Example
View Documentation
Download Class & Example Files
Creates a scroller that allows for scrolling in the x or y axis, using the mouse wheel/arrow keys, different easing functions and types, and some other miscellaneous cool things.
-
import mx.utils.Delegate;
-
import mx.transitions.easing.*;
-
import mx.transitions.Tween;
-
-
/**
-
* Creates a scroller that allows for scrolling in the x or y axis, using the mouse wheel/arrow keys, different easing functions and types, and some other miscellaneous cool things.
-
*
-
* @usage
-
* <code>
-
* <pre>
-
import com.reintroducing.ui.AxisScroller;
-
var optionalObj:Object = new Object({ scrollType: "easing",
-
isTrackClickable: true,
-
useArrows: true,
-
upArrow: content_mc.up_btn,
-
downArrow: content_mc.down_btn,
-
easeFunc: Regular.easeOut,
-
duration: .25,
-
arrowMove: 100,
-
scaleScroller: true,
-
autoHideControls: true});
-
var scroller:AxisScroller = new AxisScroller(content_mc, content_mc.scroller_mc, content_mc.movie_mc, content_mc.track_mc, content_mc.mask_mc, "y", optionalObj);
-
* </pre>
-
* </code>
-
*
-
* @author Matt Przybylski [http://www.reintroducing.com]
-
* @version 1.5
-
*/
-
-
class com.reintroducing.ui.AxisScroller
-
{
-
/*
-
* ====================================================================================================
-
* PRIVATE VARIABLES
-
* ====================================================================================================
-
*/
-
-
private var _changeProp:String;
-
private var _mouseAxis:String;
-
private var _changeScale:String;
-
private var _nextPosition:Number;
-
private var _arrowMove:Number;
-
private var _scaleScroller:Boolean;
-
private var _autoHideControls:Boolean;
-
private var _mouseIsPressedDown:Boolean;
-
private var _trackOffset:Number;
-
private var _trackStart:Number;
-
private var _trackEnd:Number;
-
private var _tween:Tween;
-
private var _arrow1Tween:Tween;
-
private var _arrow2Tween:Tween;
-
private var _scrollerTween:Tween;
-
private var _trackTween:Tween;
-
private var _easeFunc:Function;
-
private var _duration:Number;
-
private var _holder:MovieClip;
-
private var _scroller:MovieClip;
-
private var _toBeScrolled:MovieClip;
-
private var _track:MovieClip;
-
private var _mask:MovieClip;
-
private var _axis:String;
-
private var _scrollType:String;
-
private var _isTrackClickable:Boolean;
-
private var _arrows:Boolean;
-
private var _upArrow:MovieClip;
-
private var _downArrow:MovieClip;
-
-
/*
-
* ====================================================================================================
-
* CONSTRUCTOR
-
* ====================================================================================================
-
*/
-
-
/**
-
* Creates a new instance of the AxisScroller class.
-
*
-
* <p>
-
* The $optionalParams parameter takes in a couple of values.
-
* <ul>
-
* <li>scrollType: A string representing the scroll type, "instant" or "easing" (default: "easing")</li>
-
* <li>isTrackClickable: A boolean value that makes the track clickable so that you can scroll directly to a certain spot (default: true)</li>
-
* <li>useArrows: A boolean value that allows the use of arrow buttons (default: false)</li>
-
* <li>upArrow: If useArrows, a movie clip that represents the up arrow button (default: null)</li>
-
* <li>downArrow: If useArrows, a movie clip that represents the down arrow button (default: null)</li>
-
* <li>easeFunc: A function representing the ease type you'd like to use, as per the Tween class easing functions (default: Regular.easeOut)</li>
-
* <li>duration: An integer (in seconds) representing the time that the scrollable content will take to move when easing is used (default: .25)</li>
-
* <li>arrowMove: An integer (in pixels) that the scrollable content will move onMouseWheel/Key/Arrow movement (default: 100)</li>
-
* <li>autoHideControls: A boolean value that hides the scroller/track/arrows (if applicable) if the scroll content is too small to scroll (default: false)</li>
-
* <li>scaleScroller *: A boolean value that allows automatic scaling of the scroll bar (default: false)</li>
-
* </ul>
-
* * If you use the scaleScroller optional parameter, please make the size of the scroll bar the same size as the track and this will take care of
-
* scaling it automatically for you. If you AREN'T using this parameter, please size your scroll bar accordingly manually.
-
* </p>
-
*
-
* @usage <pre><code>var scroller:AxisScroller = new AxisScroller($holder, $scroller, $toBeScrolled, $track, $mask, $axis, $optionalParams);</code></pre>
-
*
-
* @param $holder The movie clip (or timeline) that holds all of the other clips corresponding to the scroller
-
* @param $scroller The movie clip to be used as the scroller
-
* @param $toBeScrolled The movie clip where the scrollable content resides
-
* @param $track The movie clip that is used as the track (measures extremes)
-
* @param $mask The movie clip that is used as the mask for the scrollable content area
-
* @param $axis A string value of either "x" or "y" that determines what axis the content is scrolled along
-
* @param $optionalParams Optional parameters to further control the scroller
-
*/
-
-
public function AxisScroller($holder:MovieClip, $scroller:MovieClip, $toBeScrolled:MovieClip, $track:MovieClip, $mask:MovieClip, $axis:String, $optionalParams:Object)
-
{
-
this._holder = $holder;
-
this._scroller = $scroller;
-
this._toBeScrolled = $toBeScrolled;
-
this._track = $track;
-
this._mask = $mask;
-
this._axis = "_" + $axis;
-
-
this._scrollType = ($optionalParams.scrollType == undefined) ? "easing" : $optionalParams.scrollType;
-
this._isTrackClickable = ($optionalParams.isTrackClickable == undefined) ? true : $optionalParams.isTrackClickable;
-
this._arrows = ($optionalParams.useArrows == undefined) ? false : $optionalParams.useArrows;
-
this._upArrow = ($optionalParams.upArrow == undefined) ? null : $optionalParams.upArrow;
-
this._downArrow = ($optionalParams.downArrow == undefined) ? null : $optionalParams.downArrow;
-
this._easeFunc = ($optionalParams.easeFunc == undefined) ? Regular.easeOut : $optionalParams.easeFunc;
-
this._duration = ($optionalParams.duration == undefined) ? .25 : $optionalParams.duration;
-
this._arrowMove = ($optionalParams.arrowMove == undefined) ? 100 : $optionalParams.arrowMove;
-
this._autoHideControls = ($optionalParams.autoHideControls == undefined) ? false : $optionalParams.autoHideControls;
-
this._scaleScroller = ($optionalParams.scaleScroller == undefined) ? false : $optionalParams.scaleScroller;
-
-
this.manageAxis(this._axis);
-
this.init();
-
}
-
-
/*
-
* ====================================================================================================
-
* PRIVATE FUNCTIONS
-
* ====================================================================================================
-
*/
-
-
// initiates listeners and other events
-
private function init():Void
-
{
-
Mouse.addListener(this);
-
Key.addListener(this);
-
-
this._holder.onEnterFrame = Delegate.create(this, manageScrolling);
-
-
this.activateButtonEvents();
-
}
-
-
// re-activates the button events
-
private function activateButtonEvents():Void
-
{
-
this._scroller.onPress = Delegate.create(this, manageMouseDown);
-
-
this._scroller.onRelease = this._scroller.onReleaseOutside = Delegate.create(this, manageMouseUp);
-
this._scroller.useHandCursor = true;
-
-
if (this._isTrackClickable)
-
{
-
this._track.onRelease = Delegate.create(this, manageTrack);
-
this._track.useHandCursor = true;
-
}
-
-
if (this._arrows)
-
{
-
this._upArrow.onRelease = Delegate.create(this, manageUpArrow);
-
this._downArrow.onRelease = Delegate.create(this, manageDownArrow);
-
-
this._downArrow.useHandCursor = this._upArrow.useHandCursor = true;
-
}
-
}
-
-
// destroys the events on the buttons
-
private function killButtonEvents():Void
-
{
-
this._scroller.onPress = this._scroller.onRelease = this._scroller.onReleaseOutside = null;
-
this._scroller.useHandCursor = false;
-
-
if (this._isTrackClickable)
-
{
-
this._track.onRelease = null;
-
this._track.useHandCursor = false;
-
}
-
-
if (this._arrows)
-
{
-
this._upArrow.onRelease = this._downArrow.onRelease = null;
-
this._upArrow.useHandCursor = this._downArrow.useHandCursor = false;
-
}
-
}
-
-
// sets the axis at which the scroller will function against
-
private function manageAxis($axis:String):Void
-
{
-
if ($axis == "_x")
-
{
-
this._changeProp = "_width";
-
this._mouseAxis = "_xmouse";
-
this._changeScale = "_xscale";
-
}
-
else if ($axis == "_y")
-
{
-
this._changeProp = "_height";
-
this._mouseAxis = "_ymouse";
-
this._changeScale = "_yscale";
-
}
-
-
this._trackOffset = - (this._scroller[this._changeProp] / 2);
-
}
-
-
// calculates values and responds to changes by the scroller
-
private function manageScrolling():Void
-
{
-
var trackSize:Number = this._track[this._changeProp];
-
var trackStart:Number = this._track[this._axis];
-
var trackEnd:Number = this._track[this._axis] + trackSize - (this._scroller[this._changeProp]);
-
var trackRange:Number = trackEnd - trackStart;
-
var maskSize:Number = this._mask[this._changeProp];
-
var movieSize:Number = this._toBeScrolled[this._changeProp];
-
var minPosition:Number = this._mask[this._axis] + 2;
-
var maxPosition:Number = minPosition + movieSize - maskSize + 4;
-
var onePercentChange:Number = (maxPosition - minPosition) / 100;
-
var percent:Number = Math.round(((this._scroller[this._axis] - trackStart) / trackRange) * 100);
-
var targetPosition:Number = Math.round(minPosition - (onePercentChange * percent));
-
-
this._trackStart = trackStart;
-
this._trackEnd = trackEnd;
-
-
if (this._scrollType == "instant")
-
{
-
this._toBeScrolled[this._axis] = targetPosition;
-
}
-
else if (this._scrollType == "easing")
-
{
-
this._toBeScrolled[this._axis] += Math.round((targetPosition - this._toBeScrolled[this._axis]) / 5);
-
}
-
-
if (movieSize <maskSize)
-
{
-
onePercentChange = 0;
-
}
-
-
if (this._mouseIsPressedDown)
-
{
-
this._scroller[this._axis] = Math.round(this._holder[this._mouseAxis] - (this._scroller[this._changeProp] / 2));
-
}
-
-
if (this._scroller[this._axis] <trackStart)
-
{
-
this._scroller[this._axis] = Math.round(trackStart);
-
}
-
else if (this._scroller[this._axis]> trackEnd)
-
{
-
this._scroller[this._axis] = Math.round(trackEnd);
-
}
-
-
if (this._scaleScroller) this.manageScrollScaling(movieSize, maskSize);
-
-
this.manageControls(this._autoHideControls, movieSize, maskSize);
-
}
-
-
// regulates mouse pressed down
-
private function manageMouseDown():Void
-
{
-
this._mouseIsPressedDown = true;
-
}
-
-
// regulates mouse released
-
private function manageMouseUp():Void
-
{
-
this._mouseIsPressedDown = false;
-
}
-
-
// regulates the movement of the content based on where the track is clicked, if applicable
-
private function manageTrack():Void
-
{
-
if (this._isTrackClickable)
-
{
-
if (this._scrollType == "instant")
-
{
-
if (this._holder[this._mouseAxis] <this._trackStart)
-
{
-
this._scroller[this._axis] = this._trackStart;
-
}
-
else if (this._holder[this._mouseAxis]> this._trackEnd)
-
{
-
this._scroller[this._axis] = this._trackEnd;
-
}
-
else
-
{
-
this._scroller[this._axis] = Math.round(this._holder[this._mouseAxis] - (this._scroller[this._changeProp] / 2));
-
}
-
}
-
else if (this._scrollType == "easing")
-
{
-
if (this._holder[this._mouseAxis] <this._trackStart)
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackStart, this._duration, true);
-
}
-
else if (this._holder[this._mouseAxis]> this._trackEnd)
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackEnd, this._duration, true);
-
}
-
else
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], Math.round(this._holder[this._mouseAxis] - (this._scroller[this._changeProp] / 2)), this._duration, true);
-
}
-
}
-
}
-
}
-
-
// regulates the movement of the up arrow, if applicable
-
private function manageUpArrow():Void
-
{
-
if (this._scrollType == "instant")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] - this._arrowMove);
-
-
if (this._nextPosition <this._trackStart)
-
{
-
this._scroller[this._axis] = this._trackStart;
-
}
-
else
-
{
-
this._scroller[this._axis] = this._nextPosition;
-
}
-
}
-
else if (this._scrollType == "easing")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] - this._arrowMove);
-
-
if (this._nextPosition <this._trackStart)
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackStart, this._duration, true);
-
}
-
else
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._nextPosition, this._duration, true);
-
}
-
}
-
}
-
-
// regulates the movement of the down arrow, if applicable
-
private function manageDownArrow():Void
-
{
-
if (this._scrollType == "instant")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] + this._arrowMove);
-
-
if (this._nextPosition> this._trackEnd)
-
{
-
this._scroller[this._axis] = this._trackEnd;
-
}
-
else
-
{
-
this._scroller[this._axis] = this._nextPosition;
-
}
-
}
-
else if (this._scrollType == "easing")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] + this._arrowMove);
-
-
if (this._nextPosition> this._trackEnd)
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackEnd, this._duration, true);
-
}
-
else
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._nextPosition, this._duration, true);
-
}
-
}
-
}
-
-
// auto hides/shows the controls of the scroller
-
private function manageControls($isAutoHideEnabled:Boolean, $contentSize:Number, $maskSize:Number):Void
-
{
-
if ($isAutoHideEnabled && $contentSize <= $maskSize)
-
{
-
// yes, hide controls
-
this._arrow1Tween = new Tween(this._upArrow, "_alpha", this._easeFunc, this._upArrow._alpha, 0, this._duration, true);
-
this._arrow2Tween = new Tween(this._downArrow, "_alpha", this._easeFunc, this._downArrow._alpha, 0, this._duration, true);
-
this._scrollerTween = new Tween(this._scroller, "_alpha", this._easeFunc, this._scroller._alpha, 0, this._duration, true);
-
this._trackTween = new Tween(this._track, "_alpha", this._easeFunc, this._track._alpha, 0, this._duration, true);
-
-
this.killButtonEvents();
-
}
-
else
-
{
-
// no, show controls
-
this._arrow1Tween = new Tween(this._upArrow, "_alpha", this._easeFunc, this._upArrow._alpha, 100, this._duration, true);
-
this._arrow2Tween = new Tween(this._downArrow, "_alpha", this._easeFunc, this._downArrow._alpha, 100, this._duration, true);
-
this._scrollerTween = new Tween(this._scroller, "_alpha", this._easeFunc, this._scroller._alpha, 100, this._duration, true);
-
this._trackTween = new Tween(this._track, "_alpha", this._easeFunc, this._track._alpha, 100, this._duration, true);
-
-
this.activateButtonEvents();
-
}
-
}
-
-
// regulates the scaling of the scroller, if applicable
-
private function manageScrollScaling($contentSize:Number, $maskSize:Number):Void
-
{
-
var trackPercent:Number = Math.ceil(($maskSize / $contentSize) * 100);
-
-
if ($contentSize> $maskSize)
-
{
-
this._scroller[this._changeScale] = trackPercent;
-
}
-
else
-
{
-
this._scroller[this._changeScale] = 100;
-
}
-
}
-
-
// regulates the movement of the mouse wheel
-
private function onMouseWheel($delta:Number):Void
-
{
-
var multiplier:Number = 10;
-
-
if (this._scrollType == "instant")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] - ($delta * multiplier));
-
-
if (this._nextPosition <this._trackStart)
-
{
-
this._scroller[this._axis] = this._trackStart;
-
}
-
else if (this._nextPosition> this._trackEnd)
-
{
-
this._scroller[this._axis] = this._trackEnd;
-
}
-
else
-
{
-
this._scroller[this._axis] = this._nextPosition;
-
}
-
}
-
else if (this._scrollType == "easing")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] - ($delta * multiplier));
-
-
if (this._nextPosition <this._trackStart)
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackStart, this._duration, true);
-
}
-
else if (this._nextPosition> this._trackEnd)
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackEnd, this._duration, true);
-
}
-
else
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._nextPosition, this._duration, true);
-
}
-
}
-
}
-
-
// regulates the movement of the up and down keyboard keys
-
private function onKeyDown():Void
-
{
-
switch(Key.getCode())
-
{
-
case 38: // up
-
if (this._scrollType == "instant")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] - this._arrowMove);
-
-
if (this._nextPosition <this._trackStart)
-
{
-
this._scroller[this._axis] = this._trackStart;
-
}
-
else
-
{
-
this._scroller[this._axis] = this._nextPosition;
-
}
-
}
-
else if (this._scrollType == "easing")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] - this._arrowMove);
-
-
if (this._nextPosition <this._trackStart)
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackStart, this._duration, true);
-
}
-
else
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._nextPosition, this._duration, true);
-
}
-
}
-
break;
-
-
case 40: // down
-
if (this._scrollType == "instant")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] + this._arrowMove);
-
-
if (this._nextPosition> this._trackEnd)
-
{
-
this._scroller[this._axis] = this._trackEnd;
-
}
-
else
-
{
-
this._scroller[this._axis] = this._nextPosition;
-
}
-
}
-
else if (this._scrollType == "easing")
-
{
-
this._nextPosition = Math.round(this._scroller[this._axis] + this._arrowMove);
-
-
if (this._nextPosition> this._trackEnd)
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._trackEnd, this._duration, true);
-
}
-
else
-
{
-
this._tween = new Tween(this._scroller, this._axis, this._easeFunc, this._scroller[this._axis], this._nextPosition, this._duration, true);
-
}
-
}
-
break;
-
}
-
}
-
-
/*
-
* ====================================================================================================
-
* PUBLIC FUNCTIONS
-
* ====================================================================================================
-
*/
-
-
/**
-
* Creates the scroller again if it was deleted using destroy.
-
*
-
* @usage <pre><code>scroller.create();</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function create():Void
-
{
-
this.init();
-
}
-
-
/**
-
* Clears the enterFrame and removes the listeners/events.
-
*
-
* @usage <pre><code>scroller.destroy();</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function destroy():Void
-
{
-
delete this._holder.onEnterFrame;
-
Mouse.removeListener(this);
-
Key.removeListener(this);
-
-
this.killButtonEvents();
-
}
-
-
/*
-
* ====================================================================================================
-
* GETTERS & SETTERS
-
* ====================================================================================================
-
*/
-
-
/**
-
* Gets the value of autoHideControls to see if they are currently being hidden or not.
-
*
-
* @usage <pre><code>trace(scroller.autoHideControls);</code></pre>
-
*/
-
-
public function get autoHideControls():Boolean
-
{
-
return this._autoHideControls;
-
}
-
-
/**
-
* Sets the value of autoHideControls "on-the-fly".
-
*
-
* @usage <pre><code>scroller.autoHideControls = true;</code></pre>
-
*/
-
-
public function set autoHideControls($b:Boolean):Void
-
{
-
this._autoHideControls = $b;
-
}
-
-
/*
-
* ====================================================================================================
-
* CLOSE CLASS
-
* ====================================================================================================
-
*/
-
}
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.
7 Comments
When making the class originally, it was not intended to work on both axis, sorry. It is not possible to do with the way the class is set up because you'd have to have two of everything (tracks, scrollers, etc) and originally it was not anticipated. What you could do though, however, and i haven't tried this, is set up two instances of it, one horizontal and one vertical, and just use the same movieclip/mask as the clips to scroll.
let me know if that works out.
Thanx man. It saved me a lot of time. I'm already working with AS3 and Flex. An application of my client is based on AS2.
I really Loved this Class. Was very useful, this superseded all the classes with the .js functions, that liberated it self from conflicts.
Cheers.













i really like this class, but instantly i thought can i use both axisses on a movieClip.
i tried it but i didn't seem te get it working.
is there a way?
Br,
Max