Jump to content
Search Community

How use ScrollTrigger.matchMedia() in my case?

Nickicool test
Moderator Tag

Go to solution Solved by ZachSaucier,

Recommended Posts

I have blocks on the page that are loaded dynamically (ajax). All blocks have a parameter that specifies the name of the function with GSAP animation.

<section class="lazyload" id="examples" data-gsap-function="blockExamples"></section>
<section class="lazyload" id="faq" data-gsap-function="blockFaq"></section>
// this functions run by events (like click or after load content with ajax)
var gsapFunctions = {
  blockExamples: function () {
    gsap.from("#examples img", {
      // ...
    });
  },

  blockFaq: function () {
    gsap.utils.toArray("#faq .item").forEach((item) => {
      var timeline = gsap.timeline({});
      timeline
      //...
    });
  }
};

After loading the block content, I run the function that contains the code for animating this block. In the Codepen example, I emulated loading Ajax content by clicking on a button.

Everything works well. But now I need to make alternative functions for some blocks with animations for the mobile/desktop version. I don't understand how this can be done in my case. I mean, I would like to keep the ability to record animations for different blocks in separate functions. Perhaps this is the wrong approach - please show me how to do it correctly.

See the Pen xxOZwjK by nickicool (@nickicool) on CodePen

Link to comment
Share on other sites

You mean that I should write my own custom handling of animation selection and resize control, instead of using the construction (?):

 

    ScrollTrigger.matchMedia({
        // desktop
        "(min-width: 800px)": function() {},
        // mobile
        "(max-width: 799px)": function() {},
        // all
        "all": function() {
        }
    });

 

Link to comment
Share on other sites

I am not a very experienced programmer, please help me with the right direction of thoughts. Do I understand correctly or not that the animation selection should be implemented inside each function (and not outside of them):

    var gsapFunctions = {
        blockExamples: function () {
        //...
        },

        blockFaq: function () {
          //...
        }
    }

If I understand correctly, when resizing, I should kill the previous animation (anim.kill(); or aim.progress(0).kill()). To do this, I need access to the animation/timeline object... Am I right? In my code, functions don't return animation objects, and I don't have access to them.

Link to comment
Share on other sites

  • Solution

As with most things there are multiple ways of doing this. Since all of your animations use ScrollTriggers then you can use ScrollTrigger's matchMedia method just fine inside of your functions. Or you could handle it yourself using JavaScript's matchMedia. 

 

You also need to make sure that you're not adding multiple ScrollTriggers to the same elements. You could do that by either restricting the creation of new ScrollTriggers to just the new elements or you could kill off the old ScrollTriggers and then create new ones for all the related elements.

 

Here's how I might set it up, restricting the creation of new ScrollTriggers to just the new elements:

See the Pen gOMPBqO?editors=0010 by GreenSock (@GreenSock) on CodePen

 

Also please use the "fork" button when making new versions of your demo on CodePen. That way the thread makes sense for future readers.

  • Like 2
  • Thanks 1
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...