【问题标题】:How to change color of text from left to right along with the background?如何随背景从左到右更改文本颜色?
【发布时间】:2016-01-27 06:25:04
【问题描述】:
我有一个元素,当悬停时,从左到右逐渐改变背景颜色。但是,我不仅想改变背景颜色,还想改变文本的颜色,完全同时/速度。我怎样才能做到这一点?
这是我的相关 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.
【问题讨论】:
标签:
css
css-transitions
css-gradients
【解决方案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>