我已经在 h1 元素上实现了悬停效果(请参阅下面的代码和笔),但该效果在鼠标移出时表现异常,并且在返回其原始状态之前有点闪烁。
有什么想法可以让它像在悬停时淡入一样平滑地过渡回原始颜色吗?
提前致谢。
https://codepen.io/lobodemon/pen/YOXKNJ
h1 {
transition: 0.5s;
}
h1:hover {
background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
background-size: 400% 400%;
color:transparent;
-webkit-background-clip: text;
background-clip: text;
-webkit-animation: Gradient 15s ease infinite;
-moz-animation: Gradient 15s ease infinite;
animation: Gradient 15s ease infinite;
}
@-webkit-keyframes Gradient {
0%, 100% {
background-position: 0 50%
}
50% {
background-position: 100% 50%
}
}
@-moz-keyframes Gradient {
0%, 100% {
background-position: 0 50%
}
50% {
background-position: 100% 50%
}
}
@keyframes Gradient {
0%, 100% {
background-position: 0 50%
}
50% {
background-position: 100% 50%
}
}
<h1>The Title</h1>
最佳答案
问题是您正在尝试将动画与过渡结合起来,但它不会像您预期的那样工作。通过添加过渡,您不会使动画变得流畅。换句话说,您不能向动画添加过渡。
由于您将悬停时的动画持续时间设置为 15s
,我认为在这种情况下不需要无限,因为没有人会持续悬停超过 15s
,所以你可以把它改成一个过渡,它会很平滑。
我已经将黑色添加到渐变中以获得我们的初始状态,然后通过过渡我们可以完成一半的初始动画,最后您将拥有 7s
持续时间,这在某种程度上足以悬停效果:
h1 {
transition: 7s;
background:
linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB,#000);
background-size: 400% 400%;
color:transparent;
-webkit-background-clip: text;
background-clip: text;
background-position: 0 0;
}
h1:hover {
background-position: 100% 50%;
}
<h1>The Title</h1>
关于CSS 文本背景悬停效果在鼠标移出时表现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51968018/