CSS 文本背景悬停效果在鼠标移开时表现异常

CSS text background hover effect behaving strangely on mouseout

我已经在 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>

使用过渡时,需要设置要更改的元素属性的初始状态。

h1 {
  background: black;
  -webkit-background-clip: text;
  background-clip: text;
}

我还发现了一个有趣的例子,效果和你的一样。 https://codepen.io/anthony-liddle/pen/uFoxA

问题是您正在尝试将动画与过渡结合使用,但这不会像您预期的那样工作。通过添加过渡,您不会使动画变得流畅。换句话说,您不能为动画添加过渡。

由于您将悬停时的动画持续时间设置为 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>