How to Use the Animate on Scroll(AoS) Library in Vue
Animations are used to attract people’s attention and add interactivity to your web application rather than just a static page. With animations, users tend to engage more with the features of your application. One of the best animations to add to your webpage is the on-scroll animation. An on-scroll animation is a form of Scrollytelling, which means telling a story or displaying content while scrolling.
On-scroll animation can be complicated to implement on a website without the use of certain scroll animation libraries. These libraries provide a simple interface for creating different interactions on the scroll and improving the user experience. With the Animate on scroll(AOS) library, you can add animations smoothly, and it's easy to configure.
This tutorial will teach you about the Animate On Scroll (AOS) Library, how to integrate it into your Vue applications, and the different use cases for it. We will also build a landing page in Vue that uses the AOS Library for animations.
Prerequisites
This tutorial assumes the reader has the following:
- Node installed on their local development machine. You can verify if you do by running the command below in your terminal.
- A basic knowledge of HTML, CSS, JavaScript, and Vue
Introduction to Animate On Scroll Library(AOS)
Animate On Scroll Library (AOS) is a library developed by Michał Sajnóg. The library's main goal is to detect the position of elements and then add appropriate classes for animation when they appear in the viewport.
Animate On Scroll Library provides an ease to let you apply different kinds of animations to elements as they scroll into view. The library was built with all types of users in mind to make it easy to configure for accessibility.
AOS is an open-source library with over 16k+ stars on Github
Integrating Animate on scroll(AOS) library in a Vue app.
Installation
To set up the AOS library in your Vue app is quite easy and can be done in different ways, such as:
Install using Yarn
Install using npm
Install with CDN
CSS
Javascript
Importing
Import AOS into the main.js
file
Initialize AOS into your vue app with the use of the mounted
lifecycle method.
Animation Options
After the setup is complete all you have to do is add some specific attributes, data-aos="animation_name"
to your HTML elements in your template for the animations. The AOS Library has some predefined options for the animations such as :
- Fade animations
- Flip animations
- Slide animations
- Zoom animations
Here is the full list of the different animations that can be used.
For any animation you choose to use, it can simply be added to the HTML elements in your templates,
Configuring Animations With AOS Data Attributes.
One unique feature about the AOS library is the ability to configure the animation using AOS Data Attributes, with these you can control your animations to your preference. The different attributes are the following:
data-aos-duration
For every animation, there is always a predefined duration for it to last for, AOS provides a default duration of 400ms. To use this attribute, your values must between 0 to 3000, with step 50ms.
data-aos-delay
The amount of time in which you want your animation to be delayed can be set using this attribute. AOS provides a default duration of 50ms. To use this attribute, your values must between 0 to 3000, with step 50ms.
data-aos-offset
You can use this attribute to trigger the animation earlier or later than the designated time. AOS provides a default value of 120px.
data-aos-easing
With the use of this AOS attribute, you can control the timing function which specifies the speed curve of the animation, it enables you to control and vary the acceleration of an animation that is it defines where the animation speeds up and slows down over the specified duration. The attribute uses the same values for your CSS animations such as linear, ease, ease-in, ease-out, ease-in-out. Here is the full list of possible values.
data-aos-anchor-placement
This attribute gives you control over the position in which the animation starts. By default, AOS sets the start position as soon as its top part reaches the bottom part of the window. The possible values to use for this attribute are top-bottom, top-center, top-top, center-bottom, center-center. Here is the full list of possible values.
data-aos-once
Animations in the AOS library by default are always replayed each time it's scrolled into view or out of view. With this attribute, you can set the value to false in other to animate the element only once.
Another alternative is to set your preferred values needed globally for all the elements you want to animate. This can be done by passing them as an object to the init() .
The AOS library also provides a lot of other features that make it even more flexible and user-friendly.
Adding custom animations:
In cases where the built-in animations are just not enough, The AOS library permits us to write our own animations and modify them based on the needs of the project in a very clean and maintainable workflow. This is quite easy to implement; In the style tag of the component
Then used, in the templates of the component
Similar to animations other css properties can also be created.
Adding custom transitions:
In the style tag of the component
Then used, in the templates of the component
This gives the component a new transition duration.
Tutorial Demo: Building a landing page with Vue using the AOS library
Yay! In this section, we will build a basic landing page in Vue, and animate the landing page using the AOS library.
Check out the demo on Codepen and the code repository on GitHub.
For an easier understanding of this section, we will go through each part step by step.
Create a Vue Project
First, let’s start up a Vue application by using installing the vue CLI:
First, install the Vue command-line interface (CLI):
Then, create the Vue app using this command:
With these steps, our vue application has been successfully created, switch to its repository using cd landing-Page-vue, and run the command below to start the application.
Add content to our Vue app
Open the Vue app in your favorite integrated development environment (IDE).
A Vue single file components allow us to define the HTML/CSS and JS of a component all within a single .vue file. The formal structure contains
<template>
,<script>
,<style>
In the App.vue
, paste the code below to in the template tag.
The code above is a simple HTML markup that we will use to animate our landing page.
Ready to pick up the pace?
Enter your email and receive regular updates on our latest articles and courses
We're here to help.
Sign up for our FREE email course
- ⭐️ Portfolio Building: Learn how to build a badass developer portfolio so you can land that next job
Styling the landing page
For the styling, we will use CSS to style each of the sections of the page. Paste the code below in the style tag.
Animate the page using Animate On Scroll (AOS) Library.
To add the AOS to our project, we first install it using the command below;
Then next, we initialize it in the main.js
file.
After setting up AOS into the Vue app successfully, we then animate the page by the AOS attribute needed.
Paste the updated code below into your template tags.
With the updated changes, our landing page’s animations will only start animating when we scroll into the view of the section.
See demo here.
Conclusion
This article has introduced you to the Animate on Scroll library which is used to animate elements as you scroll up or down the webpage. Also, we were able to integrate it in our Vue js application and also build an animated landing page in Vue using AOS Library.
Ready to pick up the pace?
Enter your email and receive regular updates on our latest articles and courses
We're here to help.
Sign up for our FREE email course
- ⭐️ Portfolio Building: Learn how to build a badass developer portfolio so you can land that next job