Animations with GreenSock Animation Platform + ScrollTrigger

Using GSAP, the ScrollTrigger GSAP plugin, and this script that I wrote, you will be able to easily apply animations to your website layout. Currently it just allows for fading-in and sliding-in elements as they are intersected by the ScrollTrigger but it can easily be modified to do much more.

There are currently four classes that can be set to an element for various animation behavior. Each has various data attributes that can be applied to them so that you can control how GSAP interacts with each element individually.


.animate-scroll-fade-in
Description: Fade in from opacity 0
data-delay = number (float) – default is 0
data-duration = number (float) – default is 1
data-trigger = top, center, bottom, 100px, 50%, etc – default is top
data-scroller = top, center, bottom, 100px, 50%, etc – default is bottom
data-markers = false, true – default is false

.animate-children-scroll-fade-in
Description: This appends the ‘animate-fade-in-scroll’ class and its data attributes to all respective child elements
data-delay = number (float) – default is 0
data-duration = number (float) – default is 1
data-trigger = top, center, bottom, 100px, 50%, etc – default is top
data-scroller = top, center, bottom, 100px, 50%, etc – default is bottom
data-markers = false, true – default is false

.animate-scroll-slide-horizontal
Description: Slide in from left or right
data-delay = number (float) – default is 0
data-duration = number (float) – default is 1
data-trigger = top, center, bottom, 100px, 50%, etc – default is top
data-scroller = top, center, bottom, 100px, 50%, etc – default is bottom
data-markers = false, true – default is false
data-position = px, em, %, vh, etc – default is “100px”

.animate-scroll-slide-vertical
Description: Slide in from top or bottom
data-delay = number (float) – default is 0
data-duration = number (float) – default is 1
data-trigger = top, center, bottom, 100px, 50%, etc – default is top
data-scroller = top, center, bottom, 100px, 50%, etc – default is bottom
data-markers = false, true – default is false
data-position = px, em, %, vh, etc – default is “100px”


There are currently six data attributes that can be set as parameters for each individual animation. This includes the following:

data-delay
The amount of time, in seconds, before the animation occurs.

data-duration
The amount of time, in seconds, that the animation runs for.

data-trigger
The amount in which the scroll trigger should be placed within the trigger element.
For example, the trigger for this element’s animation will appear 100px down on the Y axis of this element:
<div class="content | animate-scroll-fade-in" data-trigger=”100px”>...</div>
You can use px, %, etc. You can also use words such as “top, center, bottom”

data-scroller
The amount in which the scroller should be placed within the viewport.
For example, the trigger for element’s animation will appear 95% down on the Y axis of the viewport:
<div class="content | animate-scroll-fade-in" data-trigger="100px" data-scroller="95%">...</div>
You can use px, %, etc. You can also use words such as “top, center, bottom”

data-position
This is for animate-scroll-slide-horizontal and animate-scroll-slide-vertical only. The amount in which the element should be pushed to the left/right/top/bottom using the transform: translate(); CSS property. If you use a negative value then this will push to the left or bottom, if you use a positive value then this will push to the right or top.

data-markers
This enables or disables (true or false) the development markers to help you see where your element’s trigger and scroller are within the page.


The animate-children-scroll-fade-in class applies the animation to all child elements of the respectful parent element. This just reduces the redundancy of having to do it manually in case there are many adjacent elements.
However, this is not to be confused with the “stagger” element as seen in my previous post titled “JS: Animations with the Intersection Observer API“. There is currently no “stagger” ability in this script. This should be possible by getting each child element within the selected parent element and then incrementing the delay by X seconds for each element. So for example if there are 5 child elements, and the value of the delay is set to 1 second, then the first element would have a delay of 1 second and the final element would have a delay of 5 seconds. I will add this eventually.

As per my last post, this example also includes the HTML CSS for a simple header, navigation menu, masthead photo slideshow section, and main content area.

This example also includes JavaScript for setting the masthead photo slideshow section’s photo to take up the entire available height of the viewport so long as that is <=960px.

Lastly, this example also includes a script that reduces the size of the header when you scroll down (to increase screen real estate) which is similar (but not identical to) the code found in my previous post titled “GSAP: Header element that shrinks upon scrolling down to the main content“.

CodePen

JavaScript

// Created by Alex Winter
// Created: 2022-07-25
// Modified: 2022-11-28




// **************************************************
// Masthead Slideshow Height
// **************************************************
const header = document.querySelector('#site-header');
const masthead = document.querySelector('.masthead-slideshow .glide');

// ternary operator
// if the element is null (undefined and doesnt exist in DOM) then set its offsetHeight value to 0
const headerHeight = header != null ? header.offsetHeight : 0; // get header height

// define viewport height
let viewportHeight = window.innerHeight;

const setMastheadHeight = () => {
    // calculate masthead height
    let mastheadHeight = viewportHeight - headerHeight;

    // apply masthead height value to masthead element
    if (masthead) {
        masthead.style.height = mastheadHeight  + 'px';
    };
};
setMastheadHeight();




// **************************************************
//  GSAP Animations
// **************************************************
// Register the ScrollTrigger plug-in
gsap.registerPlugin(ScrollTrigger)




// **************************************************
//  GSAP - Scroll - Refresh All ScrollTriggers 
// **************************************************
// * Call this function within your scrollTrigger timeline to recalculate the positioning of all of the ScrollTriggers on the page.
// * This is useful on for the "Site Header" for example because it is a fixed element that changes height after scrolling down below the fold, which breaks the placement of all other ScrollTriggers on the page.
// * This will fire after X miliseconds, hopefully after the animations have finished. Change the amount of time as needed.
const refreshScrollTrigger = () => {
    setTimeout(() => {
        ScrollTrigger.refresh() // https://greensock.com/docs/v3/Plugins/ScrollTrigger/static.refresh()
        // console.log('setTimeout has elapsed')
    }, 2000)
    // console.log('toggled inactive or active')
}




// **************************************************
//  GSAP - Scroll - Fade In (Selected element's children)
// **************************************************
// Class:       animate-children-scroll-fade-in
// Description: This appends the 'animate-fade-in-scroll' class and its data attributes to all respective child elements
// data-delay    = number (float)      - default is 0
// data-duration = number (float)      - default is 1
// data-trigger  = top, center, bottom - default is top
// data-scroller = top, center, bottom - default is bottom
// data-markers  = false, true         - default is false
let animateChildrenScrollFadeIn = document.querySelectorAll('.animate-children-scroll-fade-in')
animateChildrenScrollFadeIn.forEach((element) => {
    element.querySelectorAll(':scope > *').forEach((childElement) => {
        childElement.classList.add('animate-scroll-fade-in')

        if (element.dataset.delay) {
            childElement.dataset.delay = parseFloat(element.dataset.delay)
        }

        if (element.dataset.duration) {
            childElement.dataset.duration = parseFloat(element.dataset.duration)
        }

        if (element.dataset.trigger) {
            childElement.dataset.trigger = element.dataset.trigger
        }

        if (element.dataset.scroller) {
            childElement.dataset.scroller = element.dataset.scroller
        }

        if (element.dataset.markers) {
            childElement.dataset.markers = element.dataset.markers
        }
    })
})



// **************************************************
//  GSAP - Scroll - Fade In (Selected element only)
// **************************************************
// Class:       animate-scroll-fade-in
// Description: Fade in from opacity 0
// data-delay    = number (float)      - default is 0
// data-duration = number (float)      - default is 1
// data-trigger  = top, center, bottom - default is top
// data-scroller = top, center, bottom - default is bottom
// data-markers  = false, true         - default is false
let animateScrollFadeIn = gsap.utils.toArray('.animate-scroll-fade-in')
animateScrollFadeIn.forEach((element) => {
    let dataDelay = 0;
    if (element.dataset.delay) {
        dataDelay = parseFloat(element.dataset.delay)
    }

    let dataDuration = 1;
    if (element.dataset.duration) {
        dataDuration = parseFloat(element.dataset.duration)
    }

    let dataTrigger = 'top'
    if (element.dataset.trigger) {
        dataTrigger = element.dataset.trigger
    }

    let dataScroller = 'bottom'
    if (element.dataset.scroller) {
        dataScroller = element.dataset.scroller
    }

    let dataMarkers = false
    if (element.dataset.markers) {
        dataMarkers = element.dataset.markers
    }

    let timeline = gsap.timeline({
        scrollTrigger: {
            trigger: element,
            start: dataTrigger + " " + dataScroller,
            markers: dataMarkers,
            toggleActions: "play none none none",
            toggleClass: "scrolled"
        }
    })

    timeline.from(element, {
        opacity: 0,
        delay: dataDelay,
        duration: dataDuration
    })
})




// **************************************************
//  GSAP - Scroll - Slide Horizontal
// **************************************************
// Class:       animate-scroll-slide-horizontal
// Description: Slide in from left or right
// data-delay    = number (float)      - default is 0
// data-duration = number (float)      - default is 1
// data-trigger  = top, center, bottom - default is top
// data-scroller = top, center, bottom - default is bottom
// data-markers  = false, true         - default is false
// data-position = px, em, %, vh, etc  - default is "100px"
let animateScrollSlideHorizontal = gsap.utils.toArray('.animate-scroll-slide-horizontal')
animateScrollSlideHorizontal.forEach((element) => {
    let dataDelay = 0;
    if (element.dataset.delay) {
        dataDelay = parseFloat(element.dataset.delay)
    }

    let dataDuration = 1;
    if (element.dataset.duration) {
        dataDuration = parseFloat(element.dataset.duration)
    }

    let dataTrigger = 'top'
    if (element.dataset.trigger) {
        dataTrigger = element.dataset.trigger
    }

    let dataScroller = 'bottom'
    if (element.dataset.scroller) {
        dataScroller = element.dataset.scroller
    }

    let dataMarkers = false
    if (element.dataset.markers) {
        dataMarkers = element.dataset.markers
    }

    let dataPosition = "100px";
    if (element.dataset.position) {
        dataPosition = element.dataset.position
    }

    let timeline = gsap.timeline({
        scrollTrigger: {
            trigger: element,
            start: dataTrigger + " " + dataScroller,
            markers: dataMarkers,
            toggleActions: "play none none none",
            toggleClass: "scrolled"
        }
    })

    timeline.from(element, {
        x: dataPosition,
        opacity: 0,
        delay: dataDelay,
        duration: dataDuration
    })
})




// **************************************************
//  GSAP - Scroll - Slide Vertical
// **************************************************
// Class:       animate-scroll-slide-vertical
// Description: Slide in from top or bottom
// data-delay    = number (float)      - default is 0
// data-duration = number (float)      - default is 1
// data-trigger  = top, center, bottom - default is top
// data-scroller = top, center, bottom - default is bottom
// data-markers  = false, true         - default is false
// data-position = px, em, %, vh, etc  - default is "100px"
let animateScrollSlideVertical = gsap.utils.toArray('.animate-scroll-slide-vertical')
animateScrollSlideVertical.forEach((element) => {
    let dataDelay = 0;
    if (element.dataset.delay) {
        dataDelay = parseFloat(element.dataset.delay)
    }

    let dataDuration = 1;
    if (element.dataset.duration) {
        dataDuration = parseFloat(element.dataset.duration)
    }

    let dataTrigger = 'top'
    if (element.dataset.trigger) {
        dataTrigger = element.dataset.trigger
    }

    let dataScroller = 'bottom'
    if (element.dataset.scroller) {
        dataScroller = element.dataset.scroller
    }

    let dataMarkers = false
    if (element.dataset.markers) {
        dataMarkers = element.dataset.markers
    }

    let dataPosition = "100px";
    if (element.dataset.position) {
        dataPosition = element.dataset.position
    }

    let timeline = gsap.timeline({
        scrollTrigger: {
            trigger: element,
            start: dataTrigger + " " + dataScroller,
            markers: dataMarkers,
            toggleActions: "play none none none"
        }
    })

    timeline.from(element, {
        y: dataPosition,
        opacity: 0,
        delay: dataDelay,
        duration: dataDuration
    })
})




// **************************************************
//  GSAP - Scroll - Site Header
// **************************************************
const headerContainer = document.querySelector('#site-header .header-container')
const headerLogo = document.querySelector('.header-logo a')
ScrollTrigger.matchMedia({
    "(min-width: 540px) and (max-height: 1100px)": function() {
        const timeline = gsap.timeline({
            scrollTrigger: {
                trigger: 'main',
                start: "top 90%",
                toggleActions: "play none none reverse",
                onToggle: self => refreshScrollTrigger(),
                // markers: true,
            }
        });
        timeline.to(headerContainer, { paddingBlock: '20px' })
        .to(headerLogo, { width: '320px' }, '<')
    }
})

HTML

<header id="site-header">
    <div class="header-container">
        <div class="header-logo | animate-scroll-fade-in" data-delay="0.25" data-duration="1.5">
            <a href="/">
              <svg viewBox="0 0 138 26" fill="none" stroke="greenyellow" stroke-width="2.3" stroke-linecap="round" stroke-linejoin="round" title="CodePen"><path d="M15 8a7 7 0 100 10m7-8.7L33 2l11 7.3v7.4L33 24l-11-7.3zm0 0l11 7.4 11-7.4m0 7.4L33 9.3l-11 7.4M33 2v7.3m0 7.4V24M52 6h5a7 7 0 010 14h-5zm28 0h-9v14h9m-9-7h6m11 1h6a4 4 0 000-8h-6v14m26-14h-9v14h9m-9-7h6m11 7V6l11 14V6"></path></svg>
            </a>
        </div>

        <div class="header-nav | animate-scroll-fade-in" data-delay="0.5" data-duration="1.5">
            <nav class="nav-traditional" aria-label="Navigation Menu">
                <ul>
                    <li class="nav-item standard"><a href="/" class="nav-link">About</a></li>
                    <li class="nav-item standard"><a href="/" class="nav-link">Services</a></li>
                    <li class="nav-item standard"><a href="/" class="nav-link">Portfolio</a></li>
                    <li class="nav-item standard"><a href="/" class="nav-link">Blog</a></li>
                    <li class="nav-item standard"><a href="/" class="nav-link">Contact</a></li>
                </ul>
            </nav>
        </div>
    </div>
</header>

<section id="site-masthead" role="complementary" aria-label="Photo Slideshow" class="animate-scroll-fade-in" data-delay="0" data-duration="1.5">
    <div class="masthead-slideshow">
        <div class="masthead-content | animate-scroll-fade-in" data-delay="0.75" data-duration="1.5">
            <div class="masthead-content-container">
                <h1>Building Cool Things Since 1988</h1>
            </div>
        </div>

        <div class="glide">
            <img src="https://images.unsplash.com/photo-1635170870336-67546b863d13" alt="">
        </div>
    </div>
</section>

<main>
    <section class="content content-split">
        <div class="content-split-item content-split-text | animate-scroll-slide-horizontal" data-delay="0.25" data-trigger="100px" data-scroller="95%" data-position="-100">
            <h2>Site Content</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Distinctio mollitia nostrum, praesentium provident officia odit possimus commodi blanditiis fugiat animi recusandae, necessitatibus assumenda cumque vel, molestias quo.</p>
          <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Distinctio mollitia nostrum, praesentium provident officia odit possimus commodi blanditiis fugiat animi recusandae, necessitatibus assumenda cumque vel, molestias quo.</p>
        </div>

        <div class="content-split-item content-split-image | animate-scroll-slide-horizontal" data-delay="0.25" data-trigger="100px" data-scroller="95%" data-position="100">
            <img src="https://images.unsplash.com/photo-1542324253097-09d465f007bc" alt="">
        </div>
    </section>
  
  <section class="content content-small | animate-children-scroll-fade-in" data-scroller="95%"  data-delay="0.25">
    <div class="content-item">
      <h2>This is a heading 1</h2>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi ipsam architecto natus dolor eius voluptatem similique quod corporis quaerat, quos magnam voluptatibus sequi illo rem porro expedita voluptatum. Repellat, voluptatem.</p>
    </div>
    
    <div class="content-item">
      <h2>This is a heading 2</h2>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi ipsam architecto natus dolor eius voluptatem similique quod corporis quaerat, quos magnam voluptatibus sequi illo rem porro expedita voluptatum. Repellat, voluptatem.</p>
    </div>
    
    <div class="content-item">
      <h2>This is a heading 3</h2>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi ipsam architecto natus dolor eius voluptatem similique quod corporis quaerat, quos magnam voluptatibus sequi illo rem porro expedita voluptatum. Repellat, voluptatem.</p>
    </div>
    
    <div class="content-item">
      <h2>This is a heading 4</h2>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi ipsam architecto natus dolor eius voluptatem similique quod corporis quaerat, quos magnam voluptatibus sequi illo rem porro expedita voluptatum. Repellat, voluptatem.</p>
    </div>
  </section>
</main>

CSS

body {
  margin: 0;
  background-color: #000;
  color: #fff;
  font-family: 'Monterrat', sans-serif;
}

h1,
h2 {
  margin: 0 0 0.5em 0;
  line-height: 1;
}

h1 {
  font-size: 3rem;
  
  @media (max-width: 1300px) {
    font-size: 2rem;
  }
}

h2 {
  font-size: 2.5rem;
}

p {
  margin: 0 0 1em 0;
  font-size: 1.5rem;
  line-height: 1.75;
}

.text-center {
  text-align: center;
}




// **************************************************
//  Header
// **************************************************
header#site-header {
  position: sticky;
  top: 0;
  width: 100%;
  z-index: 10000;
  background-color: #323233;
}




// **************************************************
//  Header Container
// **************************************************
.header-container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  gap: 2em;
  max-width: 1920px;
  margin-inline: auto;
  padding-block: 2.25em;
  padding-inline: 1em;

  @media (max-width: 1360px) {
    flex-direction: column;
    gap: 0;
    padding-block: 1em;
  }
}




// **************************************************
//  Header Logo
// **************************************************
.header-logo {
  flex-grow: 0;
  flex-shrink: 1;
  flex-basis: auto;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;

  a {
    width: 420px;
    
    @media (max-width: 1360px) {
      width: 210px;
    }
  }

  img {
    display: block;
  }
}




// **************************************************
//  Header Nav
// **************************************************
.header-nav {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.nav-traditional {
  margin: 0;
  padding: 0;
  opacity: 1;
  display: block;

  >ul {
    display: flex;
    gap: 0.25em;
    margin: 0;
    padding: 0;
    list-style: none;
    
    @media(max-width: 540px) {
      gap: 0;
    }

    >li {
      position: relative;

      >a {
        display: block;
        padding-block: 0.6em;
        padding-inline: 1.2em;
        color: #fff;
        font-size: 1.4rem;
        font-family: 'Montserrat', sans-serif;
        line-height: 1;
        font-weight: 400;
        text-decoration: none;
        
        @media (max-width: 1360px) {
          font-size: 1rem;
        }
        
        @media(max-width: 540px) {
          padding-inline: 0.75em
        }
      }

      &.standard {
        >a {
          transition: all 0.5s;

          &:hover,
          &:focus{
            background-color: #5A5F73;
            color: #fff;
          }
        }
      }
    }
  }
}




// **************************************************
//  Masthead Slideshow
// **************************************************
#site-masthead {
  position: relative;
  background-color: #000;
}

.masthead-slideshow {
  .glide {
    max-height: 960px;
  }

  img {
    display: block;
    object-fit: cover;
    width: 100%;
    height: 100%;
    max-width: none;
  }
}

.masthead-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding-inline: 1em;
  z-index: 101;
  color: #fff;
  
  h1 {
    margin: 0 0 0.25em 0;
    text-align: center;
  }
} 




// **************************************************
//  Main
// **************************************************
main {
  padding-block: 5em;
  padding-inline: 1em;
}




// **************************************************
//  Content
// **************************************************
.content {
  max-width: 1920px;
  margin-inline: auto;
  padding-block: 2.25em;
  padding-inline: 1em;
  
  &.content-small {
    max-width: 1300px;
  }
}

.content-item {
  margin-bottom: 5em;
}

.content-split {
  display: flex;
}

.content-split-item {
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 50%;
}

.content-split-text {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.content-split-image {
  min-width: 0;

  img {
    display: block;
    width: 100%;
    height: auto;
    max-height: 600px;
    object-fit: cover;
  }
}

CodePen