Make mootools Fx.Slide close on load

If you havn't already, check out mootools to see the code used here.

I'm using the Fx.Slide tool, but I don't want it to open/expand until the user clicks on the link.

To do this, you must edit the javascript for Fx.Slide, and add some css for the elements.

Here is the javascript code that is provided on the site:

<a id="toggle" href="#">toggle</a>
var mySlide = new Fx.Slide('test');
 
$('slidein').addEvent('click', function(e){
e = new Event(e);
mySlide.slideIn();
e.stop();
});

Note: you need to enclose this in a domready event. You can find this in the mootools documentation, or below:

window.addEvent('domready', function() { <!-- function here--> });

Here is my edited code. Basically, what is happening here is I'm setting a style for toggle1 (name of the div to hold content) to set visibility to hidden and height to 0px (otherwise you'll be left with a big blank space)

<a id="toggleSwitch1" href="#">toggle</a>
#toggle1 {
    visibility: hidden;
    height:0px;
    margin-left:15px;
}

The javascript then calls Fx.Slide('toggle1') when it is loaded, to close the div. Then, when the user clicks the link, it sets visibility to visible and height to auto.

window.addEvent('domready', function() {
   
    var mySlide = new Fx.Slide('toggle1');
    mySlide.toggle();
    $('toggleSwitch1').addEvent('click', function(e){
        e = new Event(e);
        $('toggle1').setStyle('visibility', 'visible');
        $('toggle1').setStyle('height', 'auto');
        mySlide.toggle();
        e.stop();
    });
   
});

It's a hack, I know. But a good one :)

Comments
Andy Matthews's Gravatar Example is missing.
# Posted By Andy Matthews | 02/04/08 20:26
Will Wilson's Gravatar Thanks for pointing that out. I've included it as a downloadable file
# Posted By Will Wilson | 02/04/08 21:26
John's Gravatar You don't need this.

Simply write:

var mySlide = new Fx.Slide('test').hide();

Thats it.
# Posted By John | 22/04/08 23:25
Shaun's Gravatar Hmmm....why the do the sounds of noisy crickets suddenly start to echo ;-)

This just re-iterates the fact that I need to read comments before jumping the gun on fixes. Thankyou John!
# Posted By Shaun | 01/05/08 06:43
benn's Gravatar Nice work, I am trying to make it horizontal , it works but only 1 time and the slider kepps the height even when I slide out,

cold you please help ,

thnx
# Posted By benn | 18/06/08 18:08
Bert's Gravatar Great! just needed this thanks man!
# Posted By Bert | 17/07/08 14:41
James's Gravatar nice work I have been searching for this one for quite a while now by the way I was just wondering if how could I make it slide up not down anyone please thanks
# Posted By James | 13/08/08 17:11
james's Gravatar as you can see I have a test page here click toggle and it slides down I need it to slide up anyone got an idea thanks
# Posted By james | 13/08/08 17:19
Will Wilson's Gravatar @ James

Replace 'slideIn' with 'slideOut'. That should do the trick. See the code on mootools.net, it has all the functions on there
# Posted By Will Wilson | 28/09/08 14:58
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.