Jump to content
Search Community

Update variable inside gsap matchMedia on every resize | ScrollTrigger

Praneet Dixit test
Moderator Tag

Go to solution Solved by Cassie,

Recommended Posts

I have a basic gsap animation set up with matchMedia.

ScrollTrigger.matchMedia({
    "(min-width: 700px)": function () {
        let containerWidth = parseInt(getComputedStyle(document.querySelector(".see-through")).width);
        let tl = gsap.timeline({
            scrollTrigger: {
                / ....    
            }
        });
        tl.to(".logo-transparent", {
            x: containerWidth/2,
            scale: "0.8"
        });
    },
    "(max-width: 699px)": function () {
        let containerHeight = parseInt(getComputedStyle(document.querySelector(".see-through")).height);
        let tl = gsap.timeline({
            scrollTrigger: {
                / ....
            }
        });
        tl.to(".logo-transparent", {
            y: containerHeight/2,
            scale: "0.9"
        })
    }
});

Now what I want is to update the values of containerWidth and containerHeight on each resize event. I tried adding an eventListener for resize event and updating the variables, but it still has no effect on the animation.

Perhaps the animation is not updated on each resize unless the media breakpoints change.

What approach can I follow for the desired effect?

Link to comment
Share on other sites

@Cassie Here it is: 

See the Pen ZExQNaV by PraneetDixit (@PraneetDixit) on CodePen

Steps to recreate issue:

1. Resize window so that the width is less than 700px.

2. On scrolling, the box should slide to center along the x-axis.

3. Now resize the window so that its width remains within the 700px limit.

4. On scrolling, the box will not slide perfectly into the center as the value of `containerWidth` is not updated.

Link to comment
Share on other sites

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...