{"id":242456,"date":"2016-06-06T06:06:14","date_gmt":"2016-06-06T13:06:14","guid":{"rendered":"http:\/\/css-tricks.com\/?p=242456"},"modified":"2017-04-10T17:48:11","modified_gmt":"2017-04-11T00:48:11","slug":"aos-css-driven-scroll-animation-library","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/aos-css-driven-scroll-animation-library\/","title":{"rendered":"AOS: CSS-Driven “On Scroll” Animation Library"},"content":{"rendered":"
The following is a guest post by Micha\u0142 Sajn\u00f3g<\/a>, a front end developer at Netguru<\/a>. Micha\u0142 has created one of those “when you scroll to here, trigger this animation” libraries. One of the things I like about it is that it leaves as much as it can to CSS for creating and controlling the animation themselves. Not to mention it’s proved itself by working well on a number of production sites. I’ll let Micha\u0142 walk you through it.<\/em><\/p>\n <\/p>\n Have you ever seen those long web pages where different animations are being applied as you scroll down? I\u2019d like to share with you a plugin I wrote that makes it really easy to handle all kinds of animations with full CSS control and no pain.<\/p>\n If you’d like to get right into it, the code is on GitHub.<\/a><\/p>\n In my previous company we were using WOW.js<\/a> (or other similar libraries) to animate elements on scroll. For simple projects it was quite nice, but on larger sites we wanted to have more control over what\u2019s actually happening. In all of popular libraries, animations were completely handled by JavaScript by inserting inline CSS. Arghgh! Inline styles are evil. They are hard to control and override. Though, in some cases it\u2019s ok to set them using JavaScript, it\u2019s still much cleaner to just leave them where they belong and handle all CSS related things inside CSS file.<\/p>\n I decided to create a library that has a pure goal – detect position of elements and then add appropriate classes when they appear in viewport.<\/p>\n I wanted to split the responsibilities with my new library:<\/p>\n This allows you to add your own animations easily, and do things like change them according to the viewport.<\/p>\n The idea behind AOS is straightforward: watch all elements and their positions based on settings you provide them. Then add\/remove the class The are lots of different animations ready to use out of the box, but creating new ones is simple also. Here’s an example:<\/p>\n You don\u2019t have to worry about duration or delay. In the CSS, you only: <\/p>\n Things like duration\/delay\/easing are set independently of the animation.<\/p>\n or with a different transition duration:<\/p>\n **Tip:** You can use With different animations:<\/p>\n See the Pen AOS – animations<\/a> by Snik (@michalsnik<\/a>) on CodePen<\/a>.<\/p>\n With anchor setting in use:<\/p>\n See the Pen AOS – anchor<\/a> by Snik (@michalsnik<\/a>) on CodePen<\/a>.<\/p>\n With anchor placement and different easing:<\/p>\n See the Pen AOS – anchor & anchor-placement<\/a> by Snik (@michalsnik<\/a>) on CodePen<\/a>.<\/p>\n With simple custom animations:<\/p>\n See the Pen AOS – custom animations<\/a> by Snik (@michalsnik<\/a>) on CodePen<\/a>.<\/p>\nThe Problem With Other Libraries<\/h3>\n
Controlling Animations Entirely in CSS<\/h3>\n
\n
How AOS Works<\/h3>\n
aos-animate<\/code>. Of course, in practice, it\u2019s not always that easy, but the idea behind AOS is as simple as that. Every aspect of animation is handled by CSS.<\/p>\n
Example Animations in CSS<\/h3>\n
[aos=\"fade\"] {\r\n opacity: 0;\r\n transition-property: opacity;\r\n}\r\n\r\n[aos=\"fade\"].aos-animate {\r\n opacity: 1;\r\n}<\/code><\/pre>\n
\n
aos<\/code> with the name of your animation<\/li>\n
transition-property<\/code> (by default this is
all<\/code>, so it\u2019s more performant and more predictable if you narrow the transition to the intended properties)<\/li>\n
.aos-animate<\/code><\/li>\n<\/ul>\n
Example HTML<\/h3>\n
<div class=\"some-item\" aos=\"fade\">Example Element<\/div><\/code><\/pre>\n
<div class=\"some-item\" aos=\"fade\" aos-duration=\"500\">Example Element<\/div><\/code><\/pre>\n
data-aos<\/code> instead of
aos<\/code> to make your HTML validate properly.<\/p>\n
Live Demos<\/h3>\n
Additional Features<\/h3>\n
\n
mobile<\/code>,
phone<\/code>,
tablet<\/code> or with a custom condition\/function<\/li>\n