Using JQuery to reveal using a slide up effect

The slideUp() method in JQuery is intended to hide the element rather than reveal it. It is basically the opposite of slideDown() which “unhides” your element by sliding it down.

As you can see on this blog, I have an “About Jason Gill” tab in my heading. When clicked, this tab reveals information about who I am by sliding upward. In figuring out how to do this, I of course consulted the Google gods and came up with a lot of people asking how, but few people providing answers. So, I’ve decided to post the solution I finally came up with hoping it will benefit others.

Basically, I am using two different effects to create what looks like one effect. What you are seeing is essentially a slideDown() in conjunction with an animate() effect. The animate() is literally moving the info box upward at the same speed it is sliding down (in my case, “slow”).  I just had to measure the height of the #about_me box and animate it upward exactly that many pixels (in my case it was 171 pixels). To close the box, I simply animate downward at the same rate I slideUp. Technically, I’m using animate in conjunction with slideToggle(), but you get the idea.

You can take a look at my source, but I’ll post the javascript here for your convenience:

$(document).ready(function(){
	var opened = false;
	$("#about_tab").click(function(){
		if(opened){
			$("#about_me").animate({"top": "+=171px"}, "slow");
		}else{
			$("#about_me").animate({"top": "-=171px"}, "slow");
		}
		$("#about_content").slideToggle("slow");
		$("#about_tab .close").toggle();
		opened = opened ? false : true;
	});
});

6 Responses  |  add yours »

6 Responses to “Using JQuery to reveal using a slide up effect”

  1. Atrus says:

    So simple, but yet so clever !
    Thanks a lot \o/

  2. Matt Johnson says:

    Fantastic example, nice work!

  3. Indialike says:

    Very nice and useful tutorials for web designers,
    Thanks for posting.

  4. b3nb says:

    Thank you very much! People like you help make online experience so much more engaging and fun.

  5. Tucker says:

    1 thing you could do to adapt it to anyones project even easier. would be to replace “171px” with $(‘ID’).height(), but otherwise, very nice.

  6. redcirce says:

    Been searching the web for days for a slideup vs slidedown div! Thanks so much! Wondered whether it would be possible to add a settimeout on it though, so that if it hasn’t been closed/clicked in say 7 seconds it closes automatically?

Leave a Reply

(if you want a pic to show with your comment, go get a gravatar!)

Search