Reputation: 11458
When I use slideDown my box just appears and doesn't slideDown when I first click on the link, but every subsequent click, it works as intended:
Upvotes: 0
Views: 2453
Reputation: 15338
box.removeClass("hide");
must be after slideDown
complete demo : http://jsfiddle.net/3Gc6Q/9/ :
$(".link").click(function(e) {
e.preventDefault();
var box = $("#test");
if (box.hasClass("hide")) {
box.slideDown(function(){box.removeClass("hide");});
}
else {
box.slideUp(function() {
box.addClass("hide");
});
}
});
Upvotes: 3
Reputation: 41862
Just remove
box.removeClass("hide");
The call to slideDown
will make the div visible. Currently, when you remove the 'hide' class, it will show (since it is being hidden by the css for that class) and the slideDown
call then does nothing. If you don't remove the class, it will be shown as you want.
Alternatively, you can remove the class when the animation is complete:
box.slideDown(function() {
box.removeClass("hide");
});
Upvotes: 0
Reputation: 7863
Try using slideToggle()
$(".link").click(function(e) {
e.preventDefault();
var box = $("#test");
box.slideToggle('slow');
});
This will drastically simplify what you've got there. It will also allow you to remove some of the unnecessary CSS you've got on on the "test" div
.
Here is a working example: http://jsfiddle.net/zYAcB/1/
Upvotes: 0