Home > Javascript, Web Development > Maintaining Dynamic Height on a Fx.Slide element with Mootools

Maintaining Dynamic Height on a Fx.Slide element with Mootools

List Central uses mooTools for it’s JavaScript framework. I love it! It makes developing in JavaScript a pleasure, with increased reliability, and nifty eye candy to play with.

I recently started using FX.Slide in the next iteration of the List Central interface. It lets the user open and close “sliders” (div’s with content in them) as they want them. Check out the < a href="http://demos.mootools.net/Fx.Slide">mooTools demo to see FX.Slide in action.

I had one little problem with Fx.Slide though. The problem was that I wanted to be able to dynamically add content to an Fx.Slide, changing the height of the slider div after it has been opened. To my dismay, the implementation of Fx.Slide set the height of the div to specific numbers in pixel to achieve the vertical slider effect. This means that any content I added to the bottom of the div after it was opened would not be shown. In order for the height of the slider div to remain dynamic, I needed the height to be set to “auto”, not a specific number.

I had to figure out a way to maintain the the div’s ability to change in height while still using FX.Slide. I did some fiddling in mooTools source to try to achieve the effect I desired. I changed where FX.Slide set the height of the div to “auto”. After trying it out, I was astonished that it worked! I had moved on to some other development when I realized that my change to the mooTools source brock the fluid motion of my slider. It now opened all choppy and jumpy. It was no longer pleasing to the eye. I had to put the source back the way I found it. I may have learned some about the inner workings of mooTools though the process, but really, messing with the framework’s source is a path to trouble.

As with many problems, this one’s solution didn’t come to me until I put the problem away for a while, and worked on other things. The solution then came to me, and like many other solutions, it seemed so simple, once I knew it.

The trick to maintain dynamic height on a Fx.Slide is to add an onComplete function on to the slider that sets the height of the slider to “auto” if the slider is open, like this:


var formSlide = new Fx.Slide('FormSlider', {
        onComplete: function(){
            if(this.wrapper.getStyle('height') != "0px"){
                // Check if the slider is open
                this.wrapper.setStyle('height', 'auto');
            }
        }
    }).hide();

That’s it! It works like a charm now, just as I want it to. So much trouble, for such a simple solution!

I expect that this solution would work the same on the width for horizontal FX.Slide’s, though I haven’t tested it.

You might also like...

  1. Michael Leach
    January 12th, 2010 at 06:20 | #1

    Marilyn,

    I’d just like to say a big thanks for this article as I had the exact same requirement and had just tried a few (failed) attempts at fixes before I hopped on to Google and found this.

    This is one of those ‘tips and tricks’ that I’ll never forget and will use time after time.

  2. winston
    February 15th, 2010 at 12:33 | #2

    thanks,

    but i think that the height will stay stuck at auto – thus preventing any subsequent toggles from working.

    • February 15th, 2010 at 12:44 | #3

      The height auto doesn’t break the sliders functionality. Subsequent toggles work fine. Try it out.

  3. winston
    February 15th, 2010 at 13:08 | #4

    err i did try …. height is stuck at auto … which code segment will reset it back to 0px ?

    • February 15th, 2010 at 13:20 | #5

      I see where the problem is. I had difficulty with the toggle function before the height auto entered the picture. I opted to use slideIn and slideOut with a function I wrote like called doSlider. Here is the relevant code:

      function doSlider(e, slider){

      if(e != “”){
      e.stop();
      }

      if(slider.open == true){
      // Close slider if open
      formSlide.slideIn();
      }else{
      // else, open it
      formSlide.slideOut();
      }
      }

      function initSlider(){
      var slider = new Fx.Slide(‘Slider’, {
      duration: 400,
      onComplete: function(){
      if(this.wrapper.getStyle(‘height’) != “0px”){
      // Prevents it breaking on close
      this.wrapper.setStyle(‘height’, ‘auto’);
      }
      }
      }).hide();

      return Slider;
      }

      var slider = initSlider();
      $(‘sliderButton’).addEvent(‘click’, function(e){
      doSlider(e, slider);
      });

  4. February 20th, 2010 at 09:43 | #6

    Thanks, you saved me lot of nerves, things around me and hours :)

  5. August 5th, 2011 at 12:57 | #7

    Well, as the other posters mentioned, this code breaks the subsequent slides from working.

    Reading the docs for Fx.Slide, I see mention of the ‘resetHeight’ property, which seems to be exactly what we want here. Alas, setting it to true doesn’t do anything for me… :/

  6. August 5th, 2011 at 14:51 | #8

    I just noticed that the party ended over a year ago :)

    Nevertheless, the non-working resetHeight option I mentioned is actually a bug that was already fixed in newer MooTools (see https://mootools.lighthouseapp.com/projects/24057/tickets/558-fxslide-resetheight-option-does-not-work-in-1311).

  7. hajbaji
    December 7th, 2011 at 19:30 | #9

    thank you for this! it works well with mootools 1.11.
    good job ! :)

  1. No trackbacks yet.