views:

3393

answers:

4

I am trying to fade in a hidden element, and then fade it back out again using mootools.

I can't just use $('my_div').fade('toggle'), because that assumes the element is always visible, whereas my div starts out with display:none.

Is there a simple way to combine a fade in/out with a slide in/out or some other way to achieve a nice effect?

+1  A: 

Start out with opacity:0 and display:block. That way you can use fade()

bucabay
I don't want to have a huge block of blank space in the middle of my page.
Brian Ramsay
what do you mean? the blank space is where the element will fade in. If you don't want space, then make the width and height 0 at start and tween that also. figure it out dude...
bucabay
See the second sentence is actually an answer that might work.
Brian Ramsay
+3  A: 

I almost always use Fx.Reveal in Mootools.More:

http://mootools.net/docs/more/Fx/Fx.Reveal

Very nice fade-in animation, almost no effort on your part. Fx.Slide might also do the trick, although it tends to be more fiddly.

If you don't want to use Mootools.More, then rolling your own solution with Fx.Morph to change both height and opacity simultaneously could also do the trick.

One Crayon
+1  A: 

I do this: I don't hide the element from CSS (if you have used «display: none» or «visibility: hidden», please remove them before trying what I'm suggesting). Instead, in «domready», I use «fade('hide')» to hide the element. This way, I can later apply «fade('in')» and «fade('out')» to it.

Ali
A: 

I prefer using display: none as well. You can just use this code when you want to fade the element:

To fade in:

$('my_div').setStyle('display', 'block');
$('my_div').fade('in');

and to fade out:

$('my_div').fade('out');
$('my_div').setStyle('display', 'none');

Alternatively, you could just setup a class that is called .hide with display: none set in it, and put that class on your element to start with. Then it makes the code easier:

$('my_div').toggleClass('hide');
$('my_div').fade('toggle');
Shawn Steward