Alicia Ramirez

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

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:

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:

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:

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.

Exit mobile version