A lightweight JavaScript utility to fade elements in and out of view on page scroll.

Overview

ScrollFade

ScrollFade is used to fade elements in and out of view while scrolling through a page. Tag any elements you want to fade with the class scrollFade and add the following CSS attributes and JS to your website:

Vist here for an example, or download this repository.

Feel free to add a PR for improvements

CSS

.scrollFade {
  opacity: 1;
  pointer-events: all;
}

.scrollFade--hidden {
  opacity: 0;
  pointer-events: none;
}

.scrollFade--visible {
  opacity: 1;
  pointer-events: all;
}

.scrollFade--animate {
  transition: opacity 0.4s ease-in-out;
}

SCSS

.scrollFade {
  opacity: 1;
  pointer-events: all;

  &--hidden {
    opacity: 0;
    pointer-events: none;
  }

  &--visible {
    opacity: 1;
    pointer-events: all;
  }

  &--animate {
    transition: opacity 0.4s ease-in-out;
  }
}

JavaScript

var fadeElements = document.getElementsByClassName('scrollFade');

function scrollFade() {
  var viewportBottom = window.scrollY + window.innerHeight;

  for (var index = 0; index < fadeElements.length; index++) {
    var element = fadeElements[index];
    var rect = element.getBoundingClientRect();

    var elementFourth = rect.height/4;
    var fadeInPoint = window.innerHeight - elementFourth;
    var fadeOutPoint = -(rect.height/2);

    if (rect.top <= fadeInPoint) {
      element.classList.add('scrollFade--visible');
      element.classList.add('scrollFade--animate');
      element.classList.remove('scrollFade--hidden');
    } else {
      element.classList.remove('scrollFade--visible');
      element.classList.add('scrollFade--hidden');
    }

    if (rect.top <= fadeOutPoint) {
      element.classList.remove('scrollFade--visible');
      element.classList.add('scrollFade--hidden');
    }
  }
}

document.addEventListener('scroll', scrollFade);
window.addEventListener('resize', scrollFade);
document.addEventListener('DOMContentLoaded', function() {
  scrollFade();
});
You might also like...

A utility for creating toggleable items with JavaScript. Inspired by bootstrap's toggle utility. Implemented in vanillaJS in a functional style.

LUX TOGGLE Demo: https://jesschampion.github.io/lux-toggle/ A utility for creating toggleable dom elements with JavaScript. Inspired by bootstrap's to

Oct 3, 2020

CLI utility that parses argv, loads your specified file, and passes the parsed argv into your file's exported function. Supports ESM/TypeScript/etc out of the box.

cleffa CLI tool that: Parses argv into an object (of command-line flags) and an array of positional arguments Loads a function from the specified file

Mar 6, 2022

Some out-of-the-box utility features based on the Oasis engine.

Engine Toolkit Some out-of-the-box utility features based on the Oasis engine Script, welcome to enjoy! Features 🛸 Controls - Some camera controllers

Nov 21, 2022

Utility to track the boundaries of html-elements in SolidJS

solid-boundaries Helps you to track the size and position of html-elements in solid-js. What does it do? See it in action here, or see it on CodeSandb

Jan 6, 2023

Reveal CSS animation as you scroll down a page

WOW.js Reveal CSS animation as you scroll down a page. By default, you can use it to trigger animate.css animations. But you can easily change the set

Jan 8, 2023

Automatically make same-page links scroll smoothly

Automatically make same-page links scroll smoothly

Smooth Scroll Plugin Allows for easy implementation of smooth scrolling for same-page links. Note: Version 2.0+ of this plugin requires jQuery version

Dec 25, 2022

infiniteScrollWithTemplate - JQuery plugin for ajax-enabled infinite page scroll / auto paging with template

jQuery Infinite With Template Plugin JQuery plugin for ajax-enabled infinite page scroll with template. If you like jQuery until now, this little libr

Mar 19, 2021

The awesomebooks project is a simple list, but separated into 3 parts and given a retro feel. The main page is where we can add books, and on another page we can see the list, and remove items. There is also a "contact-us" page.

Awesome Books This is the restructured version of the famous awesome-books project! Here you can find JavaScript broken into modules, using import-exp

Nov 15, 2022

curtains.js is a lightweight vanilla WebGL javascript library that turns HTML DOM elements into interactive textured planes.

curtains.js is a lightweight vanilla WebGL javascript library that turns HTML DOM elements into interactive textured planes.

What is it ? Shaders are the new front-end web developpment big thing, with the ability to create very powerful 3D interactions and animations. A lot

Jan 1, 2023
Owner
Jordan Trbuhovic
I'm a web developer currently working for CloudCannon as a Services engineer. I also run Meddle - my personal web development agency
Jordan Trbuhovic
A javascript library to animate elements on scroll page events

ScrollJS by Sam Sirianni ScrollJS is a library written in Javascript. With ScrollJS you can animate elements on scroll events. Visit the ScrollJS webs

Sam Sirianni 1 Mar 21, 2021
Create a deep copy of a set of matched elements with the dynamic state of all form elements copied to the cloned elements.

jq-deepest-copy FUNCTION: Create a deep copy of a set of matched elements while preserving the dynamic state of any matched form elements. Example Use

Michael Coughlin 5 Oct 28, 2022
Lightweight and easy to use vanilla js library to add css animations to elements on scroll.

Scrollrisen Trigger css animations on scroll as elements enter the viewport. Zero dependencies, lightweight and easy to use and customize Installation

null 1 Oct 13, 2022
Javascript library for switching fixed elements on scroll through sections. Like Midnight.js, but without jQuery

Library for Switching Fixed Elements on Scroll Sometimes designers create complex logic and fix parts of the interface. Also they colour page sections

Vladimir Lysov 38 Sep 19, 2022
A fullscreen scroll-based slideshow with a content view powered by GreenSock's Observer plugin.

Fullscreen Scrolling Slideshow A fullscreen scroll-based slideshow with a content view powered by GreenSock's Observer plugin. Article on Codrops Demo

Codrops 84 Jan 3, 2023
StarkNet support extension for VSCode. Visualize StarkNet contracts: view storage variables, external and view functions, and events.

StarkNet Explorer extension This VSCode extension quickly shows relevant aspects of StarkNet contracts: Storage variables of the current contract, and

Crytic 6 Nov 4, 2022
A plugin for Strapi CMS that adds a preview button and live view button to the content manager edit view.

Strapi Preview Button A plugin for Strapi CMS that adds a preview button and live view button to the content manager edit view. Get Started Features I

Matt Milburn 53 Dec 30, 2022
An app to test out your typing speed, save your progress and view statistics against them.

Introduction An app to test out your typing speed, save your progress and view statistics against them. Demo Check out the quick demo here. Getting St

Nithin Kumar B 2 Sep 7, 2022
TS & JS Library for adaptive precision cursor for the web. Releases will come out soon! Meanwhile, check out the demo site:

Haha, cool cursor go brrrr... Table of Content What is this? Installation & Setup Installation Setup Usage Cursor controls Element settings Known issu

LemonOrange 10 Nov 24, 2022