Learn Development at Frontend Masters
I was recently looking for a way to perform scrolling effects on a project and I stumbled on the Locomotive Scroll library. It lets you perform a variety of scrolling effects, like parallax and triggering/controlling animations at scroll points.
You might also call it a “smooth scrolling” library, but it doesn’t leverage native smooth scrolling — it does just the opposite by virtualizing scrolling and ensuring it’s always smooth. You could probably consider this “scrolljacking” so if you hate that generally, you might hate this, but UX research seems rather mixed on whether it’s actually bad or not. The homepage will give you a good sense of how it works and feels.
Let’s look at the basics of using Locomotive-Scroll JavaScript and how to leverage it to for delightful user experiences.
Here’s what they say:
Locomotive scroll is a simple scroll library, built as a layer on top of ayamflow’s virtual-scroll, it provides smooth scrolling with support for parallax effects, toggling classes, and triggering event listeners when elements are in the viewport.
In other words, it detects when elements are in the viewport and then alters CSS transform
property values on those elements to create scrolling effects.
Oftentimes scrolling effects are called parallax meaning some elements are made to look like they are deep in the background, making them appear to move slower than other elements that are closer to the foreground while scrolling is taking place. Imagine looking out the window from a moving car. The trees far away seems to slowly drift by where the fence right along the road zips quickly by. Sort of like the effect here in this pen from Sarah Drasner:
Locomotive Scroll works primarily through specific attributes in the HTML. Elements with these attributes trigger event listeners in JavaScript when they are in the viewport, then apply CSS transform
values as inline styles.
There are two key attributes to always call upon Locomotive:
Here’s what we’re talking about when we say that the transform
property values are updated in the HTML as inline styles.
We can use the library right as a <script>
tag if we’d like. It’s on CDNs, so like:
Now we look for the container and kick off the library:
The library is on npm as well, so we can use it that way in our build instead with the typical npm install locomotive-scroll
, then:
That means we could use them off Skypack too, like:
That’s really all there is to the setup! It’s pretty plug-and-play like that.
You can probably think of some pretty nice use cases for something like this, but let’s go over a few examples where you might use Locomotive Scroll.
Let’s start with this one:
That HTML has all kinds of data-
attributes going on in there. We’ve already looked at data-scroll
and data-scroll-container
. Here’s what the rest are and what they do:
So, let’s say we are using the data-scroll-sticky
attribute. We always have to set a data-scroll-target
attribute as well, because the target element is usually the container holding the other elements.
Now that we’ve picked one apart, here are a couple of others:
You can also use LocoMotive-Scroll in other frameworks, too. Here’s an example in React:
I can not emphasize the power of Locomotive Scroll enough. I needed to add scroll effects to a side project I was working on, and this was super quick and easy to use. I hope you’re able to use it on a project and experience how great it is for scrolling effects.
Frontend Masters is the best place to get it. They have courses on all the most important front-end technologies, from React to CSS, from Vue to D3, and beyond with Node.js and Full Stack.
Frontend Masters is the best place to get it. They have courses on all the most important front-end technologies, from React to CSS, from Vue to D3, and beyond with Node.js and Full Stack.
Very neat, and very informative article (as always, long time follower here). I will need to check it out again on desktop because some of the demos get cut off on mobile.
You may write comments in Markdown thanks to Jetpack Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (“`) with double new lines before and after. All comments are held for moderation. Be helpful and kind and yours will be published no problem.
The related posts above were algorithmically generated and displayed here without any load on my server at all, thanks to Jetpack.
CSS-Tricks* is created, written by, and maintained by Chris Coyier and a team of swell people. The tech stack for this site is fairly boring. That’s a good thing! I’ve used WordPress since day one all the way up to v17, a decision I’m very happy with. I also leverage Jetpack for extra functionality and Local for local development.
*May or may not contain any actual “CSS” or “Tricks”.
CodePen is a place to experiment, debug, and show off your HTML, CSS, and JavaScript creations.
CSS-Tricks is hosted by Flywheel, the best WordPress hosting in the business, with a local development tool to match.
ShopTalk is a podcast all about front-end web design and development.