CSS Scroll Animation Last Updated : 12 Sep, 2024 Comments Improve Suggest changes Like Article Like Report CSS scroll animations are used to make webpages more exciting and engaging to the users by applying animations on scroll. Scroll animations assist in directing the users’ attention, enhance the usability of the site, and bring a sense of depth, without overloading the user. In this article, we will see the simplest strategies for using CSS and JavaScript to create the transition effect.What are Scroll Animations?Scroll animations refer to the movement or transformation of elements as the user scrolls through the webpage. These animations can include fading, sliding, scaling, or rotating elements into view. They are widely used in modern web design to make websites feel more interactive and visually appealing.Common Use Cases:Highlighting key sections as they come into view.Creating parallax effects where background and foreground elements scroll at different speeds.Revealing content gradually, such as text or images, as the user scrolls down the page.How Scroll Animations Work?CSS scroll animations are based on applying the transition to elements that start once a user scrolls to a certain point on the page. This is usually achieved by applying CSS transition and JavaScript to identify scroll events.Performance ConsiderationsScroll animations are known to affect the level of performance of a webpage if poorly optimized. Here are some tips to ensure your animations run smoothly:Use Hardware-Accelerated Properties: When animating, use properties like transform and opacity that are hardware-accelerated by browsers. It is recommended not to use properties such as top, left or height since they cause layout calculations and hence are expensive.Debounce Scroll Events: Scroll events happen often which might cause problems with performance. To minimize the execution of the scroll event handler, one can consider the use of debounce function or the requestAnimationFrame.Lazy Load Animations: Animate only those elements which are in the viewport. This decreases the number of active animations and thus makes the work much faster and more efficient especially on large pages.Example: The example below shows how a basic scroll animation with a fade in can be created: a box element gradually appears and scrolls in from the bottom of the page. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Scroll Animation Example</title> <style> body { font-family: Arial, sans-serif; height: 200vh; /* This makes the page tall enough to scroll */ margin: 0; padding: 0; background-color: #f4f4f4; } h1 { text-align: center; margin-top: 50px; } .box { width: 100px; height: 100px; background-color: #3498db; margin: 50px auto; border-radius: 10px; opacity: 0; transform: translateY(50px); transition: transform 0.5s ease, opacity 0.5s ease; } /* This class will trigger the animation */ .show { opacity: 1; transform: translateY(0); } </style> </head> <body> <h1>Scroll Down to See the Box</h1> <div class="box" id="box"></div> <script> window.addEventListener('scroll', function () { const box = document.getElementById('box'); const boxPosition = box.getBoundingClientRect().top; // Get box position relative to the viewport const windowHeight = window.innerHeight; // Get the height of the viewport // If the box is within the visible part of the screen, //trigger the animation if (boxPosition < windowHeight - 50) { box.classList.add('show'); } else { box.classList.remove('show'); } }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article CSS Scroll Animation G gmoksi039 Follow Improve Article Tags : Web Technologies CSS Similar Reads W3.CSS Animations CSS Animations is a technique to change the appearance and behavior of various elements in web pages. It is used to control the elements by changing their motions or display. W3.CSS provides developers with some good inbuilt animation classes. The list of classes is as follows: Sr. No. Class Name De 5 min read CSS Animations CSS animations control the movement and appearance of elements on web pages. We can animate HTML elements without using JavaScript.Use @keyframes to define the animation steps.Apply animations with properties like animation-name and animation-duration.Control the animation flow using animation-timin 7 min read CSS Floating Animation CSS Floating Animation creates a visual effect where elements appear to float smoothly, often mimicking natural movement like drifting in water or air. This effect is achieved using CSS properties such as @keyframes, transform, and animation, giving elements a subtle, continuous floating motion.CSS 1 min read Primer CSS Pulse Animation Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. It is highly reusable and flexible. It is created with GitHubâs design system. Animations are an important tool to attract users to 1 min read Primer CSS Grow x Animation Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by 2 min read ES6 | Animation In ES6 JavaScript, Animation can be done in two ways. The ES6 JavaScript can be used to move <div>, <img>, and so many other HTML element. Using setInterval() Method: The setInterval() method of JavaScript takes two arguments. A function and an Integer. It is terminated using clearInterv 4 min read Primer CSS Hover Animation Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. It is highly reusable and flexible. It is created with GitHubâs design system. Animations are an important tool to attract users to 2 min read How to make CSS Animations ? Animation is a way using which we can create the illusion of motion by placing still images one after another in a typical format. For example, we can have a ball rising up at some instant then falling down as an animation effect. CSS provides us with some properties to control the animation effect 3 min read CSS animation-duration Property The animation-duration property in CSS is essential for controlling the length of time an animation takes to complete one cycle, making it a vital tool for creating dynamic and engaging web designs.Syntax:animation-duration: time | initial | inherit;Property Value:time: This value is used to specify 3 min read Seesaw Text Animation The Seesaw animation is an animation that can be applied to various components of a website like text, cards, and even buttons. But, it is mostly used for text-animation. This is a relatively new animation when it comes to text animations. Approach: The approach is simple, we will be rotating the te 2 min read Like