Create an Auto-Scrolling Vertical Content

October 6th, 2009 in Flash | 41 Comments

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 ...

(41 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.

  22. masterGOmaster
    Mar 24th, 2010

    I will try to use it.thanks

  23. anup
    Apr 6th, 2010

    i need the same effect in flex not even using the mouse
    is it possible?

  24. FLASH AS2
    Apr 9th, 2010

    AS2 version:

    var verticalCenter:Number = Stage.height / 2;
    var limit:Number = Stage.height – content_mc._height;
    var speed:Number = 0.1;
    var scrollY:Number = 0;

    function scrollContent() {
    scrollY = – speed * ( _ymouse – verticalCenter );
    content_mc._y+= scrollY;
    if (content_mc._y>0) { content_mc._y= 0;}
    else if (content_mc._y< limit) { content_mc._y= limit; }
    }

    this.onEnterFrame = function() { scrollContent(); };

  25. Free I Share 分享资源 分享快乐 » Blog Archive » 45个高级Flash Actionscript应用
    Apr 19th, 2010

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

  26. Andrew Arias
    Jul 20th, 2010

    This AS2 not found

    AS2 version:

    var verticalCenter:Number = Stage.height / 2;
    var limit:Number = Stage.height – content_mc._height;
    var speed:Number = 0.1;
    var scrollY:Number = 0;

    function scrollContent() {
    scrollY = – speed * ( _ymouse – verticalCenter );
    content_mc._y+= scrollY;
    if (content_mc._y>0) { content_mc._y= 0;}
    else if (content_mc._y< limit) { content_mc._y= limit; }
    }

    this.onEnterFrame = function() { scrollContent(); };

  27. Andrew Arias
    Jul 20th, 2010

    the script worked for me this way

    var verticalCenter:Number = Stage.height / 2;
    var limit:Number = Stage.height – content_mc._height;
    var speed:Number = 0.1;
    var scrollY:Number = 0;

    function scrollContent() {
    scrollY = -speed*(_ymouse-verticalCenter);
    content_mc._y+= scrollY;
    if (content_mc._y>0) { content_mc._y= 0;}
    else if (content_mc._y< limit) { content_mc._y= limit; }
    }

    this.onEnterFrame = function() { scrollContent(); };

  28. Bhawani Singh Bhati
    Oct 15th, 2010

    var verticalCenter:Number = hg_mask._height / 2;
    var limit:Number = hg_mask._height – content_mc._height;
    var speed:Number = 0.1;
    var scrollY:Number = 0;

    this.onEnterFrame = function() { scrollContent(); };

    function scrollContent() {
    scrollY = – speed * ( _ymouse – verticalCenter );
    content_mc._y+= scrollY;
    if (content_mc._y>0) { content_mc._y= 0;}
    else if (content_mc._y< limit) { content_mc._y= limit; }
    }

  29. Bhawani Singh Bhati
    Oct 15th, 2010

    if You are using mask then use above script it works

  30. Chris
    Feb 7th, 2011

    How would you go about making each image linked to a different page?

  31. 45+ Advanced Adobe Flash Actionscript Trainings - Flash24h.com | Thế giới Flash của bạn!
    Apr 14th, 2011

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

  32. Steven
    Jun 2nd, 2011

    OMG THANKS ive been looking all over for something like this

  33. Emran Naziri
    Jun 29th, 2011

    Dear All,

    How can i make the above scroller work in a loop. Appreciate your help guys.

    God Bless

  34. Neel
    Jul 4th, 2011

    its very easy and usefull. i need same easy Horizontal Auto-Scrolling Content.

  35. Rupali
    Jul 12th, 2011

    Hi,

    I used this link for image scrolling, I want to link the image to another page, please tell me how to do it

    var verticalCenter:Number = stage.stageHeight / 2;
    var limit:Number = stage.stageHeight – content_mc.height;
    var speed:Number = 0.05;
    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; }
    }

  36. Arron M
    Jul 15th, 2011

    Hey, thanks for this, However I could do with some help, I have used UILoaders to load external images into the content_mc (as I want to be able to replace these images regularily without editing the .flv each time) When I go to test I get the following errors:

    Error: Error #2099: The loading object is not sufficiently loaded to provide this information.
    at flash.display::LoaderInfo/get loader()
    at fl.display::ProLoader/get realLoader()
    at fl.display::ProLoaderInfo()
    at fl.display::ProLoader()
    at fl.containers::UILoader/initLoader()
    at fl.containers::UILoader/load()
    at fl.containers::UILoader/set source()
    at AutoScrollingVerticalGallery_fla::Content_1/__setProp_Image006_Content_Layer1_0()
    at AutoScrollingVerticalGallery_fla::Content_1()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
    at AutoScrollingVerticalGallery_fla::MainTimeline()
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at AutoScrollingVerticalGallery_fla::MainTimeline/frame1()

    Could someone please advise?

  37. chaman
    Aug 4th, 2011

    i want to scroll text automaticaly in loop in a slide show in as2 pls tell me

  38. MaHeN
    Aug 7th, 2011

    Use This Code if you the content to scroll within a “box”
    and giv a instance name of “boxclip” for the box:

    var verticalCenter:Number = boxclip.y + ( boxclip.height / 2 );
    var limit:Number = boxclip.height – content_mc.height + boxclip.y;
    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>boxclip.y) { content_mc.y= boxclip.y;}
    else if (content_mc.y< limit) { content_mc.y= limit; }
    }

  39. Scotty G the Webmaster
    Dec 16th, 2011

    Thanks for someone finally putting up some code that is short and straight to the point and easy for me to adapt to my project without having to weed through tons of crap to make it work! Perfect!

    -Scott

  40. LastArcher
    Dec 27th, 2011

    Thanks all

    all Works Great
    But if U are a beginner than
    do not copy and paste the script in your application

    Just write into it
    than works great

Leave a Comment

Name (required)

E-mail (required, will not be published)

Website