views:

77

answers:

7

Hi,

I am doing an Ajax call and on response I would like to hide a div. Right now, I am able to do it successfully, but that is kind of quick. I want some fade out effect on it. How to do it in one single shot?

Here is my current code.

var someDiv = document.getElementById(someId);
someDiv.style.display="none";

Thank you very much in advance!

+1  A: 

Use jQuery...

$('#someId').fadeOut();

And here's the API Reference in case you need to modify anything about the fadeOut effect:

.fadeOut() - jQuery API

Justin Niessner
Hehe, ok you were first ;)
MyGGaN
how bulk is jQuery?
Bragboy
1.4 is 70.4Kb Minified.
Justin Niessner
i just need this one effect. Is there a way to use a simple funciton or something instead of pulling the entire jQuery in ?
Bragboy
Try this one: http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation
MyGGaN
@Bragaadeesh - See @strager's answer. I prefer jQuery simply because there are enough other mundane Javascript tasks that it can handle that it makes it worth the little extra overhead.
Justin Niessner
The reason I had asked for that is because I thought it would be simple. Just looking at the length of the fade out code without using jQuery forces me to use it. Thank you guyz for your tips!! Much appreciated!
Bragboy
A: 

I could recommend using a framework (I prefer jQuery). It will also handle cross platform support.

$('#div-id-here').fadeOut('slow');

For more info consult their documentation: jQuery fadeOut

MyGGaN
A: 

How about resorting to jquery? All you'll need is a '.fadeOut()' and you're good to go

Jonn
A: 

Have a look at the jQuery fadeOut function.

Justin Ethier
+4  A: 

Nice to see all five answers so far refer to jQuery.

There are several articles for creating a fade effect using vanilla Javascript, though be weary of the Javascript most people publish online.

strager
@strager: +1 thanks for the link, very interesting simple example.
Marco Demajo
+3  A: 

If you want to forgo jQuery or frameworks, this is a pattern you can use:

function fadeThisElement(elm, interval) {
  for (var i=10; i>0; i--) {
    var opacity = i/10;
    setTimeout( function(opacity) {
      elm.setStyle("-moz-opacity",opacity);
      elm.setStyle("opacity",opacity);
      elm.setStyle("filter","alpha(opacity=" + (opacity*100).toString() );
      //set your alpha values for the various browsers
    }, interval;
  }
}

Give the interval in milliseconds. I suggest 10 for a 10-step fade.

Robusto
A: 

I'll throw in the MooTools version for good measure:

$('myid').fade(opacity)

where opacity is a value in the interval [0,1]. You can also call it as:

$('myid').fade('out')

There are more options available. MooTools is 65kb in size (YUI compressed).

awesomo