简体   繁体   中英

How does Google achieve the fading effect on the home page?

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

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 .

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.

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!

Demo: PURE CSS http://jsfiddle.net/6QS2a/1/

</div>

css:

.item {   
  height:150px;
  width:150px;
  border-radius:100%;
  background:skyblue; 
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -ms-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
    opacity:0.2;
}

.item:hover {
  opacity: 1;
}

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM