views:

461

answers:

6

If you go to google.com, you notice the menu on top slowly appears once you have mouse over the page. I was wondering what does Google use to control the fading effect?

[edit] since I don't use jQuery, I don't want to include it just to use this feature

A: 

I would think that they set the initial opacity of the elements other than the search box to zero. When the mouseover event is fired, the elements' opacity is gradually increased to 1.

Edit: In code it would look something like this:

var hiddenStuff = document.getElementByClassName("hiddenStuff");

var interval=document.setInterval(function() {
    for (var i=0; i<hiddenStuff.length;i++){
        hiddenStuff[i].style.opacity+=0.1
    }
    if (hiddenStuff[1].style.opacity===1){
        window.clearInterval(interval);
    }
}, 100);

You may need to tweak the parameters to get a smooth animation.

Lenni
I think the author of the question already knows that.
Ciwee
+2  A: 

You could use jQuery and add an onmousemove callback on the tag that fades a hidden div with id "mymenu" in, something like:

$("html").one("mousemove", function() {
 $("#mymenu").fadeIn("slow")
});

Warning: this was typed here, so I dunno if it compiles ootb.

Viktor Klang
+5  A: 

There are two ways.

Javascript

Works in most browsers.

Gradually change the CSS opacity attribute of an element using Javascript. That's easiest with a good framework like jQuery, but is quite simple to do yourself.

function fadeIn() {
    var element = document.getElementById("someID");
    var newOpacity = element.style.opacity + 0.05;
    element.style.opacity = newOpacity;
    if (newOpacity < 1) {
        window.setTimeout(fadeIn, 50);
    }
}

Pure CSS

Only supported in Webkit at the moment.

#someID {
    opacity:0;
    -webkit-transition: opacity 1s linear;
}
#someID:hover {
    opacity:1;
}

For an example have a look at the Surfin' Safari blog.

Georg
A: 

This is actually a rather complex thing to do because of the cross browser differences. The basics are something like the following:

  1. Get the current opactity of the element as float.
  2. Determine the ending opacity as float.
  3. Determine your rate speed - i dont know what this should be in raw terms - somthing like .01/ms maybe?
  4. Use a setInterval to fire a function that increases the opacity by your rate where: setInterval(function(){myElement.style.opacity = parseFloat(myElement.style.opacity)+0.01;}, 1); Somewhere in ther though you need to check if youve reached the endpoint of your animation and shutdown your interval.
prodigitalson
+1  A: 

I've never looked at it, but it's only logical to assume that there's a timer that gets started at load time for the page, and that adjusts either the alpha for the specified element or the opacity of another element that overlays it, in that element's CSS. Every timer tick, the numbers get turned up/down a little and the text becomes a bit more legible. When full visibility is reached, the timer is turned off.

JQuery is a finished, ready to use implementation of this in a cross-platform compatible package. You just add it, stir it up and it's done.

If you choose not to take the advice of the other answers, you'll have to research and implement the strategy from my top paragraph on your own. Good luck!

Carl Smotricz
A: 

@Georg, that example works on Firefox 3.5 too. :-)

The Elite Gentleman
Look here, I posted a comment, you could too!
Marius
Unfortunately I can't Marius, I don't have such priviledges yet.
The Elite Gentleman