DEV Community

Discussion on: Should I use Javascript or Css for creating animations?

Collapse
 
drmandible profile image
DrMandible

If asked, you should probably be able to do whatever you're trying to do with javascript. But usually javascript is easier than css for anything beyond basic functionality anyways, so that shouldn't be a problem.

But take something like a simple color change or fade in animation. That should be done with css unless you can point to a specific reason why you need javascript. The reason is simple: css should be used for the "view" of your site whenever possible.

If someone else looks at your code (or if you come back after a long break), they'll go to the css first when they have a view issue they want to address. If they discover that a simple animation has been brought into the javascript, that might be a real problem because now they'll also need to learn about your js code. Best practice states that your js code is the "logic" of your page, so fiddling with that might have dire repercussions that might not be immediately obvious.

On the other hand, maybe this animation is based on the user's profile which also requires spinning up a server. And maybe that animation is part of an interactive user experience, like a character in a game that moves across the screen. Now we're getting into the realm of "logic" and it starts to become more appropriate to use js. I've seen simple games made entirely in css/html. But those are fun proofs of concept and often involve unusual usages of those tools which can cause serious headaches if you ever need to do more than the tools can be stretched into.

The most honest answer I can give is this: practice, practice, practice. Pick a task that you're not sure how to do and do it whichever way you can figure out. Maybe that means javascript. Once you're done, go back and try it with just html/css. Or maybe doing it the first time made you realize it would be impossible with html/css. Even in the 21st century, experience is still by far the best teacher. I know that was long winded but I hope it helps.