I'm looking for best practice answer. Currently I'm trying to add a dropdown menu to the navigation bar of my website. I've found that I could do it using CSS and that I could also do it using JS - Angular. I've read that because with JS malicious things can be programmed some people disable JS. Therefore to keep your websites functionality on more computers it should be programmed in CSS. Is there any merit to this? Should I avoid coding in JS when possible?
Here are few important reasons why you should prefer using CSS over JS.
However, like how they say, every advantage has its disadvantage. CSS animations don't give the freedom to perform any animation.
Animating the scale, rotation, and position of an element is incredibly common. In CSS, they're all crammed into one "transform" property, making them impossible to animate in a truly distinct way on a single element. For example, what if you want to animate "rotation" and "scale" independently, with different timings and eases? Maybe an element is continuously pulsing (oscillating scale) and you'd like to rotate it on rollover. That's only possible with JavaScript.
Reference: Myth Busting: CSS Animations vs. JavaScript
If you want the flexibility of JS animations and the speed of CSS animations, you should try GSAP, which is built on JavaScript and is much faster than CSS animations. Here is the performance test comparing CSS animations (using Zepto), jQuery animations and GSAP animations. Not to forget, the results may vary as per the rendering engine.
Conclusion: This is how you can go about it.
Simple animations, CSS achievable: Use CSS
Simple animations, can't achieve with CSS: Use JS
Heavy animations: Use GSAP or craft your animations with requestAnimationFrame.
Here is an amazing performance comparison between CSS animation and JS requestAnimationFrame implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With