CSS 如何通过 CSS3 动画淡入背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16905621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:05:50  来源:igfitidea点击:

How to fade in background image by CSS3 Animation

cssanimationbackground-imagecss-transitionscss-animations

提问by Alex Jj

I am making a menu that each item has a text like item1, item2 and etc. in hover the background colour changes, text becomes transparent and a background image replaces. I used this code to ease in and out the style. but it only works for background colour and not the image.

我正在制作一个菜单,每个项目都有一个文本,如 item1、item2 等。悬停时背景颜色发生变化,文本变得透明,背景图像被替换。我使用此代码来缓入和缓出样式。但它只适用于背景颜色而不是图像。

#nav li:hover {
    color:transparent !important;
    text-shadow: none;
    background-color: rgba(255, 0, 0, .5);

    -webkit-transition: all 1s ease-in;
    -moz-transition: all 1s ease-in;
    -o-transition: all 1s ease-in;
    -ms-transition: all 1s ease-in;
    transition: all 1s ease-in;
}

Here is the online version: http://jsfiddle.net/q4uHz/

这是在线版本:http: //jsfiddle.net/q4uHz/

回答by Explosion Pills

Background images cannot be animated; there would be no algorithm for that. A simple solution is to include <img>with the icon in your HTML instead with opacity: 0and animate that. Something like.

背景图像不能动画;没有算法可以解决这个问题。一个简单的解决方案是<img>在 HTML 中包含图标,而不是使用它opacity: 0并为其设置动画。就像是。

<li id="home">
<img src="http://cdn3.iconfinder.com/data/icons/picons-social/57/16-apple-48.png">
<a href="#home">Home</a></li>

#nav li {
    position: relative;
}
#nav li img {
    opacity: 0;
    position: absolute;
    transition: all 1s ease-in;
    top: 0;
    left: 0;
}
#nav li:hover img {
    opacity: 1;
}

http://jsfiddle.net/ExplosionPIlls/q4uHz/1/

http://jsfiddle.net/ExplosionPIlls/q4uHz/1/