Two other solutions to make responsive animations
Unlock this video and the rest of this course by joining Frontend.FYI PRO .
In the previous lesson we’ve learned the best way to make your animations responsive. However, the solution I showed you might not have been the first solution you thought of.
In this lesson I want to briefly touch on two other solutions you could use, and the situations in which they might not work as well.
Using JavaScript
Your first idea for making responsive animations, might be to use JavaScript. You could for example use a hook like useMedia
to detect the current breakpoint, and then apply the correct styles.
On line 6 you see we use the hook to get a boolean value that tells us if the screen is currently wider than 600 pixels. Then on lines 39 and 40 we use this value to apply the correct styles.
And in theory that should do the trick. Take a look at the example and see if you can spot any things that are off. Try to make the animation loop, or trigger the animation again via the buttons. Resize the window too.
Resize the preview by dragging the handle in the center. Depending on the breakpoint, the animation should change.
Unlock the full lesson with PRO
Want to read the full lesson? Join Frontend.FYI PRO.
PRO is a one time purchase of €149 that gives you lifetime access to this course, any courses released in the future, and so much more!