views:

174

answers:

8

Is there easier way to make something to appear slowly on webpage? I tried to incrementally increase opacity of the CSS, but it (opacity) tag is different for each browser. Is there well know Javascript function that everyone uses? Or is there any css tag?

[edit] Thanks for Jquery suggestion. Is there other option? My page is very small and don't want to add Jquery. (I know about Google hosting it)

+1  A: 

If you can use jQuery then animate() function will hep you.

$('your_selector').animate({opacity: 0.25}, 5000, function() {
    // Animation complete.
});

See .animate()

or you can use fadeIn

$('your_selector').fadeIn('slow', function() {
        // Animation complete
      });
rahul
A: 

Have a look at the JQueryUI library - that has functionality for this...

Martin Milan
Where? Links? (To documentation describing that specific feature)
David Dorward
I didn't have time to go hunting through the documentation myself - so I just threw in an answer that would lead the user to it.-2 for that?Anyway - other people have now posted the link.
Martin Milan
+7  A: 

Easy with Jquery:

$("#myElementID").fadeIn("slow");
+1  A: 

You fadein and fadeout of jQuery. For example, jQuery('#ID').fadeout() will make an element with 'ID' as its id to fade out (slowly disappear),

NawaMan
A: 

I recommend using jQuery. You will have to use the fadeIn() function. Detailed explanation here: http://api.jquery.com/fadeIn/. I rarely use pure Javascript after having started with the jQuery library.

Marcos Buarque
+2  A: 

YUI Also has animate functionality and allows you to only include required modules which will reduce your load times.

YUI Animation

+12  A: 

Aargh! Every JS developer here seems to have contracted the jquerytis!
If you're not yet infected or still want to escape from it, here's a small function that do the job cross browser :)

function appear(elm, i, steps, speed){
    var t_o;
    i = i || 0;
    steps = steps || 5;
    speed = speed || 50;

    t_o = setInterval(function(){
        opacity = i / 100;
        i = i + steps;
        if(opacity > 1 || opacity < 0){
            clearInterval(t_o);
            return;
        }
        elm.style.opacity = opacity;
        elm.style.filter = 'alpha(opacity=' + opacity*100 + ')';
    }, speed);
}

To appear

appear(document.getElementsByTagName('DIV')[0], 0, 5, 40);

To disappear

appear(document.getElementsByTagName('DIV')[0], 100, -5, 40);
Mic
+1 for "jqueritis"
Jaanus
`style['-moz-opacity']` wouldn't work. As a JS property name the hyphens are transformed to capitals: `style.MozOpacity`. However, `-moz-opacity` hasn't been needed since Firefox 0.9... you can certainly drop it today, as every browser except IE supports the plain CSS3 syntax.
bobince
@bobince corrected
Mic
@Mic I edited and changed `clearTimeout` to `clearInterval`
Josh Stodola
@Ghommey not anymore ;)
Mic
@josh thanks for the cleaning
Mic
+2  A: 

Dear Lord! Yes, I think most of us do know about jQuery, thanks.

Opacity isn't so complicated to set, today; it's now only IE that needs special help. See this answer for some simple plain-JavaScript time-independent fading code.

bobince