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

后端 未结 7 2171
猫巷女王i
猫巷女王i 2021-01-14 06:27

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?

相关标签:
7条回答
  • 2021-01-14 06:48

    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.

    0 讨论(0)
  • 2021-01-14 06:54

    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.
    0 讨论(0)
  • 2021-01-14 06:57

    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;
    }
    
    0 讨论(0)
  • 2021-01-14 06:58

    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!

    0 讨论(0)
  • 2021-01-14 07:01

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

    0 讨论(0)
  • 2021-01-14 07:02

    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.

    0 讨论(0)
提交回复
热议问题