Fade In On Scroll Using CSS and JavaScript

Fade In On Scroll Using CSS and JavaScript
Code Snippet:OLD - show element when scrolling down (vanilla js)
Author: Nathalie Almeida
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 2,147
License: MIT
Edit Code online: View on CodePen
Read More

The “Fade In On Scroll” effect is a popular web design technique that adds a touch of interactivity to your website. This CSS and JavaScript code snippet helps you to create fade in effect on scroll event. It detects when the elements are within 300 pixels of the top of the screen and adds a class, “scroll–show,” to make them visible.

This effect is applied to headings within different sections, giving your webpage an engaging appearance. It’s a helpful way to create a stylish scroll-triggered animation on your webpage.

How to Create Fade In Effect On Scroll Using CSS & Javascript

1. Begin by setting up the HTML structure of your webpage. In this example, we’ll use a series of sections with headings that we want to apply the “Fade In On Scroll” effect to. Here’s a simplified example of the HTML structure:

<section class="section01">
  <h1>Uma</k1>
</section>
<section class="section02">
  <h1 class="scroll">coisa</h1>
</section>
<section class="section03">
  <h1 class="scroll">de cada</h1>
</section>
<section class="section04">
  <h1 class="scroll">vez</h1>
</section>

In the above HTML code, we’ve added the "scroll" class to the headings we want to apply the fading effect. You can add this class to the element that you want to fade in on the page scroll down.

2. Next, let’s define the CSS styles to control the appearance of our sections and headings. The key elements here are the "scroll" class, which initially sets the opacity to 0 and a translateY transformation to move the elements up:

@import url("https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700");

body {
  margin: 0;
  padding: 0;
}

section {
  height: 100vh;
  display: flex; 
  align-items: center;
  justify-content: center;
}
h1 {
  text-transform: uppercase;
  font-family: 'Open Sans', sans-serif;
  font-size: 100px;
  font-weight: 900;
  margin: 0;
  color: #fff;
}
.section01{
  background: #621055;
}
.section02{
  background: #b52b65;
}
.section03{
  background: #ed6663;
  
}
.section04{
  background: #ffa372; 
}
.scroll {
   transform: translateY(-30px);
  opacity: 0;
  transition: 1s;
}
.scroll--show {
  transform: translateY(0px);
  opacity: 1;
}

3. Finally, implement the JavaScript code that handles the fading effect. This code listens for scroll events and adds the "scroll--show" class to elements with the "scroll" class when they come within 300 pixels from the top of the screen:

var elScroll = document.querySelectorAll('.scroll'); //pega todos os elementos com a classe .scroll

document.onscroll = function() { //ao rolar a tela...
  elScroll.forEach(elScroll => { //cada elemento com a classe .scroll ...
  var positionEl = elScroll.getBoundingClientRect(); //pega valores da posição do elemento
  var alturaEl = positionEl.top; //pega distancia do topo da tela
  
  if(alturaEl < 300) { //se a distancia do topo for menor que 300
    elScroll.classList.add('scroll--show'); //adiciona a classe .scroll--show
  }
 });
}

That’s all! hopefully, you have successfully created Fade In animation on scroll using CSS and JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X