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
ryan champin
16,836 PointsBetter event handling code?
So i have a click event. Basically when someone clicks my nav button, an overlay appears with the navigation menu, and the navigation items bounce in, in a stagger pattern. I'm getting tired of my event handlers looking like this, very sloppy. Is there a better, more object-oriented way?
var navState = 0;
$('svg, .hamburger').on('click', function(e){
if(navState === 0){
handleOverlay();
menuItems.velocity("transition.bounceLeftIn",{stagger: 100});
animatedOverlay.velocity({ opacity:1, zIndex:99999}, {duration: 300});
activeSlide.velocity({ opacity:0}, {duration: 200});
navState = 1;
}else if (navState === 1){
handleOverlay();
menuItems.velocity("transition.bounceRightOut",{stagger: 100});
animatedOverlay.velocity("reverse", {delay: menuItems.length*100+50});
activeSlide.velocity("reverse", {delay: menuItems.length*100+50});
navState = 0;
}
});
function handleOverlay() {
$header.toggleClass('animated').toggleClass('fix');
$('.name').toggleClass('slideUpOut');
}