2017-07-12 79 views
2

我得到了一個問題,我想創建一個CSS循環漸變動畫,但是當我這樣做動畫是非常靜態它不動畫順利下面是我目前正在努力的一個例子。希望你能幫助我。CSS循環漸變過渡

.slider1 { 
 
    background: background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(109, 0, 255)) !important; 
 
} 
 

 
/* css animation not smooth */ 
 
.slider1 { 
 
    opacity: .5; 
 
    animation: myfirst 5s; 
 
    -moz-animation: myfirst 5s infinite; /* Firefox */ 
 
    -webkit-animation: myfirst 5s infinite; /* Safari and Chrome */ 
 
} 
 

 
@-moz-keyframes myfirst { /* Firefox */ 
 
    0% { 
 
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(0, 174, 255)) 
 
    } 
 
    50% { 
 
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(135, 255, 0)) 
 
    } 
 
    100% { 
 
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(109, 0, 255)) 
 
    } 
 
} 
 

 
@-webkit-keyframes myfirst { /* Safari and Chrome */ 
 
    0% { 
 
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(0, 174, 255)) 
 
    } 
 
    50% { 
 
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(135, 255, 0)) 
 
    } 
 
    100% { 
 
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(109, 0, 255)) 
 
    } 
 
}
<div class="slider1"> 
 
    This some content 
 
    <div class="wsite-spacer" style="height:600px;"></div> 
 
</div>

+0

您可以刪除供應商前綴的屬性。它們適用於非常舊的瀏覽器。 –

+0

您無法爲此類漸變創建動畫。嘗試使用動畫背景位置 - > https://www.gradient-animator.com/或使用僞元素 - > https://medium.com/@dave_lunny/animating-css-gradients-using-only -css-d2fd7671e759 – sol

+0

謝謝,你能告訴我如何解決我的例子嗎?我讀過戴夫的文章,但我仍然無法弄清楚如何解決它在我的例子 – Cody

回答

2

基本上你gradint頂部有固定的顏色,並在底部的不同顏色。

如果您構建這個gradint爲2個不同的gradint重疊,那麼您可以將一個在底部,並在適當的位置創建一個從色彩變化的變化

div { 
 
    width: 400px; 
 
    height: 300px; 
 
    background-image: 
 
linear-gradient(to top, transparent, red),  
 
linear-gradient(to right, green, yellow, blue); 
 
    background-size: 100% 100%, 2000% 100%; 
 
    animation: move 5s infinite; 
 
} 
 

 
@keyframes move { 
 
from {background-position: center center, left center;} 
 
to {background-position: center center, right center;} 
 
}
<div></div>

+0

非常感謝這是最好的解決方案,像冠軍一樣工作!你太神奇了,非常感謝! – Cody

+0

有一件事我發現它只適用於Safari macos上的所有瀏覽器。我得到一個小故障,你知道如何解決它嗎? – Cody

+0

對不起,我無法在Safari上測試...你能解釋什麼是毛刺? – vals

0

你不能動畫background但你linear-gradient添加塊和改變他們的opacity

.slider1 { 
 
    opacity: .5; 
 
    position: relative; 
 
} 
 

 
.frame { 
 
    position: absolute; 
 
    left: 0; 
 
    right: 0; 
 
    top: 0; 
 
    bottom: 0; 
 
    z-index: -1; 
 
    /* animation for 3 block 5 second each, 3 × 5 = 15 */ 
 
    animation-duration: 15s; 
 
    animation-iteration-count: infinite; 
 
} 
 

 
.frame1 { 
 
    animation-name: hide1; 
 
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(109, 0, 255)); 
 
} 
 

 
.frame2 { 
 
    animation-name: hide2; 
 
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(135, 255, 0)); 
 
} 
 

 
.frame3 { 
 
    animation-name: hide3; 
 
    background: linear-gradient(rgba(173, 136, 255, 0.44), rgb(109, 0, 255)); 
 
} 
 

 
@keyframes hide1 { 
 
    0% { 
 
    opacity: 1; 
 
    } 
 
    33% { 
 
    opacity: 0; 
 
    } 
 
} 
 

 
@keyframes hide2 { 
 
    33% { 
 
    opacity: 1; 
 
    } 
 
    67% { 
 
    opacity: 0; 
 
    } 
 
} 
 

 
@keyframes hide3 { 
 
    67% { 
 
    opacity: 1; 
 
    } 
 
    100% { 
 
    opacity: 0; 
 
    } 
 
}
<div class="slider1"> 
 
    This some content 
 
    <div class="wsite-spacer" style="height:600px;"></div> 
 
    <div class="frame frame1"></div> 
 
    <div class="frame frame2"></div> 
 
    <div class="frame frame3"></div> 
 
</div>

+0

非常感謝你這是驚人的,我剛剛得到了最後一個問題,因爲我想應用此漸變循環到特定的滑塊(滑塊革命響應jQuery),並得到它的工作我必須應用疊加CSS的顏色循環只有一個類.slider1這就是爲什麼我認爲它不會在我的網站上工作,如果我使用.frame和.frame1,但它是一個了不起的想法,如果你知道我可以如何應用相同的只有一個類,所以它可以與我的rev滑塊jquery一起工作,這將是非常史詩 – Cody