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
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsjQuery -> slideToggle()
Hi guys,
I have a quick question regarding the slideToggle() function of jQuery:
Looking through the API it is possible to run a function when the animation completes, but I was trying to do something in reverse -> run a function before the animation starts.
My question is how to do it?
I would like to run .css() before the animation and change some CSS values before the animation starts.
Any help would be really helpful :)
Jeremy Germenis
29,854 PointsHow are you calling the slideToggle? onClick, ect...
miguelcastro2
Courses Plus Student 6,573 PointsThe API supports callbacks for when the slideToggle() starts and completes. Listed in the .slideToggle( options ) section. Here is an example:
$('a.btn').slideToggle({
start: function () {
alert("Starting");
},
complete: function () {
alert("Completed");
}
});
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 Points@Sergio Alen -> I want to use .css() before the animation starts to change back the css properties of the selected object.
Jeremy Germenis -> I'm using .click()
2 Answers
Jeremy Germenis
29,854 PointstoggleClass() is what you need then. http://api.jquery.com/toggleclass/
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsAnd that my friend is the right answer:)
Didn't see this API doc. Thank you!
Jeremy Germenis
29,854 Pointsif you are trying to target the element you are toggling you can:
$( "#id" ).click(function() {
$('.slideMe').css('background','red').slideToggle();
});
if you are targeting something other than the element your are toggling you can:
$( "#id" ).click(function() {
$('.somethingElse').css('background','red');
$('.slideMe').slideToggle();
});
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsJeremy Germenis -> Thanks for taking the time to reply.
But I wrote that code before I asked the question here.
$(".lang-btn").click(function(){
$(this).css({
'border-bottom-left-radius': '0',
'border-bottom-right-radius': '0'
});
$(".lang-list").slideToggle("slow");
});
This is my js code. As you can see when you click the button the bottom borders change on it and the list is slideToggled (initial value of the list is "display:none"). When you click it the second time it slides back up and hides but the border values are not changed.
It want it to slide back up on the second click and then the border would change to the previous value.
Sergio Alen
26,726 PointsSergio Alen
26,726 Pointsdepends on the animation i guess? can you be more specific?