<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nick Ciliak</title>
    <description>The latest articles on DEV Community by Nick Ciliak (@nickcil).</description>
    <link>https://dev.to/nickcil</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F476670%2F857cc3cd-280b-49be-afd8-e2bb22834822.jpg</url>
      <title>DEV Community: Nick Ciliak</title>
      <link>https://dev.to/nickcil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nickcil"/>
    <language>en</language>
    <item>
      <title>How to create scroll animations without a library</title>
      <dc:creator>Nick Ciliak</dc:creator>
      <pubDate>Sat, 20 Feb 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/nickcil/how-to-create-scroll-animations-without-a-library-102b</link>
      <guid>https://dev.to/nickcil/how-to-create-scroll-animations-without-a-library-102b</guid>
      <description>&lt;p&gt;There are tons of scroll animation libraries out there (I've even written a &lt;a href="https://coolcssanimation.com/the-best-scroll-based-animation-libraries/"&gt;guide to the best ones&lt;/a&gt;) so it might seem like you have to use one if you want to implement scroll animations. But is it possible to create scroll-based animations without using a third-party JavaScript library?&lt;/p&gt;

&lt;p&gt;Yes: adding scroll-based animations to your page is totally possible without using a library.&lt;/p&gt;

&lt;p&gt;By using a library, you can often save time when implementing your animations. You can also take advantage of performance optimizations that are built into the library. However, when you create them from scratch, you have unlimited flexibility to create the animations exactly the way you want them. Plus, when you build from scratch, you won't be including any unnecessary code that you might be if you used a library.&lt;/p&gt;

&lt;p&gt;If you are implementing a page with a lot of scroll animations, you want to use a library. You'll not only save time, but you'll be able to reuse a lot more code and benefit from the performance optimizations a library can provide.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to implement scroll animations from scratch
&lt;/h2&gt;

&lt;p&gt;If you only need to add a few animations, you're more likely to be a contender to build from scratch, without using a library.&lt;/p&gt;

&lt;p&gt;If page speed is a big concern for your project, it might be better to build from scratch, because using a scroll animation library will add additional size to your page. However, there are plenty of small libraries out there, and if you end up reimplementing most of the features that the library provides anyway, you should consider just using the library. If you can't find a small enough library that has only the exact features that you need, then you could just build it from scratch.&lt;/p&gt;

&lt;p&gt;Additionally, any time you use a third-party JavaScript library, you add another dependency to your project that is likely not fully under your control. This can be an issue as your project ages: if the package becomes unsupported and bug-ridden in the future, it can cause your project to break as well. If something like this is a concern for your project, you might be better off implementing your animation from scratch.&lt;/p&gt;

&lt;p&gt;Here are a few examples of how to implement scroll animations without using a library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scroll-triggered animations without a library
&lt;/h2&gt;

&lt;p&gt;If you want an element to animate in some way when it enters or exits the viewport, you can use Intersection Observer to add or remove a class with a CSS animation or transition.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create the observer&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;IntersectionObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;entries&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isIntersecting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Add the CSS class that has your animation&lt;/span&gt;
      &lt;span class="nx"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fade-in&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fade-in&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Observe the element&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-element&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you only want the animation to happen once, instead of every time the element is scrolled in and out of the viewport, you can skip removing the class and call &lt;code&gt;observer.unobserve(entry.target)&lt;/code&gt; to stop observing the element after the class has been added.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scroll-bound animations without a library
&lt;/h2&gt;

&lt;p&gt;If you want to create a scroll-bound animation without a library, you can use a scroll event listener with a callback function that transforms your element based on the distance the page has been scrolled.&lt;/p&gt;

&lt;p&gt;Every time the user scrolls the page, the scroll event will be fired. In the scroll event callback, you can check the scroll distance and then change an element's style.&lt;/p&gt;

&lt;p&gt;As the user continues to scroll, the element will continue to change, giving the illusion of motion.&lt;/p&gt;

&lt;p&gt;Here's an example where an element's opacity is bound to the scroll position:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-element&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pageYOffset&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getBoundingClientRect&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;elementHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;elementHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a full demo and explanation of this technique, check out my tutorial for &lt;a href="https://coolcssanimation.com/element-fade-out-on-scroll/"&gt;how to fade an element out on scroll&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>scrollanimations</category>
      <category>javascript</category>
      <category>ui</category>
      <category>frontend</category>
    </item>
    <item>
      <title>The best scroll-based animation libraries</title>
      <dc:creator>Nick Ciliak</dc:creator>
      <pubDate>Sat, 20 Feb 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/nickcil/the-best-scroll-based-animation-libraries-3bb8</link>
      <guid>https://dev.to/nickcil/the-best-scroll-based-animation-libraries-3bb8</guid>
      <description>&lt;p&gt;While scroll-based animations are all based on the same concepts, there are actually many different tools you can use to implement them.&lt;/p&gt;

&lt;p&gt;The first, and most obvious, is by coding them from scratch. This is always an option, but not what we're going to discuss in this post. If you're here, you already know that using a library to implement scroll animations can save you lots of time.&lt;/p&gt;

&lt;p&gt;There are lots of libraries out there to help with this (I've analyzed over 30 in my &lt;a href="https://coolcssanimation.com/guide-to-scroll-triggered-animation-libraries/"&gt;guide to scroll-triggered animation libraries&lt;/a&gt;). So which scroll-based animation library is the best?&lt;/p&gt;

&lt;h2&gt;
  
  
  What to consider when choosing a scroll animation library
&lt;/h2&gt;

&lt;p&gt;There are lots of things to consider when choosing a scroll animation library to use in your project. Here's some of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scroll-triggered or scroll-bound support&lt;/li&gt;
&lt;li&gt;Tweening support&lt;/li&gt;
&lt;li&gt;Scroll-pinning support&lt;/li&gt;
&lt;li&gt;Browser support&lt;/li&gt;
&lt;li&gt;Speed and performance&lt;/li&gt;
&lt;li&gt;Customization&lt;/li&gt;
&lt;li&gt;Package size&lt;/li&gt;
&lt;li&gt;License and price&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I've spent hours researching, collecting data, and testing the scroll-based animation libraries available today (there are a &lt;em&gt;ton&lt;/em&gt; of them). Some are very similar, and some are quite different from each other.&lt;/p&gt;

&lt;p&gt;Really the most important thing to consider is going to be whether or not the library offers support for the type of scroll animation that you need to build. After all, that's why you're using the library in the first place: to accomplish a specific task.&lt;/p&gt;

&lt;p&gt;You also have to consider the library's license: is it free for commercial use? (That's the most common question!)&lt;/p&gt;

&lt;p&gt;If you don't have the budget, it's important to choose a library that has the MIT license (or similar) if you're working on a commercial project. If it's a personal project that you're not profiting off of (for example, selling work to a client) then in general you should be covered under most licenses. But it's important to &lt;em&gt;always&lt;/em&gt; double check this.&lt;/p&gt;

&lt;p&gt;Another important aspect to consider is the size of the library. Including a library means more JavaScript is on your page, which impacts the time it takes for your page to load. Some libraries are under 2kb in size and some are over 30kb. You don't want to include a hefty package if you aren't going to utilize all of it. That's why it's important to make sure you understand which features of a library you actually need to use.&lt;/p&gt;

&lt;p&gt;Flexibility is an important and often overlooked attribute when choosing a scroll animation library. Certain libraries may allow you to implement the same effect, but not all of them allow you to customize them to your exact needs, as well as provide events to hook into if you need to do additional work.&lt;/p&gt;

&lt;p&gt;So, considering all of the different options and attributes, which scroll-based animation library is the best?&lt;/p&gt;

&lt;h2&gt;
  
  
  Best library for simple scroll animations
&lt;/h2&gt;

&lt;p&gt;My pick for the best library for creating simple scroll-triggered animations is &lt;a href="https://mciastek.github.io/sal/"&gt;Sal.js&lt;/a&gt;. If you're new to web development or just need a simple and quick way to add a fade-in animation to your page, this is the library you want. It's easy enough to drop in to a project but offers options for customization.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Pros of Sal.js:&lt;/em&gt; Sal.js is very beginner-friendly and contains 10+ built-in animations for you to use right away. It's also pretty customizable: you can use your own CSS animations with it as well as easily enable and disable animations and hook into in/out events. It is only 1.3kb when minified and gzipped, which is pretty darn tiny! It's licensed under the MIT License, so it's free for commercial and personal use.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cons of Sal.js:&lt;/em&gt; Sal.js animations are triggered based on how much of an element is visible in the viewport. While it offers some customization of this, it's more difficult to fully customize a scene. Also, it can be buggy in some browsers when using repeated animations.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;When you should use Sal.js:&lt;/em&gt; Use this library if you need to create simple scroll-triggered animations using CSS classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best library for advanced scroll animations
&lt;/h2&gt;

&lt;p&gt;My pick for the best library for creating advanced scroll-triggered animations is &lt;a href="https://greensock.com/scrolltrigger/"&gt;GSAP ScrollTrigger&lt;/a&gt;. GSAP is a JavaScript animation library that offers a number of additional plugins, including ScrollTrigger (this means that you can't just use ScrollTrigger on its own, you also need to use GSAP). ScrollTrigger is the all-in-one library: you can create any scroll-based animation, including scroll-triggered animations, scroll-bound animations, and scroll-pinning. ScrollTrigger is free to use for personal projects or client projects with a one-time fee but you'll need to buy a license to use it in any commercial projects where end users are charged a usage fee.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Pros of GSAP ScrollTrigger:&lt;/em&gt; With ScrollTrigger, you can create pretty much any scroll animation effect you can think of with it right out of the box, plus it's super customizable. It's perfect for creating entire scenes where you need to control multiple keyframe animations. In addition, you can do all of your work in JavaScript, without having to coordinate adding and removing CSS classes to implement keyframe animations. Also, ScrollTrigger and GSAP are highly-focused on performance, compatibility, and browser support, which means you'll spend less time spend debugging and optimizing your animations.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cons of GSAP ScrollTrigger:&lt;/em&gt; While they offer lots of demos and great API documentation, ScrollTrigger is not as beginner-friendly as other scroll animation libraries out there (mostly because you &lt;em&gt;also&lt;/em&gt; need to learn to use GSAP itself to implement ScrollTrigger. If you already have GSAP experience, it's not as difficult). It's considerably larger than other libraries as well: if you include GSAP and ScrollTrigger in your project, you'll add around 30kb minified and gzipped to your project, which puts it on the larger side for a scroll animation libraries.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;When you should use GSAP ScrollTrigger:&lt;/em&gt; Use ScrollTrigger if you need to create a highly-interactive page with multiple complex scroll-bound animations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best library for every other use case
&lt;/h2&gt;

&lt;p&gt;There is no overall "best" option when it comes to scroll animation libraries. There is only the best for your use case! While I've picked Sal.js and ScrollTrigger to represent the best libraies for simple and advanced scroll-based animations, there are many more that represent the inbetween.&lt;/p&gt;

&lt;p&gt;I wrote a &lt;a href="https://coolcssanimation.com/guide-to-scroll-triggered-animation-libraries/"&gt;guide to the top 10 libraries&lt;/a&gt; used to create scroll-triggered animations. It compares and contrasts each library based on the criteria above so you can choose the exact right one for your needs and skill level. It even has a handy comparison chart for quick reference.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>javascript</category>
      <category>ui</category>
      <category>scrollanimations</category>
    </item>
    <item>
      <title>The three concepts behind every scroll animation</title>
      <dc:creator>Nick Ciliak</dc:creator>
      <pubDate>Sat, 13 Feb 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/nickcil/the-three-concepts-behind-every-scroll-animation-3ogg</link>
      <guid>https://dev.to/nickcil/the-three-concepts-behind-every-scroll-animation-3ogg</guid>
      <description>&lt;p&gt;Scroll-based animations can be visually complex, but behind all of them are just three basic concepts. You can create any scroll animation if you understand these three things and how to implement them.&lt;/p&gt;

&lt;p&gt;The three concepts are: triggers, pinning, and tweening. Most scroll animations rely on triggers. Some rely on triggers only, and others rely on all three. But if you understand all three, you can create any scroll animation effect.&lt;/p&gt;

&lt;p&gt;Let’s break each concept down, then see how they apply to real-world examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Triggers
&lt;/h2&gt;

&lt;p&gt;A trigger is an event that initiates a scroll animation or effect. Here are the most common triggers used in scroll animations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an element enters the viewport&lt;/li&gt;
&lt;li&gt;an element reaches the center of the viewport&lt;/li&gt;
&lt;li&gt;an element leaves the viewport&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When a trigger is detected, we can apply any transition, animation, or effect to an element. In many cases, this is as simple as adding or removing a class from an element. In other cases, it can tell an element to start tweening.&lt;/p&gt;

&lt;p&gt;Most triggers are based on the element being animated, but they don't have to be. For example, a trigger can also be the user reaching the end of a page or scrolling to the top of a page. Or it could be when two elements overlap or when the user changes scroll direction.&lt;/p&gt;

&lt;p&gt;Is scrolling itself a trigger? Not really. Scrolling is implicit in scroll-based animations, so scrolling itself is not a trigger. For our purposes, a trigger is a specific occurrence related to scrolling, not the act of scrolling itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Pinning
&lt;/h2&gt;

&lt;p&gt;Pinning is a scroll-triggered effect where a freely scrolling element becomes fixed at a certain scroll position. It remains fixed indefinitely or until a specified point. This effect is often combined with animations, but by definition does not represent an animated effect.&lt;/p&gt;

&lt;p&gt;Pinning is important to scroll animations because it allows us to manipulate time as a user scrolls. For example, an element can enter the viewport by scrolling and be pinned as other elements continue to scroll. The timeline of one element is altered while the rest are not. This contrast gives the opportunity to create interest, draw attention, and tell a story.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Tweening
&lt;/h2&gt;

&lt;p&gt;Tweening is an animation concept where frames are generated from the start point to the end point of an animation and played in succession to create the illusion of motion. The frames are generated by interpolating interstitial values based on a duration input.&lt;/p&gt;

&lt;p&gt;For scroll-triggered animations, where an animation is played independently of the scroll position, the duration of the animation is a length of time. For example, a fade-in animation that takes 3 seconds. The 3 seconds is fixed, so the browser can generate the frames for us without any additional user input. This is commonly done with a CSS animation or transition.&lt;/p&gt;

&lt;p&gt;For a scroll-bound animation, where the animation state is bound to the user's scroll position, the duration of the animation is a distance. This means that the actual animation can be fast or slow depending on the speed the user scrolls. For example, a fade-in animation that occurs over the scroll-distance of 300px can take 100ms or 1s. It all depends on the speed of scrolling. That means that for scroll-bound animations, inbetween frames must be generated on demand based on the user's scroll position.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting it all together
&lt;/h2&gt;

&lt;p&gt;Using triggers, pinning, and tweening, you can create any scroll-based animation or effect. Let's take a look at two real-world examples and see how each concept manifests in them.&lt;/p&gt;

&lt;p&gt;The first example is &lt;a href="https://www.sketch.com/"&gt;sketch.com&lt;/a&gt;. This page uses two of the three concepts: triggers and tweening. Some elements on the page fade into view (and move up slightly) when they are scrolled into the viewport. This is an example of a trigger.&lt;/p&gt;

&lt;p&gt;It also uses tweening: the section that says "Open up the design process" has an illustration that transforms as you scroll. The more you scroll, the more the animation proceeds.&lt;/p&gt;

&lt;p&gt;Another example of these concepts is &lt;a href="https://stories.google/"&gt;stories.google&lt;/a&gt;. This page has a great example of pinning: notice that when you scroll, the phone in the center of the page becomes fixed as the rest of the page continues to scroll around it. It stays pinned to draw focus for a bit, then it unpins and continues to scroll offscreen.&lt;/p&gt;

&lt;p&gt;This page also uses triggers (elements fade in when a point is reached, the background color changes when a point is reached) and tweening (the phone changes in scale as you scroll and the screens at the bottom are translated at different speeds).&lt;/p&gt;

&lt;p&gt;As you can see, these concepts can be found in scroll animations all over the web. While the results can be very different and imaginative, the three concepts of triggers, pinning, and tweening will come up again and again. Once you learn to implement each of them, you can create any scroll animation you want.&lt;/p&gt;

</description>
      <category>ui</category>
      <category>animation</category>
      <category>frontend</category>
      <category>scrollanimations</category>
    </item>
    <item>
      <title>How to make an element fade out on scroll</title>
      <dc:creator>Nick Ciliak</dc:creator>
      <pubDate>Sat, 23 Jan 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/nickcil/how-to-make-an-element-fade-out-on-scroll-2ahi</link>
      <guid>https://dev.to/nickcil/how-to-make-an-element-fade-out-on-scroll-2ahi</guid>
      <description>&lt;p&gt;Fading an element out on scroll is a subtle effect that helps draw attention away from what is exiting the viewport and towards whatever is below.&lt;/p&gt;

&lt;p&gt;It works like this: as the user scrolls down the page, when the given element reaches the top of the viewport, it begins to fade out depending on how much the user has scrolled. By the time the element exits the viewport, it has completely faded out.&lt;/p&gt;

&lt;p&gt;Take a look at this demo.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/nickcil/embed/RwRwXXN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;To make the effect, we need the trigger and action. The trigger is the user scrolling. When a user scrolls, the &lt;code&gt;scroll&lt;/code&gt; event is fired. We can then call a function, our action. The action in this case is updating the opacity of the element. The more the user scrolls, the less opaque the element should become.&lt;/p&gt;

&lt;p&gt;Here's how it works. Start with adding a scroll event listener and a handler method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;scrollHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// this runs every time the user scrolls&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scrollHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now whenever the user scrolls, the &lt;code&gt;scrollHandler&lt;/code&gt; function will be called. (Note: it will run a lot if the user is continuously scrolling!)&lt;/p&gt;

&lt;p&gt;Now we need to update the element's opacity based on how much the user has scrolled. This will involve a calculation using the distance scrolled, the distance from the element to the top of the page (if there is any), and the element's height.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pageYOffset&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getBoundingClientRect&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need the distance from the top of the element to the top of the page (note: the top of the page, not the top of the viewport) because we want to start fading out when the user has scrolled that amount of distance. If the user has scrolled that amount, it means the user has scrolled enough so that the element is just at the top of the viewport.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;elementHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we need the height of the element. We want the element to fade out evenly from the start of its exit to the end of its exit. To do that, we need to compare the amount scrolled to the height of the element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we need the amount that the user has scrolled. This is the magic number that powers our calculations.&lt;/p&gt;

&lt;p&gt;Here's the calculation. If the &lt;code&gt;scrollTop &amp;gt; distanceToTop&lt;/code&gt;, that means we should start fading out. The opacity should be 1 minus the difference of the amount we've scrolled and the distance of the element to the top of the page, divided by the element's height. As the &lt;code&gt;scrollTop&lt;/code&gt; or amount scrolled increases, this result will increase. Since we're subtracting it from 1, the more it increases, the closer to 0 the final opacity comes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;elementHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the opacity is not greater than or equal to 0, we don't need to set it on the element. (A negative opacity value will do nothing.)&lt;/p&gt;

&lt;p&gt;Here's what it looks like all together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;scrollHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;header&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pageYOffset&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getBoundingClientRect&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;elementHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;elementHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}}&lt;/span&gt;

&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scrollHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Element fade out on scroll code example
&lt;/h2&gt;

&lt;p&gt;Here's what the full code looks like for this demo. Please note, using the scroll event to perform DOM updates in this way can be resource-intensive and should be used sparingly. If you need to create this effect on one or two elements on the page, you'll be fine. But if you have to do it to many, it's likely you'll want to make some additional optimizations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;header&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fadeOutOnScroll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pageYOffset&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getBoundingClientRect&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;elementHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;documentElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;distanceToTop&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;elementHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;scrollHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;fadeOutOnScroll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;scroll&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scrollHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to take a look at the &lt;a href="https://codepen.io/nickcil/pen/RwRwXXN"&gt;CodePen demo&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>scrfollanimation</category>
      <category>ui</category>
      <category>frontend</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
