aos README

The project's README from github.com/michalsnik/aos. As one markdown file: README.md.

AOS - Animate on scroll library

NPM version NPM downloads Build Status Gitter

Twitter Follow Twitter URL

:exclamation::exclamation::exclamation: This is README for aos@next :exclamation::exclamation::exclamation:

For last stable release (v2) go here


🚀 Demo

🌟 Codepen Examples

👉 To get a better understanding how this actually works, I encourage you to check my post on CSS-tricks.


⚙ Installation

Basic

Add styles in <head>:

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

Add script right before closing </body> tag, and initialize AOS:

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

Using package managers

Install aos package:

Import script, styles and initialize AOS:

import AOS from 'aos';
import 'aos/dist/aos.css'; // You can also use <link> for styles
// ..
AOS.init();

In order to make it work you'll have to make sure your build process has configured styles loader, and bundles it all correctly. If you're using Parcel however, it will work out of the box as provided.


🤔 How to use it?

1. Initialize AOS:

AOS.init();

// You can also pass an optional settings object
// below listed default settings
AOS.init({
  // Global settings:
  disable: false, // accepts following values: 'phone', 'tablet', 'mobile', boolean, expression or function
  startEvent: 'DOMContentLoaded', // name of the event dispatched on the document, that AOS should initialize on
  initClassName: 'aos-init', // class applied after initialization
  animatedClassName: 'aos-animate', // class applied on animation
  useClassNames: false, // if true, will add content of `data-aos` as classes on scroll
  disableMutationObserver: false, // disables automatic mutations' detections (advanced)
  debounceDelay: 50, // the delay on debounce used while resizing window (advanced)
  throttleDelay: 99, // the delay on throttle used while scrolling the page (advanced)
  

  // Settings that can be overridden on per-element basis, by `data-aos-*` attributes:
  offset: 120, // offset (in px) from the original trigger point
  delay: 0, // values from 0 to 3000, with step 50ms
  duration: 400, // values from 0 to 3000, with step 50ms
  easing: 'ease', // default easing for AOS animations
  once: false, // whether animation should happen only once - while scrolling down
  mirror: false, // whether elements should animate out while scrolling past them
  anchorPlacement: 'top-bottom', // defines which position of the element regarding to window should trigger the animation

});

2. Set animation using data-aos attribute:

  <div data-aos="fade-in"></div>

And adjust behaviour by using data-aos-* attributes:

  <div
    data-aos="fade-up"
    data-aos-offset="200"
    data-aos-delay="50"
    data-aos-duration="1000"
    data-aos-easing="ease-in-out"
    data-aos-mirror="true"
    data-aos-once="false"
    data-aos-anchor-placement="top-center"
  >
  </div>

See full list of all animations, easings and anchor placements

Anchor

There is also a setting that can be used only on per-element basis:

Examples:

<div data-aos="fade-up" data-aos-anchor=".other-element"></div>

This way you can trigger animation on one element, while you scroll to another - useful in animating fixed elements.


API

AOS object is exposed as a global variable, for now there are three methods available:

Example execution:

  AOS.refresh();

By default AOS is watching for DOM changes and if there are any new elements loaded asynchronously or when something is removed from DOM it calls refreshHard automatically. In browsers that don't support MutationObserver like IE you might need to call AOS.refreshHard() by yourself.

refresh method is called on window resize and so on, as it doesn't require to build new store with AOS elements and should be as light as possible.


JS Events

AOS dispatches two events on document: aos:in and aos:out whenever any element animates in or out, so that you can do extra stuff in JS:

document.addEventListener('aos:in', ({ detail }) => {
  console.log('animated in', detail);
});

document.addEventListener('aos:out', ({ detail }) => {
  console.log('animated out', detail);
});

You can also tell AOS to trigger custom event on specific element, by setting data-aos-id attribute:

<div data-aos="fade-in" data-aos-id="super-duper"></div>

Then you'll be able to listen for two custom events then:


Recipes:

Adding custom animations:

Sometimes built-in animations are just not enough. Let's say you need one box to have different animation depending on resolution. Here's how you could do it:

[data-aos="new-animation"] {
  opacity: 0;
  transition-property: transform, opacity;

  &.aos-animate {
    opacity: 1;
  }

  @media screen and (min-width: 768px) {
    transform: translateX(100px);

    &.aos-animate {
      transform: translateX(0);
    }
  }
}

Then use it in HTML:

<div data-aos="new-animation"></div>

The element will only animate opacity on mobile devices, but from 768px width it'll also slide from right to left.

Adding custom easing:

Similar to animations you can add custom easings:

[data-aos] {
  body[data-aos-easing="new-easing"] &,
  &[data-aos][data-aos-easing="new-easing"] {
    transition-timing-function: cubic-bezier(.250, .250, .750, .750);
  }
}

Customizing default animations distance

Default distance for built-in animations is 100px. As long as you're using SCSS though, you can override it:

$aos-distance: 200px; // It has to be above import
@import 'node_modules/aos/src/sass/aos.scss';

You have to however configure your build process to allow it to import styles from node_modules beforehand.

Integrating external CSS animation library (e.g. Animate.css):

Use animatedClassName to change default behaviour of AOS, to apply class names placed inside data-aos on scroll.

<div data-aos="fadeInUp"></div>
AOS.init({
  useClassNames: true,
  initClassName: false,
  animatedClassName: 'animated',
});

The above element will get two classes: animated and fadeInUp. Using different combinations of the three above settings, you should be able to integrate any external CSS animation library.

External libraries however don't care too much about animation state before the actual animation. So if you want those elements to be not visible before scrolling, you might need to add similar styles:

[data-aos] {
  visibility: hidden;
}
[data-aos].animated {
  visibility: visible;
}

Caveats:

setting: duration, delay

Duration and delay accept values from 50 to 3000, with step 50ms, it's because those are handled by css, and to not make css longer than it is already I implemented only a subset. I believe those should cover most cases.

If not, you can write simple CSS that will add another duration, for example:

  body[data-aos-duration='4000'] [data-aos],
  [data-aos][data-aos][data-aos-duration='4000'] {
    transition-duration: 4000ms;
  }

This code will add 4000ms duration available for you to set on AOS elements, or to set as global duration while initializing AOS script. Notice that double [data-aos][data-aos] - it's not a mistake, it is a trick, to make individual settings more important than global, without need to write ugly "!important" there :)

Example usage:

  <div data-aos="fade-in" data-aos-duration="4000"></div>

Predefined options

Animations

Anchor placements:

Easing functions:


❔Questions

If you found a bug, have a question or an idea, please check AOS contribution guide and don't hesitate to create new issues.

aos (michalsnik/aos)

Searching Database…

28.1K ·GitHub
这是什么?AOS(Animate On Scroll,滚动时动画)是一个 JavaScript 库,可以让元素在滚动进入视野时播放 CSS 动画,只需通过简单的 HTML data 属性即可触发。
它是框架还是普通库?它是一个轻量的原生 JavaScript 库加一个 CSS 文件,不依赖任何框架;它提供一个全局的 AOS 对象,其中包含 init 和 refresh 方法。
怎样把它添加到项目中?在 head 中引入 AOS 的样式表,在闭合的 body 标签之前引入脚本,然后调用 AOS.init();也可以使用 yarn add aos@next 或 npm install --save aos@next 进行安装,再导入脚本和样式。
使用它需要构建工具吗?不需要,你可以直接引入 CSS 和 JS 文件;只有通过包管理器安装时,才需要配置打包工具以正确加载样式。
设置 duration 或 delay 时有什么需要注意的地方吗?duration 和 delay 只接受 50 到 3000 之间、以 50 毫秒为步长的值,因为它们是通过生成的 CSS 类来实现的;如果需要超出该范围的值,就需要额外编写自定义 CSS。
它能在 IE 等旧版浏览器中运行吗?基本可以,但 IE 不支持 MutationObserver,因此在这类浏览器中,当 DOM 发生变化时,你可能需要手动调用 AOS.refreshHard(),而不能依赖自动检测。
内置了哪些动画?AOS 自带 fade、flip、slide 和 zoom 四类动画,每类都有多个方向的变体,例如 fade-up、flip-left、slide-right、zoom-in。
可以设置动画从哪里开始触发吗?可以,通过 top-bottom、center-center 等锚点位置设置,以及 data-aos-anchor 属性,让某个元素的动画根据另一个元素的滚动偏移量来触发。
可以使用自定义动画或缓动函数吗?可以,你可以编写自定义的 CSS 类作为动画和缓动效果,然后通过常用的 data-aos 属性引用它们,具体示例见 README 中的 recipes。
可以与 Animate.css 之类的外部动画库集成吗?可以,使用 animatedClassName 选项,就能让 AOS 在滚动时应用 Animate.css 等库中的类名,而不再使用它内置的动画类。
Creator
michalsnik
Size
1.4 MB
Stars
28.1K
Updated
CommitOct 03
Age
11y
Commits · 90 Days