Creating a Reveal Animation on Page Load and Scroll using CSS and JavaScript

In this tutorial, you can learn to create a simple Reveal Animation for website elements or contents using CSS and JavaScript. This tutorial aims to provide the students and new programmers with a reference for learning to build an interactive and creative website user interface and functionality. Here, I will be providing a simple web application's web page source code scripts that demonstrate the main goal of this tutorial.

What is Reveal Animation?

Reveal Animation is one of the most often-used features in an interactive and creative website. This kind of animation hides the UI elements or contents at first and slowly reveal or shows the exact element design or content. This feature is commonly used or implemented in a website or web application with a long page scroll. This can provide the end-users or visitors with a better experience of using the application or website.

How to Create a Reveal Animation for websites?

Reveal Animation can be achieved easily using CSS and JavaScript. Using the CSS animation property, we can set the animation that we want by setting the animation name, duration, delay, and more. This animation can be achieved using opacity, width, height, clip-path, and more properties you'll just need to pick the property that suit and reaches your own requirements. Using the @keyframes at-rule, we can set the properties to whatever we want to do between 0 to 100 percent of the animation property duration. The JS will be useful for this feature to add classNames to the elements to be revealed and detects the user/page activity such as page load and scrolling events. Check out the sample web application's web page scripts below to understand it more.

Sample Web App

Here are the scripts of the sample web application's web page that demonstrate the creation of a simple Reveal Animation.

Page Interface

Here's the HTML script of the web page elements and contents. The code consists of section elements/tags that will be used for Reveal Animation.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <meta charset="UTF-8">
  4.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>CSS and JS - Reaveal Animation on Scroll</title>
  7.     <link rel="preconnect" href="https://fonts.googleapis.com">
  8.     <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9.     <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
  10.     <link rel="stylesheet" href="style.css">
  11. </head>
  12.     <main>
  13.         <section data-reveal="true">
  14.             <div class="text-center section-title">Reaveal Animation on Scroll using CSS and JS</div>
  15.             <hr style="width: 50px;">
  16.         </section>
  17.         <section data-reveal="true">
  18.             <div class="text-center section-title">Nulla venenatis erat mauris, sed laoreet orci vulputate quis. Nam facilisis bibendum mollis. Sed mattis, arcu eget ornare tempus, libero erat consequat enim, vitae vehicula metus diam ut arcu. Vestibulum fermentum justo hendrerit, faucibus risus sed, tincidunt ligula.</div>
  19.             <hr style="width: 50px;">
  20.         </section>
  21.         <section data-reveal="true">
  22.             <div class="text-center section-title">Cras nec elit id lacus finibus gravida. Duis pellentesque imperdiet libero.</div>
  23.             <hr style="width: 50px;">
  24.         </section>
  25.         <section data-reveal="true">
  26.             <div class="text-center section-title">In accumsan urna ac neque congue sollicitudin. Aliquam a leo velit. Nullam lobortis, ante quis euismod interdum, est urna convallis nunc, ut varius erat urna eget urna.</div>
  27.             <hr style="width: 50px;">
  28.         </section>
  29.         <section data-reveal="true">
  30.             <div class="text-center section-title">You have Reached the Bottom of the Page.</div>
  31.             <hr style="width: 50px;">
  32.         </section>
  33.     </main>
  34.     <script src="script.js"></script>
  35. </body>
  36. </html>

Stylesheet

The script below is the CSS code that contains the styling or design properties of the elements of the web page. It contains the animation @keyframes script.

  1. @import url('https://fonts.googleapis.com/css2?family=Rubik&display=swap');
  2.  
  3. @import url('https://fonts.googleapis.com/css2?family=Caveat:wght@700&display=swap');
  4. body *{
  5.     font-family: 'Rubik', sans-serif;
  6. }
  7. /**
  8. Page Design
  9. */
  10. body,
  11. html{
  12.     height: 100%;
  13.     width: 100%;
  14.     margin: 0;
  15.     padding: 0;
  16. }
  17. body{
  18.     background-color: #1A120B;
  19. }
  20. main{
  21.     min-height: 100%;
  22.     width: 100%;
  23.     display: flex;
  24.     flex-direction: column;
  25.     justify-content: center;
  26.     align-items: center;
  27.     overflow-x: auto;
  28. }
  29. /*
  30. Content Design
  31. */
  32. .section-title{
  33.     color: #fff;
  34.     text-shadow: 1px 1px 3px #000;
  35.     font-size: 35px;
  36.     font-family: 'Caveat', cursive;
  37. }
  38. section{
  39.     width: 100%;
  40.     height:100vh;
  41.     display: flex;
  42.     align-items: center;
  43.     justify-content: center;
  44.     flex-direction: column;
  45.     background-attachment: fixed;
  46.     background-position: center center;
  47.     background-size: cover
  48. }
  49. section p, section .section-title{
  50.     text-align: center;
  51.     margin: 0 20%;
  52.     padding: 0 10px;
  53. }
  54. section:nth-child(1){
  55.     background-image: url(img/img-4.jpg);
  56. }
  57. section:nth-child(2){
  58.     background-image: url(img/img-3.jpg);
  59. }
  60. section:nth-child(3){
  61.     background-image: url(img/img-1.jpg);
  62. }
  63. section:nth-child(4){
  64.     background-image: url(img/img-2.jpg);
  65. }
  66. section:nth-child(5){
  67.     background-image: url(img/img-5.jpg);
  68. }
  69.  
  70. section:nth-child(3) p{
  71.     color: #fff;
  72.     text-shadow: 1px 1px 3px #939393;
  73.     font-size: 20px;
  74.     font-family: 'Caveat', cursive;
  75. }
  76. section[data-reveal="true"]  *{
  77.     clip-path: inset(0 100% 0 0);
  78. }
  79. /*
  80. Text Reveal Animation
  81. */
  82. section[data-reveal="true"].reveal *{
  83.     animation: reveal-content 3.5s ease-in-out .1s forwards ;
  84. }
  85. @keyframes reveal-content{
  86.     0%{
  87.         clip-path: inset(0% 100% 0% 0%);
  88.     }
  89.     100%{
  90.         clip-path: inset(0% 0% 0% 0%);
  91.     }
  92. }

By default, the page will look like the following image if the Reveal Animation design and functionality are not yet set.

Reveal Animation using CSS and JS

Function Script

The script below is a JavaScript code that contains the initialization of reveal animation to the selected element contents. Without the script below, the web page will only the elements without any text content.

  1. /**
  2. * Selecting All elements we want to have a reveal animation
  3. */
  4. const revealEls = document.querySelectorAll('section[data-reveal="true"]')
  5.  
  6.  
  7. /**
  8. * Initialize Reveal Animation if Element is visible to page screen
  9. */
  10. const AnimateReveal = () => {
  11.     revealEls.forEach(el => {
  12.         /**
  13.         * Check if the element height for at least 50% visible to page screen then add the reveal class if not existing yet
  14.         * else: Remove the reveal class name if it exists
  15.         */
  16.         if((el.getBoundingClientRect().top + (el.clientHeight * .5)) < window.innerHeight)  {
  17.             if(!el.classList.contains('reveal'))
  18.                 el.classList.add('reveal')
  19.         }else{
  20.  
  21.             if(el.classList.contains('reveal'))
  22.                 el.classList.remove('reveal')
  23.         }
  24.     });
  25. }
  26.  
  27. /** Trigger Animation on page load */
  28. window.onload = function(){
  29.     AnimateReveal();
  30. }
  31. /** Trigger Animation on scroll */
  32. window.onscroll = function(){
  33.     AnimateReveal();
  34. }

Overall, the web page will result in something like the following:

Reveal Animation using CSS and JS

On page load, the first section element text content will be revealed since it is already visible on the page screen. Then the other elements reveal animations that will be triggered when the user scrolls the page and the element is visible for at least 50% of it.

There you go! I have provided also the complete source code zip file on this site including the images I used for the backgrounds of the elements. Feel free to download it by clicking the download button located below this tutorial's content.

That's the end of this tutorial! I hope this Creating a Reveal Animation on Page Load and Scroll using CSS and JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment