ScrollMagic for Beginners Tutorial

ScrollMagic is a JavaScript library for creating scroll interactions. It lets you create animations that start and stop at specific points during the scrolling of a page. It’s quite powerful, but can be intimidating for beginners, since unlike Skrollr, it’s all done in JavaScript.

In this tutorial, I’ll cover the basics of adding ScrollMagic to a page and creating basic animations. I will also introduce more advanced animations with the help of GSAP.

We will animate 3 different “slides”. The first one will have a parallax background (GSAP), the second and third will scroll with a “pin” effect (ScrollMagic only), and the third will also demonstrate three simple animations: fly in from the left, fade in, and fly in from the bottom (GSAP).

animation result animated gif

End result. See the demo

Step 1 – HTML and CSS setup

In your HTML, you will need the following elements:

Also, the following CSS (substitute the background image in the parallax section for your own):

Step 2 – Link the scripts

You can download ScrollMagic from their site or simply link to the scripts directly to their CDN. In this tutorial, I will use the CDN for simplicity.

Although not mandatory, there three additional scripts that I will use during the tutorial:

  • ScrollMagic indicators: this script displays the trigger, start and end point of an animation, which makes debugging much easier. This file is included in the ScrollMagic master download, or you can use the CDN
  • GSAP‘s TweenMax: GreenSock offers a robust animation library that adds more complex animations to ScrollMagic
  • ScrollMagic GSAP animation: Also included in the master download (or via CDN), this plugin allows ScrollMagic to work with GSAP.

Remember to also create your own JavaScript file to include your custom code. In my case, it’s called app.js, and it’s in my js folder.

Step 3 – Create a controller

In your JavaScript file, add the following:

This will create a ScrollMagic controller that will house all of your animations.

Step 4 – Parallax effect

Now that we have a controller, we can start adding animations to it. The first one we’ll do is the parallax animation.

Whenever you want to add a new animation, you will need to create a new “Scene”. The scene will contain the following elements:

  • triggerElement: The element to watch. In this case, we want the animation to start when #parallax enters the screen
  • triggerHook: The point on the screen when the animation starts. By default, the animation will start when the triggerElement hits the middle of the screen (“onCenter”), so we need to change it to “onEnter”
  • duration: How long will the animation last. If you set it to 100%, the animation will last one full screen. For parallax, we want it to last 200%, but you can play with this number until you get the desired results
  • setTween: This is a shorthand to add a GSAP animation. Here, we need to add the element we want to animate (in this case it’s the same one we are watching), and in an array, the animation information. For a simple parallax effect, we simply move the background vertically from 0 to 100%
  • addIndicators: If you want to see the trigger, start and end points of the animation, uncomment this line
  • addTo: Once our scene is complete, we’ll add to the controller

If you want to get better acquainted with these parameters, visit ScrollMagic’s examples, and in particular the Scene Manipulation example, where you can play with some of them.

Step 5 – Slide and pin animations

The next animation is the slide and pin animations for the second and third slides.

Both slides need the same animation:

  • triggerHook: we want the slides to become pinned to the screen once they are about to leave the screen, hence the “onLeave”
  • setPin: we set the animation to be one of ScrollMagic’s built-in behaviors, the pin. For this animation, GSAP is not necessary

If you look at ScrollMagic’s example of this effect, you will see how to make your code more efficient by automatically creating the animation for all slides at once, instead of having to create one per slide manually.

Step 6 – Other Animations

On the last slide, I have 3 animations. Each one requires the use of GSAP timelines. These timelines let us create more complex sequences than with a simple tween, as before.

In the first animation, we create a GSAP timeline that will have two points. It will start with our element #left placed 500 pixels to the left of the screen, and it will end with it at its original position.

We first create a new timeline, then we establish the starting, or “from” point, and then the end or “to” point. Lastly, we add them to the timeline.

Then we create our scene as before:

In this case, I added an offset of 200, so the start point is 200 pixels below the top of the #slidein2. We add the timeline via setTween(), and set the duration to a scroll of 400 pixels.

The reverse(false) can be uncommented if you want the animation to only happen once, instead of every time you scroll up or down.

Here are the other 2 animations, which are very similar. Each one has its own timeline, and scene:

Conclusion

You can look at the result here.

I’d recommend that you take some time to check out the other examples ScrollMagic has available, and GSAP’s Jump Start examples to better understand your animation options.

2 Responses to “ScrollMagic for Beginners Tutorial”

  1. Katie

    I tried replacing your h1 text in the bottom frame with an svg to have an svg fly in from the left and nothing appears. Do you have any idea why that might be?

    • Alicia Ramirez

      I tried adding an SVG to mine and it worked. Make sure the SVG shows when not in ScrollMagic (try replacing the first H1 or on an element that is not affected by the script). I suspect it may be a size or color issue with the SVG. It’s hard to tell without being able to look at your code. If you still have issues, please post on StackOverflow.