Table of Contents
Use AOS.js fade animation
index.html
Example
Use AOS.js flip animation
Using AOS.js scaling animation
使用 AOS.js 进行多重设置动画
结论
Home Web Front-end JS Tutorial Add animation on scroll using HTML, CSS and AOS.js

Add animation on scroll using HTML, CSS and AOS.js

Aug 31, 2023 pm 03:49 PM

使用 HTML、CSS 和 AOS.js 在滚动上添加动画

AOS.js (Scroll Animation) is a JavaScript library that provides animations, making it easier just by changing the class name in the desired div tag Add a lot of animations to add animations. While there are different animation JavaScript libraries, AOS.js is probably the simplest of them all.

In this tutorial, we will explore the different types of animations that can be used in AOS.js with the help of different examples.

The first type of animation we will explore is the fade animation. Before doing that, we first need to make sure that aos.css and aos.js are available in our code and that we can get them via a CDN link.

Simply paste the following code snippet at the end of the

tag in your HTML code.
<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
Copy after login

The above code snippet will help to get the css file, in order to get the js file we need to paste the CDN code snippet as shown below at the end of the body tag in the HTML code .

<script src="https://unpkg.com/aos@next/dist/aos.js"></script>
<script>
   AOS.init();
</script>
Copy after login

After adding the above two code snippets to the HTML code, we can use AOS in the code.

Use AOS.js fade animation

The fade-in and fade-out animation simulates the fade-in and fade-out behavior, and a total of 8 different animations can be achieved. these are -

  • fade in and fade out

  • fade in and fade out

  • Fade left

  • Fade right

  • Fade left

  • Fade right

  • Fade left

  • Fade left

Now let’s use them one by one in a simple HTML-CSS example.

The code snippet below is the only part where we will make changes to all the above fade animations.

<div id="main">
   <div data-aos="fade-up">Something up!</div>
</div>
Copy after login

In the above code, we pass the value as "fade-up" to the data-aos attribute, and in all fade cases, only this value will be changed.

index.html

Now, let's consider the following index.html file where we will perform a "fade out" animation.

Example




   
   
   
   AOS - Animation
   <link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
   


   
Something Up!
<script> AOS.init(); </script>
Copy after login

When you run the above code in your browser, you should see a div containing the text "Something Up!" appearing in an upward direction with a fade animation.

Similarly, if we want to perform a fade animation, we can use the one shown in the code snippet below.

<div id="main">
   <div data-aos="fade-down">Something down!</div>
</div>
Copy after login

We only need to replace the div in the index.html file with the above div to get the fade animation.

For Fade to the left, the code snippet looks like this.

<div id="main">
   <div data-aos="fade-left">Something left!</div>
</div>
Copy after login

For the fade, the code snippet is as follows.

<div id="main">
   <div data-aos="fade-right">Something right!</div>
</div>
Copy after login

For Fade-up-left, the code snippet is shown below.

<div id="main">
   <div data-aos="fade-up-left">Something up left!</div>
</div>
Copy after login

For Fade-up-right, the code snippet is shown below.

<div id="main">
   <div data-aos="fade-up-right">Something up right!</div>
</div>
Copy after login

For Fade left, the code snippet looks like this.

<div id="main">
   <div data-aos="fade-down-left">Something down left!</div>
</div>
Copy after login

For Fade-down-right, the code snippet is shown below.

<div id="main">
   <div data-aos="fade-down-right">Something down right!</div>
</div>
Copy after login

At this point, we can conclude that the fade animation in AOS has been completed.

Use AOS.js flip animation

Flip animation simulates flipping behavior, and a total of 4 different animations can be achieved. these are -

  • On the turn

  • Turn down

  • Flip left

  • Turn right

Now let’s use them one by one in a simple HTML-CSS example.

index.html

Let's consider the following index.html file where we will perform a "flip up" animation.




   
   
   
   AOS - Animation
   <link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
   


   
Flip Up!
<script> AOS.init(); </script>
Copy after login

When you run the above code in your browser, you should see a div with the text "Flip Up!" Appears with a flip animation in the upward direction.

Similarly, if we want to do a flip-down animation, we can use the one shown in the code snippet below.

<div id="main">
   <div data-aos="flip-down">Flip down!</div>
</div>
Copy after login

For left flipping, the code snippet is as follows.

<div id="main">
   <div data-aos="flip-left">Flip left!</div>
</div>
Copy after login

For right flipping, the code snippet is as follows.

<div id="main">
   <div data-aos="flip-right">Flip right!</div>
</div>
Copy after login

Using AOS.js scaling animation

Zoom animation simulates zoom behavior, and a total of 8 different animations can be achieved. these are -

  • enlarge
  • enlarge
  • Zoom
  • Zoom left
  • Zoom right
  • Zoom
  • enlarge
  • Zoom
  • Zoom left
  • Zoom right

Now let’s use them one by one in a simple HTML-CSS example.

index.html

Let us consider the following index.html file where we will perform a "zoom in" animation.

Example




   
   
   
   AOS - Animation
   <link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
   


   
Zoom in!
<script> AOS.init(); </script>
Copy after login

When we run the above code in the browser, we should see a div with the Text Zoom pointing upwards in its zoom animation.

Similarly, if we want to perform a zoom-in animation, we can use the one shown in the code snippet below.

<div id="main">
   <div data-aos="zoom-in-up">Zoom in up!</div>
</div>
Copy after login

For zoom, the code snippet is shown below.

<div id="main">
   <div data-aos="zoom-in-down">Zoom in down!</div>
</div>
Copy after login

For Zoom to the left, the code snippet looks like this.

<div id="main">
   <div data-aos="zoom-in-left">Zoom in left!</div>
</div>
Copy after login

For zoom to the right, the code snippet looks like this.

<div id="main">
   <div data-aos="zoom-in-right">Zoom in right!</div>
</div>
Copy after login

同样,如果我们想要缩小动画,我们可以使用下面代码片段中显示的。

<div id="main">
   <div data-aos="zoom-out">Zoom out!</div>
</div>
Copy after login

对于放大,代码片段如下所示。

<div id="main">
   <div data-aos="zoom-out-up">Zoom out up!</div>
</div>
Copy after login

对于缩小,代码片段如下所示。

<div id="main">
   <div data-aos="zoom-out-down">Zoom out down!</div>
</div>
Copy after login

对于向左缩小,代码片段如下所示。

<div id="main">
   <div data-aos="zoom-out-left">Zoom out left!</div>
</div>
Copy after login

对于向右缩小,代码片段如下所示。

<div id="main">
   <div data-aos="zoom-out-right">Zoom out right!</div>
</div>
Copy after login

使用 AOS.js 进行多重设置动画

在上面的所有示例中,我们使用的是单个动画,没有其他选项,但 AOS.js 也允许我们使用带有动画的选项。例如,考虑这样的情况:我们想要一个淡入淡出动画,但持续时间为某个时间。

在下面的代码片段中,我们将创建一个带有持续时间的淡入淡出动画。

<div id="main">
   <div data-aos="fade-down" data-aos-duration="3000"></div>>Fade Down With Duration!</div>
</div>
Copy after login

结论

在本教程中,我们演示了如何借助 AOS.js、HTML 和 CSS 创建滚动动画。

The above is the detailed content of Add animation on scroll using HTML, CSS and AOS.js. 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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles