How to make a Slide and Hide with Yahoo YUI

Javascript transitions are becoming more and more popular with the Web 2.0 sites. We simply couldn’t use an Ajax powered site without some sort of visual notifications, and although animation is not absolutely neccessary the sites that do use it stand out from the crowd.

There are quite a few transitions covered in the Yahoo Design Pattern Library but I’m going to show you my way of Sliding and Hiding a div. Unlike the design pattern library, I’m not going to tell you why the effect should be used, but exactly how to create it.

Before we get started, you might want to look at the finished product, so you know what the goal of this is.

Overview of the process

When I wanted tp make this transition for a page of mine, as a programmer I thought about how the div would dissapear:

  • The div would maintain size and hide beneath another div with a higher z-index or
  • The div would decrease in size, height or width depending on the orientation of the transition.

Although hiding the slider div beneath another div might have worked, what would happen if the slider moved off the visible screen area? You’d get those nasty scrollbars in the browser window…

So the second option sounded better. But (yes so many buts) what happens to the content of the div when the height/width shrinks? Yes, it moves with it. The solution for that is a small CSS property: overflow:hidden;

That’s a good start, but it’s still nto perfect. For example, when shrinking the width of a div, the content (in the case of text) would wrap as much as possible before it will overflow. It looks tacky believe me.

I’ve found that you can wrap the sliding div with another div, and set the width of the inner div, while setting overflow:hidden on the outer div. If you didn’t quite catch that, have a look at this diagram:

Slide and Hide Diagram

The outer div compresses to the dimensions specified by the transition, while the inner div overflows because the width is set.

Have a look at the source of the example for CSS and Javascript.

Technorati: yahoo, yui, javascript, css, slide, hide


Share this Post:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Slashdot
  1. written by James February 8th, 2007 at 02:34 | #1

    I am curious as to how to change the speed of the div opening and closing.

    Thanx


  2. written by James February 8th, 2007 at 02:38 | #2

    I am also curious as to how to set one sliding div behind another without interfering one or another when they open and close. I have tried many things including the z-index but to no avail.

    This is some great code. Thanx for posting it.

    James


  3. written by jc February 8th, 2007 at 08:42 | #3

    Firstly, the speed of the transition is found in the animation variable declaration:
    “var anim = new YAHOO.util.Anim(‘slider’,{width:{to:0}},1,YAHOO.util.Easing.easeIn);”
    Where the 3rd value is 1, that is the time in seconds for the animation. This can be a decimal value aswell. There are other transition types aswell besides “YAHOO.util.Easing.easeIn”. These are:

    * YAHOO.util.Easing.backBoth
    * YAHOO.util.Easing.backIn
    * YAHOO.util.Easing.backOut
    * YAHOO.util.Easing.bounceBoth
    * YAHOO.util.Easing.bounceIn
    * YAHOO.util.Easing.bounceOut
    * YAHOO.util.Easing.easeBoth
    * YAHOO.util.Easing.easeBothStrong
    * YAHOO.util.Easing.easeInStrong
    * YAHOO.util.Easing.easeNone
    * YAHOO.util.Easing.easeOut
    * YAHOO.util.Easing.easeOutStrong
    * YAHOO.util.Easing.elasticBoth
    * YAHOO.util.Easing.elasticIn
    * YAHOO.util.Easing.elasticOut

    Try them out to see the effect.

    As for the 2nd question, about sliding a div behind another: I prefer not to use this approach of ‘hiding’ one div behind another and applying a z-index higher for the static div. It can have unpredictable results between browsers. This is why I opt to ‘squeeze’ the div’s height or width and allowing the contents of this div to overflow and hide itself. So in effect the div isn’t moving, it’s just shrinking.


  4. written by james May 14th, 2009 at 01:35 | #4

    Hi,

    how do i change the direction of the animation so that it scrolls out from the left to the right?

    many thanks


  5. written by james May 14th, 2009 at 01:58 | #5

    @james

    please ignore my last comment – figured it out in the css. thanks