Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements

Posted January 25, 2010 at 9:58 AM

Tags: Javascript / DHTML

jQuery makes the creation of dynamic user interfaces easier than ever before. A few of the convenience methods that I use every single in my web application development are slideDown() and slideUp(). These are perfect for showing and hiding content in a non-jarring manner. The slideDown() method shows content by sliding open a given element; the slideUp() method, on the other hand, hides content by sliding closed a given element. These are very basic methods, but I describe them here in order to point out that sometimes, I get so attached the name of a given method, I lose sight of what it's actually doing.

 
 
 
 
 
 
 
 
 
 

I fell victim to this narrow-sightedness just last week. I had an element with a fixed position in which its position was defined using the, "bottom," CSS property. Essentially, I had a bottom window user interface panel. Now I didn't want this panel to be shown all the time; sometimes it would need to be out of sight, and sometimes, I wanted it to slide up onto the screen. I was so used to sliding things DOWN with jQuery, that when it came to sliding things UP, I was so out of context that I actually started to use the animate() method to achieve the slide-up effect.

After I got this approach to work, including the offset calculation and re-positioning that I needed to perform prior to slide-up, I had a "well d'uh" moment! It suddenly dawned on me that the methods "slideDown" and "slideUp" weren't just for "down" and "up" directions, respectively - they were for "open" and "close" actions in general. That's when I realized that the slideDown() method could be used to open an element in an upward direction.

 Launch code in new window » Download code as text file »

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>jQuery SlideDown() / SlideUp() With Bottom-Positioned Elements</title>
  • <style type="text/css">
  •  
  • #container {
  • bottom: 0px ;
  • display: none ;
  • left: 20px ;
  • position: fixed ;
  • width: 90% ;
  • }
  •  
  • #inner {
  • background-color: #F0F0F0 ;
  • border: 1px solid #666666 ;
  • border-bottom-width: 0px ;
  • padding: 20px 20px 100px 20px ;
  • }
  •  
  • </style>
  • <script type="text/javascript" src="jquery-1.4.js"></script>
  • <script type="text/javascript">
  •  
  • // When the DOM is ready, initialize the scripts.
  • jQuery(function( $ ){
  •  
  • // Get a reference to the container.
  • var container = $( "#container" );
  •  
  •  
  • // Bind the link to toggle the slide.
  • $( "a" ).click(
  • function( event ){
  • // Prevent the default event.
  • event.preventDefault();
  •  
  • // Toggle the slide based on its current
  • // visibility.
  • if (container.is( ":visible" )){
  •  
  • // Hide - slide up.
  • container.slideUp( 2000 );
  •  
  • } else {
  •  
  • // Show - slide down.
  • container.slideDown( 2000 );
  •  
  • }
  • }
  • );
  •  
  • });
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • jQuery SlideDown() / SlideUp() With Bottom-Positioned Elements
  • </h1>
  •  
  • <p>
  • <a href="#">Show Div With SlideDown()</a>
  • </p>
  •  
  • <div id="container">
  • <div id="inner">
  •  
  • Check it out! This DIV is positioned based on its
  • very sexy Bottom and then is opened using a slide-down
  • animation effect (and closed using a slide-up)
  • animation effect.
  •  
  • </div>
  • </div>
  •  
  • </body>
  • </html>

Once I had this realization, it made so much sense that I was, in fact, more than a little embarrassed that it didn't occur to me sooner. This is just a good reminder to myself to always stop and think about what I'm trying to do philosophically, and not just concentrate on which combinations of methods I need to call in order to accomplish a given task.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jan 25, 2010 at 10:12 AM // reply »
78 Comments

I've been making use of this functionality over at my freelance site:

http://commadelimited.com/


Jan 25, 2010 at 10:18 AM // reply »
7,572 Comments

@Andy,

Cool effect! Yeah, I feel so silly that I had confusion about this :)


Jan 25, 2010 at 10:25 AM // reply »
18 Comments

Yeah, that's pretty confusing. Rather than try to keep track in your head that up really means down, you might consider aliasing those methods.

$.fn.slideOpen = $.fn.slideDown;
$.fn.slideClose = $.fn.slideUp;


Jan 25, 2010 at 11:24 AM // reply »
14 Comments

What about slideToggle()? It will automatically show or hide based upon visibility. Does this fail under certain circumstances?

$(function(){
$('a').click(function() {
$('#container').slideToggle();
});
});


Jan 25, 2010 at 11:43 AM // reply »
7,572 Comments

@Patrick,

That's an interesting idea; but, the reality is that the slideDown and slideUp are very descriptive names for the methods. The issue was not with the method name so much as it was with my understanding of what it was doing.

@James,

I was going to go with slideToggle() at first, but I wanted to have the slideDown() and slideUp() method calls being made explicitly in the code (to help the conversation).


Jan 25, 2010 at 1:18 PM // reply »
5 Comments

It sounds to me as if these methods would have been better named slideOpen(); and slideClose(); :)


Jan 25, 2010 at 9:04 PM // reply »
7,572 Comments

@Jeff,

Maybe. I think because the down/up approach is the 99% use case for what they are, that would make sense. Someone could come along and make a slideRight() and slideLeft() method... but again, these are outliers.


Feb 9, 2010 at 3:09 AM // reply »
2 Comments

I came across this page because I was searching for a certain behavior. I didn't just want the element to slide up; I wanted it's contents to slide up with it. As if the contents were written on a piece of paper that you were sliding up or down. Instead slideUp/Down/Toggle methods change the height of the element shrinking or expanding the element and in the usual case simply cutting off the contents as it gets smaller.

On Jeff's website mentioned above it gives the effect I want: the content slides up as if it were attached to something. However, I want my content to slide from the top not the bottom.

So i found a way to do it by placing my container element within an overflow:hidden wrapper and using the animate method to slide the element up using negative margin. (this is what google is doing on their adwords dashboard gadgets)

Here's the code, sorry for the long post just thought it might help someone looking for the same behavior. :)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
.panel { width: 100px; background: #f3f3f3; border: solid #333; border-width: 0px 1px 1px 1px; padding: 1em; }
.dispensor { width: 100px; background: #ccc; border: 1px solid #333; padding: 1em;}
</style>
</head>
<body>
<div class="dispensor">
Dispensor
</div>
<div style="overflow: hidden">
<div class="panel">
<ul>
<li>apple</li><li>bear</li><li>cookie</li></ul>
</div>
</div>
<p>
some stuff below...</p>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$(document).click(function () {
if ($('.panel').is(':hidden')) {
$('.panel').show();
$('.panel').animate({ marginTop: '0px' });
}
else {
$('.panel').animate({ marginTop: '-' + $('.panel').height() + 'px' }, function () {
$('.panel').hide();
});
}
});
});
</script>
</body>
</html>


Feb 9, 2010 at 3:16 AM // reply »
2 Comments

Just a quick fix for the code example above:

$('.panel').height()

should instead be...

$('.panel').outerHeight()


Feb 9, 2010 at 7:50 AM // reply »
7,572 Comments

@Thomas,

Not bad. I suppose you could do the same with Top as well as margin.


Feb 13, 2010 at 4:18 AM // reply »
2 Comments

Ben you are my Hero !
I also found this confusing at first, but not now after I watch this video...

thank you Ben


Feb 25, 2010 at 1:58 AM // reply »
1 Comments

thanks ben good effect


Mar 4, 2010 at 8:24 PM // reply »
1 Comments

Awesome. Thanks. I just used it.


Mar 8, 2010 at 7:22 PM // reply »
7,572 Comments

@Muhammadiq, @Sabaqahmad, @Derrick,

Glad you guys like it. It's trickier to change the mind set of what the term, "slideDown", actually means than it is to implement.


Mar 12, 2010 at 1:50 PM // reply »
1 Comments

That's exactly the situation that came to my mind the other day. Saved my life there! xoxo


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 22, 2010 at 7:43 AM
Terms Of Service / Privacy Policy Document Generator
Thankyou for this very helpful form. You've made my life much easier today. I'll have a look around your site... I'm sure there's some more good stuff here..Thanks Dave ... read »
Mar 22, 2010 at 7:21 AM
Encountered "(. Incorrect Select Statement, Expecting a 'FROM', But Encountered '(' Instead, A Select Statement Should Have a 'FROM' Construct.
I got this exception now. In case you're using var-es local struct, CF gives you couple of "new" exceptions: Encountered "local. and Encountered "id. Incorrect Select List, Incorrect select colum ... read »
Mar 22, 2010 at 3:08 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Thanks for the response. I finally discovered that I was getting this error because I had cfsetting enablecfoutputonly="yes" in Application.cfc, and was neither setting it to false elsewhere nor brac ... read »
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »