0

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

这是我的相关 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;
}

这是小提琴。

4

1 回答 1

1

一种可能性是使用背景剪辑:文本。但不幸的是,支持很差。

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

于 2016-01-26T22:25:04.463 回答