2017-07-05 144 views
0

https://codepen.io/codeispoetry/pen/dRKKEY背景顏色過渡W/O JQuery的

.back { 
 
    margin: 20px; 
 
    padding: 100px; 
 
    background: yellow; 
 
    font-size: 30px; 
 
}
<div class="back"> 
 

 
    Fun place to stay. We got a golf cart to see the rest of the island, but the house is well-equipped and comfortable. Vanessa was kind to pick us up and make golf cart rental arrangements. We were here on our honeymoon, it was perfect for a memorable trip! 
 
    Hosts easily available 
 

 
</div>

我想要的黃色背景應該隨着時間而改變,以黃,但我想通過CSS做的第i個。在StackOverflow上有足夠的解決方案可以與Jquery一起使用,但是我們可以通過CSS來實現這一點,以便它可以在所有瀏覽器上運行。

https://www.w3schools.com/css/css3_animations.asp我試過了,但它回滾到原來的顏色。如何使顏色永久後。

+0

請給一個鏡頭的CSS動畫。你不能僅僅提出一個問題而不提供你已經嘗試過的代碼。試試css動畫,如果您遇到任何問題,請在此處發佈您的代碼。謝謝。 – viCky

+0

如果我知道代碼,我不會發布問題先生。 – somethingnow

+0

你想讓黃色背景隨着時間的推移變黃?在我看來,這已經發生了 –

回答

1

使用下面的更新的代碼。您需要添加「動畫填充模式:轉發」以在動畫完成後停止動畫。

.back { 
    margin: 20px; 
    padding: 100px; 
    font-size: 30px; 
    background-color: yellow; 
    -webkit-animation-name: example; 
    -webkit-animation-duration: 4s; 
    animation-name: example; 
    animation-duration: 4s; 
    -webkit-animation-fill-mode: forwards; /* Safari 4.0 - 8.0 */ 
    animation-fill-mode: forwards; 
} 
@-webkit-keyframes example { 
    from {background-color: yellow;} 
    to {background-color: white;} 
} 

@keyframes example { 
    from {background-color: yellow;} 
    to {background-color: white;} 
} 
+0

老闆你的解決方案。你貶低我了嗎?爲什麼?我不是垃圾郵件發送者。這是一個很好的問題。 – somethingnow

+1

不,我沒有做downvote ...可能是某人... – Karthik

0

這可能是你要找的。

.block { 
    background-color: yellow; 
} 

.block:hover { 
    background-color: white; 
    -webkit-transition: background-color 1000ms linear; 
    -ms-transition: background-color 1000ms linear; 
    transition: background-color 1000ms linear; 
} 
+0

這不是懸停。 – somethingnow

+1

雖然這樣做完成了顏色轉換作業,但聽起來好像OP希望它由懸停觸發一樣。 – domsson

+0

@AdiYogi它應該如何觸發?當您滾動瀏覽或頁面加載時?加載頁面時加載了 – Stefan

0

試試這個代碼

.back { 
 
    margin: 20px; 
 
    padding: 100px; 
 
    // background: yellow; 
 
    font-size: 30px; 
 
} 
 

 
div { 
 
    background-color: red; 
 
    -webkit-animation-name: example; 
 
    /* Safari 4.0 - 8.0 */ 
 
    -webkit-animation-duration: 4s; 
 
    /* Safari 4.0 - 8.0 */ 
 
    animation-name: example; 
 
    animation-duration: 4s; 
 
    animation-iteration-count: infinite; 
 
} 
 

 

 
/* Safari 4.0 - 8.0 */ 
 

 
@-webkit-keyframes example { 
 
    from { 
 
    background-color: red; 
 
    } 
 
    to { 
 
    background-color: yellow; 
 
    } 
 
} 
 

 

 
/* Standard syntax */ 
 

 
@keyframes example { 
 
    from { 
 
    background-color: red; 
 
    } 
 
    to { 
 
    background-color: yellow; 
 
    } 
 
}
<div class="back"> 
 

 
    Fun place to stay. We got a golf cart to see the rest of the island, but the house is well-equipped and comfortable. Vanessa was kind to pick us up and make golf cart rental arrangements. We were here on our honeymoon, it was perfect for a memorable trip! 
 
    Hosts easily available 
 

 
</div>

+0

一旦動畫完成,這似乎跳回到初始顏色。 (另外,爲什麼不使用OP指定的顏色?) – domsson

+0

https://www.w3schools.com/css/css3_animations.asp我試過這個,但它回滾到原來的顏色。如何使後續顏色永久。 – somethingnow

+0

你低估了爲什麼? – somethingnow

1

您可以使用keyframe animation

@keyframes background-change { 
    to { background-color: hotpink; } 
} 

.block { 
    animation: background-change 900ms forwards; 
} 
+0

嘗試沒有工作。 – somethingnow

+0

@AdiYogi它的工作.. [演示](https://jsfiddle.net/h12hqe2q/) – bhv

+0

你低估了我爲什麼? – somethingnow

1
.back { 
    margin:20px; 
    padding: 100px; 
    font-size: 30px; 
    animation-name: changeColor; 
    animation-duration: 4s; 
    animation-iteration-count: infinite; 
} 

@-webkit-keyframes changeColor { 
    0% { 
    background-color: yellow; 
    } 
    50% { 
    background-color: white; 
    } 
    100%{ 
    background-color: yellow; 
    } 
} 



@keyframes changeColor { 
    0% { 
    background-color: yellow; 
    } 
    50% { 
    background-color: white; 
    } 
    100%{ 
    background-color: yellow; 
    } 
} 

它將改變的時間改變顏色無限量從黃色到白色回黃色,使它看起來光滑。

+0

你爲什麼低估我? – somethingnow

+1

@AdiYogi不,我沒有。在你的問題中的第一個評論要求你嘗試過的代碼,以便他可能已經低估了。當我看到你的帖子時,我已經看到你已經嘗試了一些東西,所以沒有理由downvote。 – user5014677