简体   繁体   English

Google如何在主页上实现淡出效果?

[英]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. 如果你去google.com,你会发现当鼠标悬停在页面上时,顶部的菜单会慢慢显示。 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 [编辑]因为我不使用jQuery,我不想只是为了使用这个功能

There are two ways. 有两种方法。

Javascript 使用Javascript

Works in most browsers. 适用于大多数浏览器。

Gradually change the CSS opacity attribute of an element using Javascript. 使用Javascript逐渐更改元素的CSS opacity属性。 That's easiest with a good framework like jQuery , but is quite simple to do yourself. 对于像jQuery这样的好框架来说这是最简单的,但是自己做起来很简单。

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 纯CSS

Only supported in Webkit at the moment. 目前仅在Webkit中受支持。

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

For an example have a look at the Surfin' Safari blog . 举个例子来看看Surfin的Safari博客

You could use jQuery and add an onmousemove callback on the tag that fades a hidden div with id "mymenu" in, something like: 您可以使用jQuery并在标记上添加onmousemove回调,该标记将ID为“mymenu”的隐藏div删除,例如:

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

Warning: this was typed here, so I dunno if it compiles ootb. 警告:这是在这里键入的,所以我不知道它是否编译了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. 我从来没有看过它,但是假设有一个计时器在页面加载时启动,并且调整指定元素的alpha或覆盖它的另一个元素的不透明度,这是合乎逻辑的。元素的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. JQuery是一个完整的,可以在跨平台兼容包中实现的。 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/ 演示:PURE CSS http://jsfiddle.net/6QS2a/1/

</div>

css: 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. 触发mouseover事件时,元素的不透明度逐渐增加到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. 获取元素的当前不可行性为float。
  2. Determine the ending opacity as float. 将结束不透明度确定为float。
  3. Determine your rate speed - i dont know what this should be in raw terms - somthing like .01/ms maybe? 确定你的速度 - 我不知道这应该是什么原始术语 - 像.01 / ms这样的东西可能吗?
  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); 使用setInterval来触发一个函数,该函数通过以下值来增加不透明度: 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. @ Georg ,这个例子也适用于Firefox 3.5。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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