Scrollbox

Code Actionscript 2.0

Instance action

onClipEvent (load) {
// highest and lowest possible position of scrollbar:
top = _parent.box._y + 2;
bottom = (_parent.box._height - 2) + _parent.box._y - this._height;

// range of possible values:
scrollrange = bottom - top;

// proportion between height of scrolled content and scrollrange:
scrollfactor = (_parent.content._height - (_parent.box._height - 8)) / scrollrange;
py = top;
mousepressed = 0;
}
on (press) {
mousepressed = 1;
}
on (release, releaseOutside) {
mousepressed = 0;
}
onClipEvent (enterFrame) {
if (mousepressed == 1) {
py = _parent._ymouse - this._height / 2;
}
if (py < top) {
py = top;
}
if (py > bottom) {
py = bottom;
}
this._y = py;
_parent.content._y = 2 - scrollfactor * (py - top);
}

Code Actionscript 3.0

stage.addEventListener(Event.ENTER_FRAME, enterFrame); stage.addEventListener(MouseEvent.MOUSE_UP, releaseOutside); bar.addEventListener(MouseEvent.MOUSE_DOWN, press); bar.addEventListener(MouseEvent.MOUSE_UP, release); // highest and lowest possible position of scrollbar: var top = box.y + 2; var bottom = (box.height - 2) + box.y - bar.height; // range of possible values: var scrollrange = bottom - top; // by how much does content exceed box: var scrollfactor = (content.height - (box.height - 8)) / scrollrange; var py = top; var mousepressed = 0; function press(e:MouseEvent) { mousepressed = 1; } function release(e:MouseEvent){ mousepressed = 0; } function releaseOutside(e:MouseEvent) { mousepressed = 0; } function enterFrame(event:Event) { if (mousepressed == 1) { py = mouseY - bar.height / 2; } if (py < top) { py = top; } if (py > bottom) { py = bottom; } bar.y = py; content.y = top - scrollfactor * (py - top); }

Description

With this scrollbar, various content (text or image) that is larger than the surrounding box can be scrolled. The variables top and bottom denote the coordinates of the surrounding box. The scrollfactor denotes the relation between the height of the exceeding content and the height of the box. To scroll objects other than textblocks, the instance 'content' has to be replaced by a picture or alternate object.

related to: Mouse position, Slider

Download

Right click: Flashfile AS 2.0 | Flashfile AS 3.0 | SWF-File


Share