我希望有人能帮助解决这个问题。
我正在使用 HTML5 CSS3 开发搜索栏,这是工作示例http://jsfiddle.net/4Mr6D/
text-decoration: none;
transition: all 400ms;
-webkit-transition: all 400ms;
-moz-transition: all 400ms;
-o-transition: all 400ms;
从第 164 行或我评论“搜索结果”的地方开始 我试图让渐变背景在悬停时动画化,它似乎只能在卷展时动画回到原始颜色。
我尝试过使用背景图像来制作动画,但这不起作用。然后我转而使用关键字“all”,但这不起作用。
到目前为止,只有文本颜色会产生动画。有人可以帮助我找出我在使背景渐变也具有动画效果时做错了什么吗?
编辑: 即使在现代网络中,您也无法直接对
background-image
的 CSS 渐变进行动画处理,因为 CSS 颜色属性不能直接进行动画处理,background-image
也不能直接进行动画处理。因此,下面的解决方案(或类似的解决方案,例如对跨越比元素本身更大的尺寸的渐变的背景位置进行动画处理)是唯一的方法。
背景图像不可设置动画。由于 CSS 中的渐变使用背景图像,因此您无法像文本颜色那样使用到新图像的过渡。
话虽如此,如果下拉列表中只有文本,您可以使用伪元素并为其不透明度设置动画。以下是如何执行此操作的示例:
.container {
width: 500px;
height: 300px;
color: white;
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #333333), color-stop(100%, #141413));
background-image: -webkit-linear-gradient(top, #333333, #141413);
background-image: -moz-linear-gradient(top, #333333, #141413);
background-image: -ms-linear-gradient(top, #333333, #141413);
background-image: -o-linear-gradient(top, #333333, #141413);
background-image: linear-gradient(top, #333333, #141413);
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
position: relative;
z-index: 0;
}
.container:after {
content: "";
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #59aaf4), color-stop(100%, #338cdf));
background-image: -webkit-linear-gradient(top, #FF0000, #770000);
background-image: -moz-linear-gradient(top, #FF0000, #770000);
background-image: -ms-linear-gradient(top, #FF0000, #770000);
background-image: -o-linear-gradient(top, #FF0000, #770000);
background-image: linear-gradient(top, #FF0000, #770000);
position: absolute;
top: 0;
left: 0;
opacity: 0;
width: 100%;
height: 100%;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
z-index: -1;
}
.container:hover {
color: white;
}
.container:hover:after {
opacity: 1;
}
<div class='container'>Example Text!</div>
另外,我会删除您所拥有的 900 毫秒延迟(我在演示中做到了)。对于用户来说,这真的很烦人,他们想要立即反馈,而不是稍后反馈。
附注您应该像我一样将代码本地化到相关部分,以获得更快、更好的响应。没有人愿意通过 300 行代码来解决一个只需要 20 行的问题。
特别感谢 vals 使用 z-index 对其进行改进。
抱歉,伙计,但尚不支持渐变图像的过渡。是的,我也觉得这很疯狂。
但还是有希望的:这是一个 fiddle 的解决方法。您可以将搜索输入的背景设置为主要颜色,并在悬停时将其转换为辅助颜色。通过这种方式,您可以获得看起来像渐变正在过渡的颜色过渡效果。
为了清晰起见,简化示例:
.search {
background: primary_color;
background-image: linear-gradient(top, primary_color, secondary_color);
transition: background 1s ease-out;
}
.search:hover {
background: secondary_color;
}