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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.