Using Locomotive Scroll in Vue for Smoother UX
Tags: vue, scroll
Published: 2023-01-13 15:31:17

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/locomotivescroll_0dec536d14e893ef22ed964eca67a495.jpg" alt="Using locomotive scroll in Vue for smoother UX."/></figure>

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](https://github.com/locomotivemtl/locomotive-scroll#readme) 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](https://code.pieces.app/blog/improve-website-accessibility) 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.

```
yarn create vite my-vue-app vue-locomotive-scroll
```

Once the command above is done installing all of the required files, install the[ locomotive scroll](https://github.com/locomotivemtl/locomotive-scroll) library using the command below.

```
cd vue-locomotive-scroll && yarn add locomotive-scroll
```

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

```
<template>
  <main>
    <section className="hello">
      <h1>Hello World From Sam</h1>
    </section>
    <section id="sticky">
      <h1>Sticky scroll example</h1>
      <p>Placeholders Placeholders</p>
      <p>Placeholders Placeholders</p>
      <p>Placeholders Placeholders</p>
      <p>Placeholders Placeholders</p>
    </section>
    <section className="parallax">
      <h1>Vertical Parallax scroll</h1>
      <h1>Horizontal Parallax Scroll</h1>
    </section>
    <section className="scroll-into-view">
      <h1>
        Here, we're calling the Animate class when the Content scrolls into view
      </h1>
    </section>
  </main>
</template>

<script>

</script>

<style>
section {
 height: 100vh;
 display: flex;
 align-items: center;
 justify-content: center;
 color: white;
 font-size: xx-large;
}
.hello {
 background-color: red;
 background-image: url("./background.png");
 color: rgba(0, 0, 0, 0.745);
}
.parallax {
 background-color: greenyellow;
}
#sticky {
 background-color: rgba(137, 43, 226, 0.469);
 padding: 50px;
}
.scroll-into-view {
 background-color: black;
 color: white;
}
</style>
```

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](https://res.cloudinary.com/iamvictorsam/image/upload/v1665509403/background_qtdtz5.png) _and add it to the_ `src/assets` _folder._

With the project setup complete, run the code using `yarn dev` to see the results below.

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/gif1-small_3b8f59d4d6a4a9c732d62f7827e4574b.gif" alt="A beginning demo of Locomotive Scroll."/></figure>

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

```
<template>
  <main ref="container">

       <!-- other code blocks here -->

  </main>
</template>
```

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.

```
<script>
import LocomotiveScroll from "locomotive-scroll";

export default {
  methods: {
    setLocomotiveScroll() {
 new LocomotiveScroll({
        el: this.$refs.container,
      });
    },
  },
  mounted() {
 this.setLocomotiveScroll();
  },
};
</script>
```

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.

```
* {
 margin: 0;
 padding: 0;
 box-sizing: border-box;
}
html.has-scroll-smooth {
 overflow: hidden;
}
html.has-scroll-dragging {
 -webkit-user-select: none;
 -moz-user-select: none;
 -ms-user-select: none;
 user-select: none;
}
.has-scroll-smooth body {
 overflow: hidden;
}
.has-scroll-smooth [data-scroll-container] {
 min-height: 100vh;
}
[data-scroll-direction="horizontal"] [data-scroll-container] {
 height: 100vh;
 display: inline-block;
 white-space: nowrap;
}
[data-scroll-direction="horizontal"] [data-scroll-section] {
 display: inline-block;
 vertical-align: top;
 white-space: nowrap;
 height: 100%;
}
.c-scrollbar {
 position: absolute;
 right: 0;
 top: 0;
 width: 11px;
 height: 100%;
 transform-origin: center right;
 transition: transform 0.3s, opacity 0.3s;
 opacity: 0;
}
.c-scrollbar:hover {
 transform: scaleX(1.45);
}
.c-scrollbar:hover,
.has-scroll-scrolling .c-scrollbar,
.has-scroll-dragging .c-scrollbar {
 opacity: 1;
}
[data-scroll-direction="horizontal"] .c-scrollbar {
 width: 100%;
 height: 10px;
 top: auto;
 bottom: 0;
 transform: scaleY(1);
}
[data-scroll-direction="horizontal"] .c-scrollbar:hover {
 transform: scaleY(1.3);
}
.c-scrollbar_thumb {
 position: absolute;
 top: 0;
 right: 0;
 background-color: black;
 opacity: 0.5;
 width: 7px;
 border-radius: 10px;
 margin: 2px;
 cursor: -webkit-grab;
 cursor: grab;
}
.has-scroll-dragging .c-scrollbar_thumb {
 cursor: -webkit-grabbing;
 cursor: grabbing;
}
[data-scroll-direction="horizontal"] .c-scrollbar_thumb {
 right: auto;
 bottom: 0;
}
```

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.

```
export default {
  methods: {
    setLocomotiveScroll() {
 new LocomotiveScroll({
        el: this.$refs.container,
        smooth: true,           //added this
      });
    },
  },
  mounted() {
 this.setLocomotiveScroll();
  },
};
```

In the code block above, we’re setting the smooth property to true.

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/gif2-small_09e7265874e0c24288060a33dea0c03b.gif" alt="Using Smooth scroll."/></figure>

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

```
export default {
  methods: {
    setLocomotiveScroll() {
 new LocomotiveScroll({
        el: this.$refs.container,
        smooth: true,
        multiplier: 5,        //add this
      });
    },
  },
  mounted() {
 this.setLocomotiveScroll();
  },
};
```

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.

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/gif3-small_287dc7e4f8e235f44cfbb61ec4b7f61d.gif" alt="Using speed scroll."/></figure>

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

```
<template>  
  <main ref="container" data-scroll-container>
    <section
      className="hello"
      data-scroll
      data-scroll-speed="2"
      data-scroll-section
    >
      <h1>Hello World From Sam</h1>
    </section>

    <section id="sticky" data-scroll-section>
      <h1>Sticky scroll example</h1>
      <p>Placeholders Placeholders</p>
      <p>Placeholders Placeholders</p>
      <p>Placeholders Placeholders</p>
      <p>Placeholders Placeholders</p>
    </section>

    <section className="parallax" data-scroll-section>
      <h1>Vertical Parallax scroll</h1>
      <h1>Horizontal Parallax Scroll</h1>
    </section>

    <section className="scroll-into-view" data-scroll-section>
      <h1>
        Here, we're calling the Animate class when the Content scrolls into view
      </h1>
    </section>
  </main>
</template>
```

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.

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/gif4-small_fcdfd67f6a5d1424a3e5e4abfb520b1b.gif" alt="Using section overlay scroll."/></figure>

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

```
<template>
  <main ref="container">
    ...     <!-- other code blocks here -->

    <section id="sticky" data-scroll-section>
      <h1
        data-scroll
        data-scroll-sticky
        data-scroll-target="#sticky"
      >
        Sticky scroll example
      </h1>
      <p>Placeholders Placeholders</p>
      <p>Placeholders Placeholders</p>
      <p>Placeholders Placeholders</p>
      <p>Placeholders Placeholders</p>
    </section>

    ....     <!-- other code blocks here -->
  </main>
```

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.

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/gif5-small_292e44c454d6c02164adf1c04ddfe171.gif" alt="Using sticky scroll."/></figure>

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

```
<template>
  <main ref="container">
    ....     <!-- other code blocks here -->

    <section className="parallax" data-scroll-section>
      <h1 data-scroll data-scroll-direction="vertical" data-scroll-speed="9">
        Vertical Parallax scroll
      </h1>
      <h1 data-scroll data-scroll-direction="horizontal" data-scroll-speed="9">
        Horizontal Parallax Scroll
      </h1>
    </section>
 
    ....     <!-- other code blocks here -->
  </main>
</template>
```

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.

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/gif6-small_8e65028953d15a133389d40964b70dcb.gif" alt="Using Locomotive scroll parallax."/></figure>

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

```
<template>
  <main>
    ....     <!-- other code blocks here -->

    <section className="scroll-into-view" data-scroll-section>
      <h1
        className="hint-text"
        data-scroll
        data-scroll-repeat="true"
        data-scroll-class="animate"
        data-scroll-speed="5"
      >
        Here, we're calling the Animate class when the Content scrolls into view
      </h1>
    </section>
  </main>
</template>
```

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.

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/gif7-small_e13a452ed156a49b21dbcff33356af2b.gif" alt="Using scroll-into-view classes."/></figure>

## 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](https://github.com/iamVictorSam/landing-page-with-locomotive-scroll/tree/starter).

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/gif8-actuallysmall_18754bfb4e31c51ba17c04f6b53b8185.gif" alt="Our final landing page project."/></figure>

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.

```
<template>
  <main ref="container" data-scroll-container>
    <Introduction />
    <Services />
    <Appreciation />
  </main>
</template>
<script>

import Introduction from "./components/Introduction.vue";
import Services from "./components/Services.vue";
import Appreciation from "./components/Appreciation.vue";
import LocomotiveScroll from "locomotive-scroll";

export default {
  components: {
    Introduction,
    Services,
    Appreciation,
  },

  methods: {
    setLocomotiveScroll() {
 new LocomotiveScroll({
        el: this.$refs.container,
      });
    },
  },

  mounted() {
 this.setLocomotiveScroll();
  },
};
</script>
```

Next, we’ll add smooth Locomotive scrolling and multiplier properties to our Locomotive scroll instance to give it some swift scrolling and speed.

```
setLocomotiveScroll() {
 new LocomotiveScroll({
        el: this.$refs.container,
        smooth: true,
        multiplier: 3,
 });
},
```

Head over to the `Introduction.vue` file in the `src/components/` folder and update the `template` to the code block below.

```
<template>
  <section data-scroll data-scroll-speed="2" data-scroll-section>
    <h1 data-scroll-speed="2" data-scroll>
      Welcome to Jexxi Code
      <br />
      Lorem ipsum, dolor sit amet consectetur adipisicing elit. Nostrum tenetur
      magnam natus repellat quod dolorem culpa excepturi doloremque, autem.
    </h1>
    <div>
      <img src="../assets/man-thinking.png" />
    </div>
  </section>
</template>
```

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.

```
<template>
  <section id="sticky" data-scroll-section>
    <div class="service-text">
      <h1
        data-scroll
        data-scroll-sticky
        data-scroll-target="#sticky"
        data-scroll-speed="9"
      >
        Services we Offer
      </h1>
    </div>
    <div>
      <ServiceText />
      <ServiceText />
      <ServiceText />
    </div>
  </section>
</template>
```

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.

```
<template>
  <section data-scroll-section>
    <h1
      className="appreciation-text"
      data-scroll
      data-scroll-repeat="true"
      data-scroll-class="animate"
      data-scroll-speed="5"
    >
      Thank You For Using The Locomotive Scroll Library
    </h1>
    <br />
    <h2 data-scroll data-scroll-direction="horizontal" data-scroll-speed="9">
      - Sam Victor
    </h2>
  </section>
</template>
```

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.

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/gif9-small_0e96155c6e04b2817d5b2a8e752f4a70.gif" alt="Combining the features of Locomotive Scroll to create a landing page."/></figure>

## 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](https://github.com/locomotivemtl/locomotive-scroll) to find out more about the library.

Here is the link to the[ complete source](https://github.com/iamVictorSam/landing-page-with-locomotive-scroll/tree/final) code on Github.