2017-08-26 91 views
3

我有一個麻煩得到一個交叉淡化動畫停止在最後一個孩子。我知道動畫填充模式:轉發,但它似乎不工作(我試圖把它放在不同的地方,如在最初的.crossfade聲明。)如何使css動畫停止在最後一個孩子css3

這是我的html:

<body> 
    <div class= "ad"> 
    <div class="crossfade"> 
     <img src="image1.jpg" alt="Image 1"> 
     <img src="image2.png" alt="Image 2"> 
     <img src="image3.png" alt="Image 3"> 
     <img src="image4.png" alt="Image 4"> 
    </div> 
    </div> 
</body> 

我的CSS是在這裏:

.crossfade > img { 
    width: 300px; 
    height: 250px; 
    position: absolute; 
    top: 0px; 
    left: 0px; 
    color: transparent; 
    opacity: 0; 
    z-index: 0; 
    animation-iteration-count:1; 
    -webkit-backface-visibility: hidden; 
    -webkit-animation-name: imageAnimation; 
    -webkit-animation: imageAnimation 43s linear 1 0s; 
    -moz-animation: imageAnimation 43s linear 1 0s; 
    -o-animation: imageAnimation 43 linear 1 0s; 
    -ms-animation: imageAnimation 43 linear 1 0s; 

} 

.crossfade > img:nth-child(1) { 
    -webkit-animation-delay: 1s; 
    -moz-animation-delay: 1s; 
    -o-animation-delay: 1s; 
    -ms-animation-delay: 1s; 
    animation-delay: 1s; 
} 

.crossfade > img:nth-child(2) { 
    -webkit-animation-delay: 7s; 
    -moz-animation-delay: 7s; 
    -o-animation-delay: 7s; 
    -ms-animation-delay: 7s; 
    animation-delay: 7s; 
} 
.crossfade > img:nth-child(3) { 
    -webkit-animation-delay: 14s; 
    -moz-animation-delay: 14s; 
    -o-animation-delay: 14s; 
    -ms-animation-delay: 14s; 
    animation-delay: 14s; 
} 

.crossfade > img:nth-child(4) { 
    -webkit-animation-delay: 21s; 
    -moz-animation-delay: 21s; 
    -o-animation-delay: 21s; 
    -ms-animation-delay: 21s; 
    animation-delay: 21s; 
    animation-fill-mode: forwards; 
} 

@-webkit-keyframes imageAnimation { 
    0%{ opacity:0; 
     -webkit-animation-timing-function: ease-in; } 
    8% { opacity: 1; 
     -webkit-animation-timing-function: ease-out; } 
    17% { opacity: 1} 
    25% { opacity: 0 } 
    100% { opacity: 0} 
} 

@-moz-keyframes imageAnimation { 
    0% { opacity: 0; 
    -moz-animation-timing-function: ease-in; } 
    8% { opacity: 1; 
     -moz-animation-timing-function: ease-out; } 
    17% { opacity: 1 } 
    25% { opacity: 0} 
    100% { opacity: 0 } 
} 

@-o-keyframes imageAnimation { 
    0% { opacity: 0; 
    -o-animation-timing-function: ease-in; } 
    8% { opacity: 1; 
     -o-animation-timing-function: ease-out; } 
    17% { opacity: 1 } 
    25% { opacity: 0 } 
    100% { opacity: 0 } 
} 

@-ms-keyframes imageAnimation { 
    0% { opacity: 0; 
    -ms-animation-timing-function: ease-in; } 
    8% { opacity: 1; 
     -ms-animation-timing-function: ease-out; } 
    17% { opacity: 1 } 
    25% { opacity: 0} 
    100% { opacity: 0 } 
} 

我不太清楚我在做什麼錯誤或我錯過了什麼。提前致謝!

回答

1

參考以此爲example,你甚至可以嘗試像這樣,一次又一次地調用宣佈animationeach child延遲一定時間後。 現在animation-fill-mode:forwards考慮到動畫的最後property結束於100%即動畫結束您的opacity價值將被應用。在下面的例子中,我用animation-fill-mode:forwards爲每個image執行圖像的顏色轉換。

div img { 
 
    width: 250px; 
 
    height: 250px; 
 
    position: absolute; 
 
} 
 

 
div img:nth-child(1) { 
 
    opacity: 0; 
 
    animation: mv 10s linear forwards; 
 
} 
 

 
div img:nth-child(2) { 
 
    opacity: 0; 
 
    animation: mv 10s linear 10s forwards; 
 
} 
 

 
div img:nth-child(3) { 
 
    opacity: 0; 
 
    animation: mv 10s linear 20s forwards; 
 
} 
 

 
div img:nth-child(4) { 
 
    opacity: 0; 
 
    animation: mv 10s linear 30s forwards; 
 
} 
 

 
@keyframes mv { 
 
    0% { 
 
    opacity: 0; 
 
    } 
 
    50% { 
 
    opacity: 1; 
 
    } 
 
    100% { 
 
    opacity: 1; 
 
    } 
 
}
<div> 
 
    <img src="http://placehold.it/301/f22" alt="Image 1"> 
 
    <img src="http://placehold.it/302/f2f" alt="Image 2"> 
 
    <img src="http://placehold.it/303/ff2" alt="Image 3"> 
 
    <img src="http://placehold.it/304/2f2" alt="Image 4"> 
 
</div>

+0

非常感謝!你的回答很簡單,而且有道理。 (我對css3還是有點新鮮的)。我是否理解宣佈一個新的「動畫:」每次爲每個孩子重新開始動畫? 旁邊的問題:我目前正在關注一個教程,並注意到你不需要在你的答案中使用-o-,-moz-,-webkit-。我的理解是,這些是用來使它與不同的瀏覽器兼容,但你的版本似乎無論如何工作。有什麼特別的情況下我會使用這些嗎? :]再次感謝堆。 –

+0

@JessicaFoo這個答案的解決方案並不_restart_動畫,它創建了4個動畫,這比我在我的答案中已經發布的性能要低(檢查我的第二個樣本,它與這個樣本完全相同,但更加優化),所以我不建議這樣做。 – LGSon

+0

@JessicaFoo此外,缺少前綴屬性無論如何,它的工作原理與您理解的一樣,對於需要它們的舊瀏覽器,如果您希望您的網站訪問者使用舊瀏覽器,也應該添加它們看動畫。 – LGSon

2

由於您@keyframes規則與opacity: 0如果你使用forwards與否都無所謂結束,其最終狀態將是opacity: 0兩個。

一個解決方案是爲最後一個項目添加第二個@keyframes規則以及forwards

請注意,我刪除了所有前綴的屬性,使得更容易解析代碼。此外,原代碼缺少了很多非的前綴,這兩個屬性和關鍵幀的規則,你也應該始終把前綴屬性的非前綴

.crossfade > img { 
 
    width: 300px; 
 
    height: 250px; 
 
    position: absolute; 
 
    top: 0px; 
 
    left: 0px; 
 
    color: transparent; 
 
    opacity: 0; 
 
    z-index: 0; 
 
    backface-visibility: hidden; 
 
    animation: imageAnimation 43s linear 0s 1 forwards; 
 
} 
 

 
.crossfade > img:nth-child(1) { 
 
    animation-delay: 1s; 
 
} 
 

 
.crossfade > img:nth-child(2) { 
 
    animation-delay: 7s; 
 
} 
 
.crossfade > img:nth-child(3) { 
 
    animation-delay: 14s; 
 
} 
 

 
.crossfade > img:nth-child(4) { 
 
    animation-delay: 21s; 
 
    animation-name: imageAnimationLast; 
 
} 
 

 
@keyframes imageAnimation { 
 
    0%{ opacity:0; 
 
     animation-timing-function: ease-in; } 
 
    8% { opacity: 1; 
 
     animation-timing-function: ease-out; } 
 
    17% { opacity: 1} 
 
    25% { opacity: 0 } 
 
    100% { opacity: 0} 
 
} 
 

 
@keyframes imageAnimationLast { 
 
    0%{ opacity:0; 
 
     animation-timing-function: ease-in; } 
 
    8% { opacity: 1; 
 
    100% { opacity: 1} 
 
}
<div class= "ad"> 
 
    <div class="crossfade"> 
 
     <img src="http://placehold.it/200/f00" alt="Image 1"> 
 
     <img src="http://placehold.it/200/ff0" alt="Image 2"> 
 
     <img src="http://placehold.it/200/0ff" alt="Image 3"> 
 
     <img src="http://placehold.it/200/00f" alt="Image 4"> 
 
    </div> 
 
    </div>


基於前你打算如何處理這些項目以及它們如何重疊,並且當第二個位於第三位等等時,你可以簡單地讓@keyframes規則停留在opacity: 1

.crossfade > img { 
 
    width: 300px; 
 
    height: 250px; 
 
    position: absolute; 
 
    top: 0px; 
 
    left: 0px; 
 
    color: transparent; 
 
    opacity: 0; 
 
    z-index: 0; 
 
    backface-visibility: hidden; 
 
    animation: imageAnimation 43s ease-in 0s 1 forwards; 
 
} 
 

 
.crossfade > img:nth-child(1) { 
 
    animation-delay: 1s; 
 
} 
 

 
.crossfade > img:nth-child(2) { 
 
    animation-delay: 7s; 
 
} 
 
.crossfade > img:nth-child(3) { 
 
    animation-delay: 14s; 
 
} 
 

 
.crossfade > img:nth-child(4) { 
 
    animation-delay: 21s; 
 
} 
 

 
@keyframes imageAnimation { 
 
    0%{ opacity:0; } 
 
    8% { opacity: 1; } 
 
    100% { opacity: 1; } 
 
}
<div class= "ad"> 
 
    <div class="crossfade"> 
 
     <img src="http://placehold.it/200/f00" alt="Image 1"> 
 
     <img src="http://placehold.it/200/ff0" alt="Image 2"> 
 
     <img src="http://placehold.it/200/0ff" alt="Image 3"> 
 
     <img src="http://placehold.it/200/00f" alt="Image 4"> 
 
    </div> 
 
    </div>