Subtle, yet Beautiful Scroll Animations

Beyond Fireship · Intermediate ·🌐 Frontend Engineering ·3y ago

Key Takeaways

The video demonstrates how to implement scroll animations using HTML, CSS, and JavaScript, specifically utilizing the JS IntersectionObserver and CSS transitions to achieve a native 'animate on scroll' effect. The tutorial covers how to create a simple fade animation and provides pro tips on enhancing the animation with additional effects such as transforming and blurring elements, as well as staggering animations for multiple items.

Full Transcript

a few days ago I was on the GitHub co-pilot homepage as I was scrolling down trying to figure out if I should buy this thing I noticed how different elements in the page would animate into the screen when they became visible like this text comes in first then the icons come in staggered after that I was so impressed in fact that it's what pushed me over the edge to buy get Hub co-pilot despite my better judgment if you want to sell something on a website in 2022 you have to animate things on scroll and in today's video I'll show you exactly how to do that it can actually be accomplished with just a tiny bit of vanilla JavaScript and some CSS along the way I'll teach you some other cool tricks and we'll talk about some nextg CSS features that might make this tutorial obsolete in the future there are a few different ways we could approach this problem there's a popular Library called animate on scroll but it hasn't been touched in 4 years and will add about 4 and 1 12 kilobytes to your bundle that could work but the next gen approach would be to use a brand new CSS feature called scroll timeline it's an at rule kind of like a media query that integrates with the animation timeline from the web animations API this allows you to apply CSS animations where the key frames are based on the position of the scroll bar that's pretty cool but let's make sure that it's supported in modern browsers upon inspection it looks like it's not supported anywhere so that one's out of the question for now but we'll keep it in mind for the future another approach is to use the intersection Observer API which does have full support in all modern browsers what it does is tell you if an element in the Dom is visible to the end user that's a very useful piece of information if an element is intersecting we can apply a CSS class that animates it into the Dom to get started open up your editor and just create a plain HTML page type an exclamation point followed by tab to generate some boiler plate then in the head of the document I'm linking to a stylesheet and a script named app.js and you want to make sure the script has the defer attribute so that it's executed after the HTML elements in the body are available now go ahead and create the app.js and style.css files you can view the website by copying the path to the index HTML file and paste it into a browser or if you have node installed you can run npx serve from the terminal now that we have a blank website I'm going to add some markup to the body in the form of a bunch of different sections with some basic content the content itself doesn't really matter now in the CSS file I'm importing a Google font then adding a dark background to the body now the most difficult thing you'll have to do in this tutorial is Center a div or in this case Center all the content inside of each section an easy way to do that is to make it a grid and then place the items in the center if you have multiple items inside the section you can also use a line content Center to kind of squeeze everything into the middle I'm also giving each section a mid height just so we have a lot of room to scroll through and now it's time to bring in the intersection Observer we'll first need to identify an element in the Dom that we want to observe and this would be much easier to do in a component framework but in vanilla JS we can give each element a special class like hidden then in the JavaScript grab all the elements that have that class using document query selector all the next step is to create the intersection Observer this is a class that takes a callback function in its Constructor the cool thing about it is that it can observe multiple elements or entries at the same time this function will run anytime the visibility of one of the observed elements changes because it handles multiple entries we'll need to use four each to Loop over them then we can run a simple conditional check to find out if that entry is intersecting the viewport or not if it is intersecting We'll add a class to it called show which will make it visible if you only want to show the animation once you could stop here but if you want to show it multiple times then you'll also want to remove the class show when it's not intersecting now that we have the Observer we need to tell it what to observe in this demo we can simply Loop over all of the Hidden elements and tell the Observer to observe each one of them and that's all the code we need on the JavaScript side we can handle the actual animation with CSS transitions the hidden class will apply an opacity of zero making the element invisible while the show class will bring the opacity back to one now we can easily turn this into a fade animation with a transition property and now when you view the website you should get this fadein animation when the elements scroll into view so overall that effect is super easy but there's a few extra Pro tips that I want to talk about here first of all you may want to put your animation code inside a media query for prefers reduced motion not everybody wants to watch your stupid animations over and over again so this is the polite way to write your code the other thing is that you can do a lot more than just a simple fade I like to go balls out and also animate the transform property to start the animation all the way on the left side of the screen and I might also use the filter property to throw in a blur which gives us an animation that flies in from the left side and goes from blurry to clear and just feels a little more interesting and lifelike that's a nice Improvement but if you're working with multiple items in a list list they'll look way cooler if you stagger them which can be accomplished by adding a delay for each item in the list as you can see here I have a div with four children that all have a class of logo in the CSS we can Target the children with the nth child pseudo selector it'll find all the elements that have a class of logo and the parent element allowing you to Target them one by one or the second element will apply a transition delay of 100 milliseconds then 200 milliseconds for the third element 300 for the fourth and so on and that's all it takes to create a far more interesting staggered animation thanks for watching and I will see you in the next one

Original Description

Learn how to implement scroll animations with HTML, CSS, and JavaScript. Use the JS IntersectionObserver and CSS transitions to implement a native "animate on scroll" effect that works in any browser.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Beyond Fireship · Beyond Fireship · 6 of 42

1 Social Media Style Number Formatting in JS
Social Media Style Number Formatting in JS
Beyond Fireship
2 I used 3 different File System APIs in Node.js
I used 3 different File System APIs in Node.js
Beyond Fireship
3 Time is Relative, even in JavaScript
Time is Relative, even in JavaScript
Beyond Fireship
4 How to NOT Screw Up Firebase Environment Variables
How to NOT Screw Up Firebase Environment Variables
Beyond Fireship
5 How to make your JavaScript Bundle Smaller
How to make your JavaScript Bundle Smaller
Beyond Fireship
Subtle, yet Beautiful Scroll Animations
Subtle, yet Beautiful Scroll Animations
Beyond Fireship
7 Beyond Surreal? A closer look at NewSQL Relational Data
Beyond Surreal? A closer look at NewSQL Relational Data
Beyond Fireship
8 How to Build a Discord Bot
How to Build a Discord Bot
Beyond Fireship
9 How to make Eyeballs that Follow You Around
How to make Eyeballs that Follow You Around
Beyond Fireship
10 Reverse Engineer Google’s NASA Dart Easter Egg with CSS
Reverse Engineer Google’s NASA Dart Easter Egg with CSS
Beyond Fireship
11 Generate Images Programmatically on the Edge
Generate Images Programmatically on the Edge
Beyond Fireship
12 WTF are all these config files for?
WTF are all these config files for?
Beyond Fireship
13 NEW Firebase Features Just Dropped
NEW Firebase Features Just Dropped
Beyond Fireship
14 Next.js 13 - The Basics
Next.js 13 - The Basics
Beyond Fireship
15 Make Crazy Art with the NEW OpenAI Dall-e API
Make Crazy Art with the NEW OpenAI Dall-e API
Beyond Fireship
16 How to Setup Node.js with TypeScript in 2023
How to Setup Node.js with TypeScript in 2023
Beyond Fireship
17 Dramatically improve website speed with Partytown
Dramatically improve website speed with Partytown
Beyond Fireship
18 The easiest realtime app I’ve ever built
The easiest realtime app I’ve ever built
Beyond Fireship
19 10 Rendering Patterns for Web Apps
10 Rendering Patterns for Web Apps
Beyond Fireship
20 You don't need Node to use NPM packages
You don't need Node to use NPM packages
Beyond Fireship
21 Sorting Algorithms Explained Visually
Sorting Algorithms Explained Visually
Beyond Fireship
22 ChatGPT Official API First Look
ChatGPT Official API First Look
Beyond Fireship
23 I built an image search engine
I built an image search engine
Beyond Fireship
24 Industrial-scale Web Scraping with AI & Proxy Networks
Industrial-scale Web Scraping with AI & Proxy Networks
Beyond Fireship
25 Next.js Server Actions...  5 awesome things you can do
Next.js Server Actions... 5 awesome things you can do
Beyond Fireship
26 The ultimate guide to web performance
The ultimate guide to web performance
Beyond Fireship
27 I built a fullstack PaLM AI app in just 2 minutes
I built a fullstack PaLM AI app in just 2 minutes
Beyond Fireship
28 I tried 8 different Postgres ORMs
I tried 8 different Postgres ORMs
Fireship
29 I built a *streaming* AI chat app
I built a *streaming* AI chat app
Fireship
30 React VS Svelte...10 Examples
React VS Svelte...10 Examples
Fireship
31 How GitHub Actions 10x my productivity
How GitHub Actions 10x my productivity
Fireship
32 PROOF JavaScript is a Multi-Threaded language
PROOF JavaScript is a Multi-Threaded language
Fireship
33 Mind-blowing page animations are easy now... View Transitions API first look
Mind-blowing page animations are easy now... View Transitions API first look
Fireship
34 I built an Apple Vision Pro app... visionOS tutorial
I built an Apple Vision Pro app... visionOS tutorial
Fireship
35 This UI component library is mind-blowing
This UI component library is mind-blowing
Fireship
36 How I deploy serverless containers for free
How I deploy serverless containers for free
Fireship
37 GitHub Copilot now controls your command line...
GitHub Copilot now controls your command line...
Fireship
38 Build better payment forms using new “embedded” Stripe Checkout
Build better payment forms using new “embedded” Stripe Checkout
Fireship
39 Does Deno 2 really uncomplicate JavaScript?
Does Deno 2 really uncomplicate JavaScript?
Fireship
40 JavaScript performance is weird... Write scientifically faster code with benchmarking
JavaScript performance is weird... Write scientifically faster code with benchmarking
Fireship
41 Is Next.js 15 any good? "use cache" API first look
Is Next.js 15 any good? "use cache" API first look
Beyond Fireship
42 I built a DeepSeek R1 powered VS Code extension…
I built a DeepSeek R1 powered VS Code extension…
Fireship

Learn how to create scroll animations using HTML, CSS, and JavaScript, and discover pro tips for enhancing the animation. The tutorial covers the basics of implementing a simple fade animation and provides examples of more complex effects.

Key Takeaways
  1. Create a plain HTML page
  2. Link to a stylesheet and a script named app.js
  3. Add markup to the body in the form of sections with content
  4. Import a Google font and add a dark background to the body
  5. Center the content inside each section using grid or flexbox
  6. Identify elements to observe using the IntersectionObserver API
  7. Create the IntersectionObserver and observe the elements
  8. Add a class to make the elements visible when they intersect the viewport
  9. Handle the animation with CSS transitions
💡 The IntersectionObserver API can be used to observe multiple elements and apply animations when they become visible, making it a powerful tool for creating scroll animations.

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
How to Open KIT Files (CodeKit Project)
File Extension Geeks
Watch →