亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Key Takeaways
How to Install the AOS Library
Getting Started With AOS
Configuring Your Animations With AOS Data Attributes
Exploring the AOS Library’s Inner Workings
More Features
Conclusion
FAQs About on Scroll Animations with the AOS Library
How Do I Install the AOS Library in My Project?
Can I Customize the AOS Animations?
How Do I Use AOS with Vue.js?
How Do I Use AOS with React.js?
Can I Use AOS with Pseudo-Elements?
How Do I Troubleshoot AOS Issues?
Can I Use AOS on Mobile Devices?
How Do I Update the AOS Library?
Can I Use AOS with Other JavaScript Libraries?
How Do I Uninstall the AOS Library?
Home Web Front-end JS Tutorial Cool on Scroll Animations Made Easy With the AOS Library

Cool on Scroll Animations Made Easy With the AOS Library

Feb 16, 2025 pm 12:56 PM

Cool on Scroll Animations Made Easy With the AOS Library

Cool on Scroll Animations Made Easy With the AOS Library

As front-end developer, a popular request you might get from your clients is to implement stunning animation effects on page scroll. There are many libraries to make this task easier for us. AOS, also called Animate on Scroll, is one such library and it does exactly what its name suggests: it lets you apply different kinds of animations to elements as they scroll into view.

Here, you will learn about the inner workings of AOS, how to install the library and get it to work. By the end of this tutorial, building animations on scroll for your clients will be a breeze.

Key Takeaways

  • The Animate on Scroll (AOS) library is a useful tool for front-end developers to create on-scroll animations with ease, offering a range of animation types such as fade, flip, and slide.
  • The AOS library can be installed using Bower or npm, and initialized with a single line of code. Once initialized, animations can be applied by adding specific attributes to HTML elements.
  • AOS allows developers to configure animations using data attributes, such as data-aos-offset, data-aos-duration, and data-aos-easing. It also provides options to trigger animations based on the position of other elements, change the default behavior of animations, and control if animations should be played once or every time an element scrolls into view.
  • The AOS library separates the logic and animation for a cleaner, maintainable workflow. It tracks elements and their positions, dynamically adding or removing the aos-animate class based on provided settings. The library also allows for the creation of custom animations and provides features for disabling animations on certain devices or under specific conditions.

How to Install the AOS Library

You can install AOS using Bower or npm.

Bower:

bower <span>install aos --save</span>

npm:

<span>npm install aos --save</span>

Next, link AOS styles and scripts:

<span><span><span><link</span> rel<span>="stylesheet"</span> href<span>="bower_components/aos/dist/aos.css"</span>></span>
</span><span><span><span><script</span> src<span>="bower_components/aos/dist/aos.js"</span>></span><span><span></script</span>></span></span>

If you prefer, you can download the AOS stylesheet and JavaScript files using a CDN as follows:

The CSS:

<span><span><span><link</span> href<span>="https://cdn.rawgit.com/michalsnik/aos/2.1.1/dist/aos.css"</span> rel<span>="stylesheet"</span>></span></span>

The JavaScript:

<span><span><span><script</span> src<span>="https://cdn.rawgit.com/michalsnik/aos/2.1.1/dist/aos.js"</span>></span><span><span></script</span>></span></span>

That’s it, there are no other dependencies, which helps to keep your website’s performance under control.

To initialize AOS, just write the line below in your JavaScript file.

<span>AOS.init();</span>

Getting Started With AOS

After initializing the library all you have to do is add some specific attributes.

To use basic animations you just need to add data-aos="animation_name" to your HTML elements.

There are several types of animation you can choose from. For example, you can add fade animations like “fade”, “fade-up” and “fade-down-left”. Similarly, you can also add flip and slide animations like “flip-up”, “flip-left”, “slide-down”, and “slide-right”.

Here’s the markup of our first example:

bower <span>install aos --save</span>

Besides the initialization line in the previous section, animating the elements doesn’t require you to do anything else.

Have a look at the code above in action:

See the Pen Animate on Scroll Examples by SitePoint (@SitePoint) on CodePen.

Configuring Your Animations With AOS Data Attributes

Let’s dive into the list of the data attributes you can use to configure your animations.

  • data-aos-offset — You can use this attribute to trigger the animation sooner or later than the designated time. Its default value is 120px.
  • data-aos-duration — You can use this attribute to specify the duration of the animation. The duration value can be anywhere between 50 and 3000 with steps of 50ms. Since the duration is handled in CSS, using smaller steps or a wider range would have unnecessarily increased the size of the CSS code. This range should be sufficient for almost all animations. The default value for this attribute is 400.
  • data-aos-easing — You can use this attribute to control the timing function for animating different elements. Possible values are: linear,ease-in-out and ease-out-quart. You can see a list of all accepted values on the project’s Readme file on GitHub.

Here’s an example using data-aos-duration and data-aos-easing:

See the Pen Animate on Scroll Examples – Attributes by SitePoint (@SitePoint) on CodePen.

More data attributes you can use are:

  • data-aos-anchor — This attribute is useful when you want to trigger the animation based on the position of some other element. It accepts any selector as its value. The default value is null. This means that all the animations will be triggered with respect to the element’s own position.
  • data-aos-anchor-placement — By default, the animations on an element are applied as soon as its top part reaches the bottom part of the window. This behavior can be changed using the data-aos-anchor-placement attribute. As its value, this attribute accepts two words separated by a hyphen. For example, you can set it to top-bottom, top-center or top-top. Three such combinations are also possible for both the center and bottom values.
  • data-aos-once — You can also control if the animations should be played only when you get to a particular element the first time or every time you scroll up or down. By default, the animations are replayed every time the elements scroll into view. You can set the value of this attribute to false in order to animate the elements only once.

Below is an example of using data-aos-anchor-placement:

See the Pen Animate on Scroll Examples – Attributes II by SitePoint (@SitePoint) on CodePen.

Exploring the AOS Library’s Inner Workings

The aim of Animate on Scroll is to handle the logic and the animation separately. For this purpose, the logic is written inside the JavaScript but the animation is written inside the CSS. This separation permits us to write our own animations and modify them based on the needs of the project in a very clean and maintainable workflow.

The library keeps track of all the elements and their positions. This way it can dynamically add or remove the aos-animate class based on the settings that we have provided. For example, the aos-animate class is removed whenever the elements to which it is applied move out of the viewport. However, if an element has the value of data-aos-once set to true, the aos-animate class will not be removed from that particular element, thereby preventing any animation from happening on subsequent scroll events that bring the element into view.

AOS also applies the default value of attributes to the

element on the HTML document. For example, data-aos-easing will be set to ease and data-aos-duration to 400 .

As I have already mentioned, the library applies animation duration only in the range of 50 to 3000 with steps of 50ms. This means that by default, you cannot have an animation duration of 225ms. However, you can add that duration yourself using CSS as follows:

bower <span>install aos --save</span>

Adding your own custom animations to AOS is also quite straightforward.

Just create a data-aos attribute selector and set it to the name of your custom animation.

Next, add the property you want to animate with its initial value, as well as the transition property set to the name of the property you want to animate.

For example, let’s say your animation is called rotate-c and the element to which it is applied is initially rotated by -180 degrees.

Here’s what your CSS should look like:

bower <span>install aos --save</span>

To set the final stage of your animation (in our example this will be the element rotating from -180 degrees to 0 degrees) you add the following CSS rule just below the previous one:

<span>npm install aos --save</span>

Now add data-aos="rotate-c" to your chosen HTML element and this will rotate clockwise (from -180 degrees to 0 degrees) as users scroll that element into view.

Here’s a live demo showing custom rotation animations both clockwise and anti-clockwise using the method above:

See the Pen Animate on Scroll – Custom Animations by SitePoint (@SitePoint) on CodePen.

More Features

The AOS library also provides a lot of other features that make it even more flexible and user friendly. Instead of providing attributes for each element separately, you can pass them as an object to the init() function. Here is an example:

<span><span><span><link</span> rel<span>="stylesheet"</span> href<span>="bower_components/aos/dist/aos.css"</span>></span>
</span><span><span><span><script</span> src<span>="bower_components/aos/dist/aos.js"</span>></span><span><span></script</span>></span></span>

You can also disable the animations on certain devices or under certain conditions using the disable key and setting its value to a device type like mobile, phone or tablet. Alternatively, you can also disable the library using a function.

Here are two examples to illustrate both features:

<span><span><span><link</span> href<span>="https://cdn.rawgit.com/michalsnik/aos/2.1.1/dist/aos.css"</span> rel<span>="stylesheet"</span>></span></span>

In this Pen, when the screen is smaller than 800px, AOS animations are disabled using the function above:

See the Pen Animate on Scroll Examples – Disable Animaions by SitePoint (@SitePoint) on CodePen.

Besides init(), AOS also provides two additional functions: refresh() and refreshHard().

refresh() is used to recalculate all elements’ offsets and positions. It is called automatically on events like window resize.

refreshHard() is called automatically whenever new elements are programmatically removed from or added to the DOM. This way the library can keep the array of AOS elements updated. Once the array has been updated, refreshHard() also triggers the refresh() function to recalculate all the offsets.

Conclusion

This tutorial has introduced you the Animate on Scroll library which you can use to animate elements as you scroll up or down the webpage.

Having no dependencies and letting you create your own custom animations are two features that make AOS a great choice of library for scrolling animations.

If you’re interested in JavaScript animation, you might also like to check out JS with Performance: requestAnimationFrame by Tim Evko.

Have your ever tried AOS in a project? How was your experience? Feel free to share some tips with fellow readers.

FAQs About on Scroll Animations with the AOS Library

How Do I Install the AOS Library in My Project?

To install the AOS library in your project, you need to use npm (Node Package Manager). Open your terminal and navigate to your project directory. Then, type the following command: npm install aos --save. This command will install the AOS library and save it in your project dependencies. After installation, you can import it into your project using import AOS from 'aos'; and initialize it with AOS.init();.

Can I Customize the AOS Animations?

Yes, you can customize the AOS animations. The AOS library provides several data attributes that you can use to customize the animations. For example, you can use data-aos-duration to set the duration of the animation, data-aos-delay to set a delay before the animation starts, and data-aos-offset to set the distance from the top of the page where the animation should start.

How Do I Use AOS with Vue.js?

To use AOS with Vue.js, you need to install the AOS library in your Vue.js project. After installation, you can import it into your Vue.js components and initialize it in the mounted lifecycle hook. You can then use the AOS data attributes in your HTML to apply the animations.

How Do I Use AOS with React.js?

To use AOS with React.js, you need to install the AOS library in your React.js project. After installation, you can import it into your React.js components and initialize it in the componentDidMount lifecycle method. You can then use the AOS data attributes in your JSX to apply the animations.

Can I Use AOS with Pseudo-Elements?

Unfortunately, AOS does not support animations on pseudo-elements. This is because pseudo-elements are not actual DOM elements and cannot be directly manipulated by JavaScript, which AOS uses to apply the animations.

How Do I Troubleshoot AOS Issues?

If you’re having issues with AOS, there are several things you can do. First, make sure you’ve correctly installed and initialized the AOS library. Second, check your HTML for any syntax errors that might be preventing the animations from working. Third, use your browser’s developer tools to inspect the elements and see if the AOS classes are being applied.

Can I Use AOS on Mobile Devices?

Yes, AOS works on mobile devices. However, keep in mind that animations can be resource-intensive and may not perform well on older or lower-end devices. You can use the disable option to disable animations on certain devices if needed.

How Do I Update the AOS Library?

To update the AOS library, you can use npm. Open your terminal, navigate to your project directory, and type the following command: npm update aos. This command will update the AOS library to the latest version.

Can I Use AOS with Other JavaScript Libraries?

Yes, you can use AOS with other JavaScript libraries. However, make sure that the other libraries do not interfere with the AOS animations. If you’re having issues, try disabling the other libraries to see if they’re causing the problem.

How Do I Uninstall the AOS Library?

To uninstall the AOS library, you can use npm. Open your terminal, navigate to your project directory, and type the following command: npm uninstall aos. This command will remove the AOS library from your project.

The above is the detailed content of Cool on Scroll Animations Made Easy With the AOS Library. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How does garbage collection work in JavaScript? How does garbage collection work in JavaScript? Jul 04, 2025 am 12:42 AM

JavaScript's garbage collection mechanism automatically manages memory through a tag-clearing algorithm to reduce the risk of memory leakage. The engine traverses and marks the active object from the root object, and unmarked is treated as garbage and cleared. For example, when the object is no longer referenced (such as setting the variable to null), it will be released in the next round of recycling. Common causes of memory leaks include: ① Uncleared timers or event listeners; ② References to external variables in closures; ③ Global variables continue to hold a large amount of data. The V8 engine optimizes recycling efficiency through strategies such as generational recycling, incremental marking, parallel/concurrent recycling, and reduces the main thread blocking time. During development, unnecessary global references should be avoided and object associations should be promptly decorated to improve performance and stability.

How to make an HTTP request in Node.js? How to make an HTTP request in Node.js? Jul 13, 2025 am 02:18 AM

There are three common ways to initiate HTTP requests in Node.js: use built-in modules, axios, and node-fetch. 1. Use the built-in http/https module without dependencies, which is suitable for basic scenarios, but requires manual processing of data stitching and error monitoring, such as using https.get() to obtain data or send POST requests through .write(); 2.axios is a third-party library based on Promise. It has concise syntax and powerful functions, supports async/await, automatic JSON conversion, interceptor, etc. It is recommended to simplify asynchronous request operations; 3.node-fetch provides a style similar to browser fetch, based on Promise and simple syntax

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

React vs Angular vs Vue: which js framework is best? React vs Angular vs Vue: which js framework is best? Jul 05, 2025 am 02:24 AM

Which JavaScript framework is the best choice? The answer is to choose the most suitable one according to your needs. 1.React is flexible and free, suitable for medium and large projects that require high customization and team architecture capabilities; 2. Angular provides complete solutions, suitable for enterprise-level applications and long-term maintenance; 3. Vue is easy to use, suitable for small and medium-sized projects or rapid development. In addition, whether there is an existing technology stack, team size, project life cycle and whether SSR is needed are also important factors in choosing a framework. In short, there is no absolutely the best framework, the best choice is the one that suits your needs.

JavaScript time object, someone builds an eactexe, faster website on Google Chrome, etc. JavaScript time object, someone builds an eactexe, faster website on Google Chrome, etc. Jul 08, 2025 pm 02:27 PM

Hello, JavaScript developers! Welcome to this week's JavaScript news! This week we will focus on: Oracle's trademark dispute with Deno, new JavaScript time objects are supported by browsers, Google Chrome updates, and some powerful developer tools. Let's get started! Oracle's trademark dispute with Deno Oracle's attempt to register a "JavaScript" trademark has caused controversy. Ryan Dahl, the creator of Node.js and Deno, has filed a petition to cancel the trademark, and he believes that JavaScript is an open standard and should not be used by Oracle

Understanding Immediately Invoked Function Expressions (IIFE) in JavaScript Understanding Immediately Invoked Function Expressions (IIFE) in JavaScript Jul 04, 2025 am 02:42 AM

IIFE (ImmediatelyInvokedFunctionExpression) is a function expression executed immediately after definition, used to isolate variables and avoid contaminating global scope. It is called by wrapping the function in parentheses to make it an expression and a pair of brackets immediately followed by it, such as (function(){/code/})();. Its core uses include: 1. Avoid variable conflicts and prevent duplication of naming between multiple scripts; 2. Create a private scope to make the internal variables invisible; 3. Modular code to facilitate initialization without exposing too many variables. Common writing methods include versions passed with parameters and versions of ES6 arrow function, but note that expressions and ties must be used.

Handling Promises: Chaining, Error Handling, and Promise Combinators in JavaScript Handling Promises: Chaining, Error Handling, and Promise Combinators in JavaScript Jul 08, 2025 am 02:40 AM

Promise is the core mechanism for handling asynchronous operations in JavaScript. Understanding chain calls, error handling and combiners is the key to mastering their applications. 1. The chain call returns a new Promise through .then() to realize asynchronous process concatenation. Each .then() receives the previous result and can return a value or a Promise; 2. Error handling should use .catch() to catch exceptions to avoid silent failures, and can return the default value in catch to continue the process; 3. Combinators such as Promise.all() (successfully successful only after all success), Promise.race() (the first completion is returned) and Promise.allSettled() (waiting for all completions)

What is the cache API and how is it used with Service Workers? What is the cache API and how is it used with Service Workers? Jul 08, 2025 am 02:43 AM

CacheAPI is a tool provided by the browser to cache network requests, which is often used in conjunction with ServiceWorker to improve website performance and offline experience. 1. It allows developers to manually store resources such as scripts, style sheets, pictures, etc.; 2. It can match cache responses according to requests; 3. It supports deleting specific caches or clearing the entire cache; 4. It can implement cache priority or network priority strategies through ServiceWorker listening to fetch events; 5. It is often used for offline support, speed up repeated access speed, preloading key resources and background update content; 6. When using it, you need to pay attention to cache version control, storage restrictions and the difference from HTTP caching mechanism.

See all articles