Back
Using Locomotive Scroll in Vue for Smoother UX
Websites use scrolling animations to make browsing more enjoyable. In this article, we’ll use locomotive scroll in Vue to create a smoother user experience.
Websites, especially those with enormous amounts of material, can use scrolling animations as features to make browsing more enjoyable. With the advent of specialized libraries, scrolling animations are becoming increasingly widespread on the web. Web pages may use a variety of scroll movements, including sticky scroll, smooth scroll, CSS parallax, and more. In this article, we’ll be using Locomotive scroll to create a smoother user experience with scroll effects.
What is Locomotive Scroll?
The Locomotive scroll is a scrolling library that is used to create custom scrollers that support touch, keyboard, and mouse interactions. It comes with a variety of customizable features including Locomotive Smooth scroll, Page overlay scroll, and parallax effects.
Why use Locomotive Scroll?
An accessible and fully customized website is one of a developer’s main priorities for users. With the browser's default scroll, just a few scroll effects are possible. However, we can alter how your website scrolls and behaves when you go from page to page thanks to the Locomotive scroll library. Now, let’s walk through how to use Locomotive scroll.
Installation
Let’s get started with scaffolding our Vue application. Run the command below in the terminal.
Once the command above is done installing all of the required files, install the locomotive scroll library using the command below.
Project Setup
Let’s create a basic home page that we’ll customize later with some cool locomotive scroll speed effects. To achieve this, replace all of the code in the App.vue
file with the code below.
In the code above, we’re creating four sections, namely, hello
, sticky
, parallax
, and scroll-into-view
. These will be customized later on in this tutorial.
For the hello
style, we’re adding a background image. Download this image and add it to the src/assets
folder.
With the project setup complete, run the code using yarn dev
to see the results below.
Configuring Locomotive Scroll
Let’s configure the Locomotive Scroll library into our application before looking at its features. Update the <main>
tag in the App.vue
file.
In the code block above, we’re giving the <main>
tag an identifier, a ref
value.
Next, copy and paste the code below into the <script>
tag.
In the code above, we’re calling the setLocomotiveScroll()
function whenever the page is initialized on the browser. The setLocomotiveScroll()
function creates a new LocomotiveScroll
instance that accepts certain properties for its customization.
The el
property initializes its children as a scrollable container.
Finally, replace all of the code in the style.css
file with the code block below.
The code block above is the recommended style when using Locomotive Scroll to prevent page distortion.
Locomotive Scroll Features
A number of features are included in the Locomotive Scroll package to provide your website with a nice UX and UI. We'll examine some of the Locomotive Scroll package's most practical capabilities in this section.
Locomotive Smooth Scroll
Locomotive Smooth Scroll is one of the major Locomotive Scroll effects; this gives the page a sleek and fluid scroll experience. Let’s add this to our page.
In the code block above, we’re setting the smooth property to true.
Locomotive Scroll Speed
With the Locomotive Scroll library, the scrolling speed of a page can be fully customized. This feature is dependent on the smooth scrolling feature.
In the block above, we’re adding the multiplier
property to the LocomotiveScroll
instance. The multiplier boosts or reduces the scrolling speed of the page based on the value provided.
Locomotive Scroll Attributes
Before looking into other Locomotive Scroll features, let’s review Locomotive Scroll attributes.
Locomotive scroll attributes are custom classes that give children tags special locomotive effects.
These include:
data-scroll-container
: This is a required attribute used mostly in the top-level tag of the container to define the scroll container of the application.
data-scroll
: This attribute detects if an element is in view, and is necessary when trying to add an effect to any element.
data-scroll-section
: This attribute defines a scrollable section within your section.
data-scroll-speed
: This sets the speed of the element it's used on.
data-scroll-direction
: This helps in parallax scrolling. It scrolls the element into place from the direction specified.
data-scroll-target
: This attribute targets the element’s location when it scrolls into view.
data-scroll-repeat
: When set as true, this attribute makes all effects repeat their initial phase, thus causing a continuous effect when scrolling over.
Section Overlay Scroll
Creating a nice visual effect where a section scrolls over the previous section before going out of view can easily be achieved with the Locomotive Scroll library. Let’s add this to our application.
In the code block above, we’re initializing all of the sections using the data-scroll-section
attribute to prevent page distortion among the sections. We’re then making the hello
section with the data-scroll
attribute to enable the Locomotive Scroll feature to work on it when its in view. We’re also delaying the scroll-out time of the hello
sections by setting the speed to 2. This combo gives rise to the Scroll-overlay
animation.
Locomotive Scroll Sticky
Pinning an element in its position when scrolling can also be implemented with the Locomotive Scroll library. Let’s implement this by updating the <h1>
tag in the sticky
section.
In the code block above, we added the data-scroll-sticky
attribute to our h1
tag. We also pinpointed the target section that we want the text to stick to, which is the sticky
section.
Locomotive Scroll Parallax
Parallax scrolling means moving elements around a page either horizontally or vertically at different speeds when scrolling. Let’s see how we can achieve this in our Locomotive parallax section.
In the code block above, we added the data-scroll-direction
to both h1
tags and specified the direction we want them to flow. We also increased the speed to make it move faster when scrolling.
Scroll-into-view Classes
Sometimes we want to add some style to an element when it is scrolled into view and remove it when it is out of view. The Locomotive Scroll library makes this feature very easy to achieve; this comes in handy when dealing with certain animations.
In the code block above, we’re adding the hint-text
to the h1
tag making the tag invisible on the initials. When it scrolls into view, we’re adding the animate
class, which contains some animations to fade in the tag. We repeat this process every time the user scrolls to this section.
Building a Simple Landing Page with Locomotive scroll features
Let’s build a Mini project with all of the Locomotive features we’ve discussed. To get started, clone the project starter file from Github.
First, install and configure the Locomotive Scroll library the same way we did in the “Configuring Locomotive Scroll” section. With that done, your App.vue
should look like the code below.
Next, we’ll add smooth Locomotive scrolling and multiplier properties to our Locomotive scroll instance to give it some swift scrolling and speed.
Head over to the Introduction.vue
file in the src/components/
folder and update the template
to the code block below.
Now we’ve registered our section with the Locomotive library and given the h1
text some scroll speed to make it scroll off of the page before it is completely removed.
Next, head over to the Services.vue
file in the scr/components
folder and update the template
to the code below.
We’ve made our h1
text stick to its position on the screen whenever the user scrolls.
Finally, head over to the Appreciation.vue
file also in the src/components
and update the template
to the code below.
In the code block above, we’re adding the animate
class whenever the user scrolls this section into view to give our element a fade-in animation. We’re also giving the h2
tag some parallax scroll effect with speed.
With our achievements so far, run the code using yarn dev
to get the project running.
Conclusion
In this article, we’ve practiced some Locomotive scroll examples. We learned how to configure, use and build with the Locomotive library. We’ve also seen how to combine two or more attributes to build some nice effects. Head over to the official documentation to find out more about the library.
Here is the link to the complete source code on Github.