Create an Auto-Scrolling Vertical Content

October 6th, 2009 in Flash

In this lesson, let’s see how to build an auto-scrolling vertical content that scrolls from top to bottom and vice-versa according to the mouse position.

View DemoDownload Source

1. Create a new flash file (Actionscript 3.0) and save it as AutoScrollVerticalContent.fla. Set the frame rate to 30fps.

2. Rename “layer 1″ to “content” and on this layer put the content that you want to scroll.
Convert your content to a movie clip with registration point at the top. Give it an instance name of “content_mc”.
Center the content on the stage.

3. Create a new “actions” layer and with its first frame selected open the actions panel.
In order to scroll from top to bottom and vice-versa, we need to determine the limits inside which the content can scroll, which means that we have to determine the y coordinate limits. The schema below recaps it :

Screenshot

To sum up, stage.stageHeight – content-mc.height < content_mc.y < 0.

4. In the code, create the following variables :

var verticalCenter:Number = stage.stageHeight / 2;
var limit:Number = stage.stageHeight - content_mc.height;
var speed:Number = 0.1;
var scrollY:Number = 0;

5. Next add an ENTER_FRAME event listener that will be handled by the scrollContent function.

addEventListener(Event.ENTER_FRAME, scrollContent);

6. In the scrollContent function, we set the y property of “content_mc” to its new value scrollY, that is calculated according to the mouse position relative to the stage’s center.
Finally, we check if we are still in the limits and if we’ve reached them we set the y property in order to block the content from scrolling.

7. Here’s the final code, test your movie to see it in application.

var verticalCenter:Number = stage.stageHeight / 2;
var limit:Number = stage.stageHeight - content_mc.height;
var speed:Number = 0.1;
var scrollY:Number = 0;

addEventListener(Event.ENTER_FRAME, scrollContent);

function scrollContent(e:Event):void {
	scrollY = - speed * ( mouseY - verticalCenter );
	content_mc.y+= scrollY;
	if (content_mc.y>0) { content_mc.y= 0;}
 	else if (content_mc.y< limit) { content_mc.y= limit; }
}
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Twitter


What you think ...

(21 Comments)

+ Add a Comment
  1. Nick
    Oct 7th, 2009

    yo nice, sweet and simple,

    but is it possible you create a horizontal one with scrollers that loads dynamic text/image/swf tutorial? i seen a couple normal xml galleries here and elsewhere, can’t seems to find one to suit what that I really need.

  2. Nath
    Oct 8th, 2009

    Really really cool
    Thx :-)

  3. Ryan
    Oct 19th, 2009

    Nice.
    Is there any way to do it in Actionscript 2.0?

  4. Micha
    Oct 19th, 2009

    Hello – very nice! Also looking for a way in Actionscript 2.0 – can anyone help PLZ!

  5. 45+ Advanced Adobe Flash Actionscript Trainings | Master Design
    Nov 1st, 2009

    [...] Create an Auto-Scrolling Vertical Content [...]

  6. Matt
    Nov 4th, 2009

    For horizontal just do the opposite of all the Y

  7. Matt
    Nov 4th, 2009

    BTW, thank you for this great tutorial. Something like this is VERY hard to find online for some reason.

  8. Remco
    Nov 6th, 2009

    First of all, Great tutorial! But is it also possible to integrate a neutral position in the slider, I want to implement some text in my slider so it would be great if it is possible.

  9. TC
    Nov 7th, 2009

    I’m new to Flash and I’m having trouble adding my own images. Any advice would be appreciated.

  10. Adilson Vicnete
    Nov 16th, 2009

    This is really cool and very easy.

    But I need just a little more :P can you tell me how to create a link for each image? So I can open a web site in a blank window?

    Plea se give me a help, I’ve been trying to reach the code for 2 weeks and I get nothing but insomnia.

    Thank you very much.

  11. Advanced Adobe Flash Actionscript Trainings « Flash Criminals
    Nov 26th, 2009

    [...] Create an Auto-Scrolling Vertical Content [...]

  12. Insane
    Nov 26th, 2009

    Simple but usefull, thank you.

  13. Zach
    Dec 13th, 2009

    This works perfect! I’ve been searching for something like this for a long time

  14. The Best of 2009 in Flash Web Design - Flash Web Design and Design Photography | DesignOra
    Dec 30th, 2009

    [...] Create an Auto-Scrolling Vertical Content link [...]

  15. nobody
    Dec 31st, 2009

    Thank you its working for me.
    But how can I make it should stop when the mouse is not on the images?

  16. jim
    Jan 3rd, 2010

    Excellent ! Simple and efficient. Thanks

  17. Juliette
    Jan 14th, 2010

    actionscript for horizontal movement:

    var horizontalCenter:Number = stage.stageWidth / 2;
    var limit:Number = stage.stageWidth – content_mc.width;
    var speed:Number = 0.1;
    var scrollX:Number = 0;

    addEventListener(Event.ENTER_FRAME, scrollContent);

    function scrollContent(e:Event):void {
    scrollX = – speed * ( mouseX – horizontalCenter );
    content_mc.x+= scrollX;
    if (content_mc.x>0) { content_mc.x= 0;}
    else if (content_mc.x< limit) { content_mc.x= limit; }
    }

  18. Aaron
    Jan 19th, 2010

    I have inserted this behind a mask on a larger page but when my mouse moves off of the images they keep on scrolling, is there a work around for this?

    Please help!!

    Great tut by the waybeen looking around for this for a long time, well done.

  19. Joseph
    Jan 29th, 2010

    Is there a way to make the image start to scrolling up automatically when it first loads?

  20. jonathan
    Feb 3rd, 2010

    just wondering how to set a custom X and Y so its not the whole stage?

  21. flexbeginner
    Feb 22nd, 2010

    great work ! ! !
    can we make same auto scrolling component in flex because i dont know how to work on flash.can u help me.

Leave a Comment

Name (required)

E-mail (required, will not be published)

Website