Jump to content
Search Community

matchMedia()

danb92 test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Can anybody tell me why this doesn't work?
Just trying to get cards to fade in from different directions depending on the screen size. The first animation works fine but the second not at all.
How am I supposed to set up different behaviours for the same elements depending on the screen size?
Thanks in advance!
Daniel
 

    let mm = gsap.matchMedia();
    mm.add('(min-width: 800px)', () => {
      ScrollTrigger.create({
        trigger: '#my-story',
        start: 'top 30%',
        end: 'bottom bottom',
        scrub: true,
        animation: tlLarge,
      });
    });
 
    mm.add('(max-width: 799px)', () => {
      ScrollTrigger.create({
        trigger: '#my-story',
        start: 'top 30%',
        end: 'bottom bottom',
        scrub: true,
        animation: tlMed,
      });
    });
Link to comment
Share on other sites

Just in case it's relevant I'll add the animations too..

 

    //Large screen box directions
    let tlLarge = gsap.timeline();
    tlLarge.from('#t-c1', {
      opacity: 0,
      y: 100,
      filter: 'blur(30px)',
    });
    tlLarge.from('#t-c2', {
      opacity: 0,
      y: 100,
      filter: 'blur(30px)',
    });
    tlLarge.from('#t-c3', {
      opacity: 0,
      y: 100,
      filter: 'blur(30px)',
    });
 
    //Medium screen box directions
    let tlMed = gsap.timeline();
    tlMed.from('#t-c1', {
      opacity: 0,
      x: -100,
      filter: 'blur(30px)',
    });
    tlMed.from('#t-c2', {
      opacity: 0,
      x: 100,
      filter: 'blur(30px)',
    });
    tlMed.from('#t-c3', {
      opacity: 0,
      x: -100,
      filter: 'blur(30px)',
    });
Link to comment
Share on other sites

  • Solution

Hi,

 

Is really hard to find out what the issue could be without a minimal demo.

 

Most likely this has to do with the fact that you are creating two different animations that affect the same targets and the same properties. Is better to create the animation inside each MatchMedia callback:

mm.add('(max-width: 799px)', () => {
  let tlMed = gsap.timeline();
  tlMed.from('#t-c1', {
    opacity: 0,
    x: -100,
    filter: 'blur(30px)',
  });
  tlMed.from('#t-c2', {
    opacity: 0,
    x: 100,
    filter: 'blur(30px)',
  });
  tlMed.from('#t-c3', {
    opacity: 0,
    x: -100,
    filter: 'blur(30px)',
  });
  ScrollTrigger.create({
    trigger: '#my-story',
    start: 'top 30%',
    end: 'bottom bottom',
    scrub: true,
    animation: tlMed,
  });
});

And follow the same pattern with the large screen animation.

 

Hopefully this helps.

Happy Tweening!

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