如何通过 CSS3 动画淡入背景图像 [英] How to fade in background image by CSS3 Animation

查看:35
本文介绍了如何通过 CSS3 动画淡入背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

#nav li:hover {颜色:透明!重要;文字阴影:无;背景颜色:rgba(255, 0, 0, .5);-webkit-transition:全 1s 缓入;-moz-transition:全 1s 缓入;-o-transition:全 1 缓入;-ms-transition:全 1s 缓入;过渡:全 1 缓入;}

这是在线版本:

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.

#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/

解决方案

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: 0 and animate that. Something like.

<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/

这篇关于如何通过 CSS3 动画淡入背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆