如何随背景一起从左到右改变文字颜色?

时间:2016-01-26 13:19:45

标签: css3 css-transitions css-gradients

我有一个元素,当悬停时,会逐渐从左到右改变背景颜色。但是,我不仅要改变背景颜色,还要改变文本的颜色,完全同时/节奏。我怎样才能做到这一点?

这是我的相关CSS:

.left-to-right {
  color: white;
  background-size: 200% 100%;
  background-image: linear-gradient(to right, #011228 50%, #FF7800 50%);
  transition: background-position 1s;
}

.left-to-right:hover {
    background-position: -100% 0;
    color: #011228;
}

Here's the fiddle.

1 个答案:

答案 0 :(得分:1)

一个可能性是使用background-clip:text。但遗憾的是,支持率很低。



.test {
  height: 66px;
  width: 200px;
  background-size: 200% 100%;
  background-image: linear-gradient(to right, lightblue 50%, blue 50%), linear-gradient(to right, green 50%, lightgreen 50%);
  transition: background-position 6s;
    text-indent: 28px;
    font-size: 60px;
    background-size: 200% 100%;
    background-repeat: no-repeat;
    background-position: 100% top, 100% top;
    -webkit-background-clip: text, border-box;
    background-clip: text, border-box;
    color: transparent;
}

.test:hover {
	background-position: 0% top, 0% top;
}

<div class="test">TEST</div>
&#13;
&#13;
&#13;