2016-01-26 52 views
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; 
} 

Here's the fiddle.

+0

這可能是有用的https://jsfiddle.net/suunyz3e/2168/ – omer

回答

1

一個posibility是使用背景剪輯:文本。但不幸的是,支持非常差。

.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 { 
 
\t background-position: 0% top, 0% top; 
 
}
<div class="test">TEST</div>

+0

謝謝,這正是我想要的! –

相關問題