Jump to content
Search Community

ScrollTrigger timeline break on mid-scrolling when window resize or refreshing page.

raaan127

Go to solution Solved by Rodrigo,

Recommended Posts

Posted

Hello, I am learning to use gsap and struggle to resolve this issue.

I create these for testing:

- a wrapper with original height on load is 1440px, that will change height to 0 when scrolling from .cover to .about

- a scale-box which will scale from 100vh to 0 base on wrapper height / 1440

- an item of 100px * 100px on bottom of scale-box

If I refresh the page on mid-scrolling between .cover and .about, or resize the browser window, the height which is controlled by gsap timeline is bugged and change and when I scroll up to top it won't return to original size (1440px)

Somehow [Sign Up and Log In to CodePen is unavailable]  so I post the code in here.


<header>
    <nav class="navbar">
        <div class="hero-name-wrapper">
            <div class="hero-name-scale-box">
                <div class="hero-name"></div>
            </div>
        </div>
    </nav>
</header>

<main id="smooth-content">
    <div class="header-background"></div>
    <!-- Cover Section -->
    <section class="section cover">
        <div class="cover-content">
        </div>
    </section>
    <!-- About Section -->
    <section class="section about">
        <div class="about-content">
        </div>
    </section>

</main>
/* ================= INITIALIZE ================= */
:root {
  /* ----- COLORS -----*/
  --debug-red: #f005;
  --debug-yellow: #ff05;
  --debug-green: #0f05;
  --debug-cyan: #0ff5;
  --debug-blue: #00f5;
  --debug-magenta: #f0f5;

}
*,
*:before,
*:after {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  list-style: none none;
  text-decoration: none;
}

html {
  box-sizing: inherit;
  scroll-behavior: smooth;
  height: -webkit-fill-available;
}

body {
  font-family: "Roboto", sans-serif;
  font-size: 26px;
  font-weight: 300;
  height: -webkit-fill-available;
  background-color: var(--background);
  color: var(--primary);
}

h1{
  font-weight: 800;
  text-transform: uppercase;
  font-size: clamp(92px, 10vw, 300px);
  font-weight: bold;
  margin: 0;
}

/* =================== HEADER =================== */
header {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10;
  width: 100%;
  height: auto;
  margin: 0 auto;
  transition: all 0.35s ease;
  /*background-color: var(--debug-blue);*/
}


.navbar {
  width: 100%;
  transition: all 0.4s ease-in-out;
  position: relative;
}

/* ==================== NAME ==================== */

.hero-name-wrapper{
  position: fixed;
  background-color: var(--debug-green);
  width: 2560px;
  height: 1440px;
  border-radius: 100px;
  z-index: 100;
  pointer-events: none;
}
.hero-name-scale-box{
  background-color: var(--debug-red);
  min-height: 100px;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  border-radius: 200px;
}
.hero-name{
  background-color: yellow;
  width: 200px;
  height: calc(285 * (100vh/1440) );
  min-height: 64px;
  margin: 0 auto;
  filter: drop-shadow(0px 0px 12px rgba(0, 0, 0, 0.15));
  transform: rotate(0deg);
}


/* ==================== COVER =================== */
.cover{
  position: relative;
  display: flex;
  justify-content: center; /* Horizontally */
  align-items: flex-start; /* Vertically */
  overflow: hidden;
  z-index: 1;
}

.cover-content{
  position: relative;
  width: 100vw;
  height: 100vh;
}

/* ==================== ABOUT =================== */
.about {
  position: relative;
}
.expander{
  height: 100vh;
  background-color: #1b6d85;
}
let vh = window.innerHeight;
window.addEventListener("resize", () => {
    vh = window.innerHeight;
});
window.addEventListener('DOMContentLoaded', () => {
// ======================================== //
// ============== INITIALIZE ============== //
    // Register ScrollTrigger plugin from GSAP
    gsap.registerPlugin(ScrollTrigger);
    const header = document.querySelector("header");
    const heroWrapper = document.querySelector(".hero-name-wrapper");
    const heroBox = document.querySelector(".hero-name-scale-box");
    const heroName = document.querySelector(".hero-name");

    let resizeObserverInitialized = false;

    function resizeHeroBox() {
        if (!heroBox || !heroBox.parentElement) return;
        const parent = heroBox.parentElement;
        const parentHeight = parent.offsetHeight;
        const vh = window.innerHeight;
        const boxHeight = parentHeight * (vh / 1440);
        heroBox.style.height = `${boxHeight}px`;

        // Initialize observer only once
        if (!resizeObserverInitialized) {
            // Observe parent height changes
            const observer = new ResizeObserver(() => resizeHeroBox());
            observer.observe(parent);
            // Update on viewport resize
            window.addEventListener('resize', resizeHeroBox);
            resizeObserverInitialized = true;
        }
    }

    function setupHeroNameAnimations() {
        if (!heroWrapper) return;
        let tl = gsap.timeline({
            scrollTrigger: {
                trigger: ".cover",
                start: `top top`,
                endTrigger: ".about",
                end: `top top`,
                pin: "none",
                pinSpacing: false,
                markers: false,
                scrub: true,
                invalidateOnRefresh: true,
            }
        })
        tl
            .to(heroWrapper, {height: 0, ease: "none"}, 0)
            .to(heroName, {height: 84, ease: "none"}, 0);
    }


    // function calling
    resizeHeroBox();
    setupHeroNameAnimations();
    ScrollTrigger.refresh();

    window.addEventListener("resize", () => {
        // resizeTestBox();
        ScrollTrigger.refresh();
    });
})

 

GSAP Helper
Posted

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen.

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import GSAP as shown in the Install Helper in our Learning Center : 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Posted
2 hours ago, GSAP Helper said:

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

I just created stackblitz accound and still dont figured out how to make the demo yet but here is the files. .https://stackblitz.com/edit/stackblitz-starters-eo8skmlv?file=script.js

I tested and it can run on local.

 

Idea: Yellow box stay on bottom center of viewport, .cover is scale based on view height. yellow box will move to top when scrolling from .cover to .about

The issue happens when window resize on mid-scroll, 

It can only reset to the right height when scroll up to the top then resize window again. Please check video on attachment.

  • Solution
Posted

Hi,

 

The problem stems from this:

html {
  box-sizing: inherit;
  scroll-behavior: smooth;
  height: -webkit-fill-available;
}

Basically scroll-behavior: smooth on the <html> tag translates in some issues with the markers position when ScrollTrigger refreshes. In order to set everything correctly, ScrollTrigger sets the scroll back to zero so everything is at the initial natural position, but scroll-behavior: smooth actually doesn't allow the scroll position to be set immediately, is like a CSS transition for the scroll position, so when ScrollTrigger makes the calculations again, the scroll position is not actually 0 so all the calculations are off.

 

Just add this to your CSS:

html {
  scroll-behavior: auto !important;
}

 

Finally there is no need for this:

window.addEventListener("resize", () => {
  ScrollTrigger.refresh();
});

ScrollTrigger has it's own internal mechanism to detect window resize events and it debounces it (for better performance), which ultimately executes the refresh method in order to update the ScrollTrigger instances.


Hopefully this helps

Happy Tweening!

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...