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
Gabriel Ward
20,222 PointsAdding classes to an overlay
So I have a hamburger icon that transitions to an 'x' on the click of a button. When the button is clicked, an overlay with a nav appears. I'm trying to dynamically add some css transitions to the nav when it appears on the overlay. I want it to fade in and fade out with the overlay. At the moment I've got it so that the first time I click on the hamburger icon everything works properly. The overlay and nav fade in. Then when I click on the 'x' the overlay and nav fade out as they should. But after that, when I click on the hamburger icon, the overlay fades in as it should, but the nav fades out instead of fades in. Here's the jQuery code, the dynamic adding are the lines $('.wrap-nav').addClass('animated fadeInDown'); and $('.wrap-nav').addClass('animated fadeOutDown');
$(document).ready(function(){
$('#nav-icon1').click(function(){
$(this).toggleClass('open');
});
});
$(document).ready(function(){
var open = false;
$('#nav-icon1').on('click',function(){
if(open == false){
$('.overlay').fadeIn(200);
$('.wrap-nav').addClass('animated fadeInDown');
open = true;
} else {
$('.overlay').fadeOut(200)
$('.wrap-nav').addClass('animated fadeOutDown');
open = false;
}
});
$('.overlay').on('click', function(){
$('.overlay, .btn-close').fadeOut(200);
$('nav ul li, .btn-open').fadeIn(200);
open = false;
});
$('.wrap').on('click', function(){
return false;
});
})
2 Answers
LaVaughn Haynes
12,397 PointsI can't see your HTML or CSS so I don't know for sure... but if I had to guess, maybe it's because you are adding classes in your conditional when the condition is true but not removing it when it's not.
if(open == false){
$('.wrap-nav').removeClass('animated fadeOutDown').addClass('animated fadeInDown');
}else{
$('.wrap-nav').removeClass('animated fadeInDown').addClass('animated fadeOutDown');
}
Anyway, that's my best guess
LaVaughn Haynes
12,397 PointsThat's my job (though my new employer doesn't allow JavaScript on the site so I rarely use it anymore). I'm self taught though so I'm here starting from scratch in case I missed anything. I'd like to learn from the pros planning to work my way up to more advanced stuff like nodejs, express, and sencha.
(sorry, this was supposed to be a comment not an answer)
Gabriel Ward
20,222 PointsOh nice. Yeah I'm teaching myself with the intention of getting into the industry. How'd you come up with the solution here? Was it intuitive?
LaVaughn Haynes
12,397 PointsI was working on a project recently where I wanted some things to display differently if the user was scrolled down the page. I didn't want to modify the DOM a lot with JavaScript on the project so I added a "scrolled" class to the body if the page was scrolled past a certain point and removed it if it wasn't. This way I could do the changes with CSS. Example:
header{ color: #333; }
.scrolled header{ color: #0000ff; }
What you were doing reminded me of that
Gabriel Ward
20,222 PointsGabriel Ward
20,222 PointsThanks so much LaVaughn. That was it. I'm curious, do you work as a professional web developer?