Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

David Todd
David Todd
18,439 Points

I can't help but feel I'm doing this wrong (CSS animations / jQuery) ...

I'm working on a project for a client where I'm re-vamping a scrollable header they have on their site, currently it uses parallax scrolling to play through a few animations. The client and I think it's a little gimmick-y and complicated for the user so I'me starting from scratch.

But here's the thing, I'm using CSS animations which are working really well; to advance to the next slide I have more customs CSS animations.

To achieve this I'm using jQuery to addClass-es which trigger the pre-defined CSS animation keyframes, this is working okay but it seems very code heavy and complex, the more I add the harder it is to manage and bug fix.

I'm fairly far into the project now and as I'm working alone I just thought I'd ask whether it sounds like I'm on the right track or would there be issues with browser compatibility etc?

Any advice would be appreciated.

2 Answers

Youssef S
Youssef S
4,467 Points

I'm not sure if you're just asking about jQuery vs CSS animations or general code maintenance as well, I'll try answering both with the best to my knowledge. Hope this helps.

Here goes, so when it comes to animations: as long as you're avoiding the jQuery animate function and animating using CSS animations and transitions then you're on the safe side when it comes to heavy usage on the DOM. Refer to this short article on CSS3 vs jQuery Transition Speeds.

If you're talking about general code maintenance then always try to align yourself to design principles, such as DRY - Don't repeat yourself, YAGNI - You aren't Gonna Need it ... The bigger and more complex the project you're working on, the more efficient you'll have to work on writing your code. So for future reference, you can trace it easily and understand all the operations leading to whatever result you're working on.

Always make sure your code is well formatted and easy to read. Don't copy, paste chunk of codes and alter it for different purpose. Try to put them in methods/functions and link to them with whatever variables and thus applying that chunk of code on them and returning the result accordingly. Interesting read about when writing code

I've been where you at before and I always find myself from time to time lost when I'm trying to maintain my own code. Just try to get others to read your code and watch how they interpret it, are they constantly going up and down trying to understand where is x and how did it reach y. Or is it in a smooth hierarchal flow with a clear implemented algorithm.

Here's a different take on the css vs js animations that you may be interested in.

http://css-tricks.com/myth-busting-css-animations-vs-javascript/

David Todd
David Todd
18,439 Points

Thanks very much for your reply, thankfully it is all CSS animation-lead rather than jQuery .animate, I think I'll spend some time converting my CSS animation structures into Sass. My code is unbelievably 'Wet' at the moment and it's doing me no favours. I guess some time spent now putting it into Sass will be made up later in the project. Thanks for the re-assurance.

Youssef S
Youssef S
4,467 Points

Yes, Sass is a great approach to organize everything and apply the principles accordingly. Don't worry about CSS animations browser support, it's widely supported now in all modern browsers. Just keep it prefixed and you're good to go CSS Animations Compatibility. Good luck in your coding.