Jump to content
Search Community

Working with responsive animation on NextJs

therealbeez test
Moderator Tag

Recommended Posts

I am currently working on a nextjs app and I am wondering what the correct, production-ready method is for creating animations that act differently on different screen sizes. My go to method used to be 'window.innerWidth' for the js bits, but that doesn't appear to work with ssr. So what would be the optimal way to do this?

Link to comment
Share on other sites

Yeah, it's tough to troubleshoot without a minimal demo, but you could probably use conditional logic like:

if (typeof(window) !== "undefined") {
  // do stuff that references window...
  if (window.innerWidth < 800) {
    //...
  } else {
    //...
  }
}

@iDad5 I think in some environments, the code actually runs multiple times - initially it runs server-side where there's no window object at all and that's where it's throwing errors for @therealbeez (I'm totally guessing here). But when when it runs in the browser (again), all the GSAP animation magic happens. 

  • Thanks 1
Link to comment
Share on other sites

8 hours ago, GreenSock said:

Yeah, it's tough to troubleshoot without a minimal demo, but you could probably use conditional logic like:

if (typeof(window) !== "undefined") {
  // do stuff that references window...
  if (window.innerWidth < 800) {
    //...
  } else {
    //...
  }
}

@iDad5 I think in some environments, the code actually runs multiple times - initially it runs server-side where there's no window object at all and that's where it's throwing errors for @therealbeez (I'm totally guessing here). But when when it runs in the browser (again), all the GSAP animation magic happens. 

Ok, thanks for the explanation, guess I just assumed, that if would make no sense to let fron-end animation code run at the server at all, but I'm really not familiar with that stuff.

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