2015-05-19 92 views
2

我在CSS關鍵幀動畫內出現時間問題。在CSS圖像滑塊內計時

我有90%的存在,但在外觀上每個圖像之間都有差距。

我知道這是一個時間問題,但我不能爲了我的生活而在不破壞當前功能的情況下讓間隙消失。

到目前爲止的代碼:

.slider { 
 
    position: relative; 
 
    height: 200px; 
 
    width: 300px; 
 
    background: gray; 
 
    overflow:hidden; 
 
} 
 
.slide, 
 
.slide img { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0%; 
 
    height: 100%; 
 
    width: 100%; 
 
} 
 
.slide h1 { 
 
    position: absolute; 
 
    top: 20%; 
 
    right: 0; 
 
    height: 10%; 
 
    color: white; 
 
} 
 
.slide p { 
 
    position: absolute; 
 
    top: 40%; 
 
    left: 0; 
 
    height: 60%; 
 
    color: white; 
 
} 
 
.slide { 
 
    transform: translateX(100%); 
 
    -webkit-animation: slide 20s forwards infinite; 
 
} 
 
.slide:nth-child(2) { 
 
    -webkit-animation-delay: 5s; 
 
} 
 
.slide:nth-child(3) { 
 
    -webkit-animation-delay: 10s; 
 
} 
 
.slide:nth-child(4) { 
 
    -webkit-animation-delay: 15s; 
 
} 
 
@-webkit-keyframes slide { 
 
    0% { 
 
    transform: translateX(100%); 
 
    } 
 
    5% { 
 
    transform: translateX(0); 
 
    } 
 
    20% { 
 
    transform: translateX(0); 
 
    } 
 
    25% { 
 
    transform: translateX(-100%); 
 
    } 
 
    100% { 
 
    transform: translateX(-100%); 
 
    } 
 
}
<div class="slider"> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/300/200" /> 
 
    <h1>Slide 1</h1> 
 

 
    <p>Text to do with slide 1</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/600/400" /> 
 
    <h1>Slide 2</h1> 
 

 
    <p>Text to do with slide 2</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/1200/800" /> 
 
    <h1>Slide 3</h1> 
 

 
    <p>Text to do with slide 3</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/1800/1200" /> 
 
    <h1>Slide 4</h1> 
 

 
    <p>Text to do with slide 4</p> 
 
    </div> 
 
</div>

在我的片段,你可以看到一個短暫的延遲,讓灰色的背景下一個圖像之前出現。

我的設計將只適合4張圖片,這應該在屏幕上每個5秒左右。我很高興留在這個CSS關鍵幀(而不是jQuery或外部插件)。

請注意我已經刪除了前綴,因此目前支持前綴webkit

有人請解釋我應該如何計時嗎?我很確定這是0% - > 5%的過渡,但我不確定嗎?

這裏的任何幫助將是偉大的。

回答

2

解決方法是讓屏幕上的每個圖像停留(固定狀態或幻燈片進/出狀態)爲動畫持續時間的1/4。考慮到前5%滑入,最後5%滑出,我們必須將滑出設置爲25%到30%之間,如下面的代碼片段所示。

.slider { 
 
    position: relative; 
 
    height: 200px; 
 
    width: 300px; 
 
    background: gray; 
 
    overflow: hidden; 
 
} 
 
.slide, 
 
.slide img { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0%; 
 
    height: 100%; 
 
    width: 100%; 
 
} 
 
.slide h1 { 
 
    position: absolute; 
 
    top: 20%; 
 
    right: 0; 
 
    height: 10%; 
 
    color: white; 
 
} 
 
.slide p { 
 
    position: absolute; 
 
    top: 40%; 
 
    left: 0; 
 
    height: 60%; 
 
    color: white; 
 
} 
 
.slide { 
 
    transform: translateX(100%); 
 
-webkit-animation: slide 20s infinite linear; 
 
} 
 
.slide:nth-child(2) { 
 
    -webkit-animation-delay: 5s; 
 
} 
 
.slide:nth-child(3) { 
 
    -webkit-animation-delay: 10s; 
 
} 
 
.slide:nth-child(4) { 
 
    -webkit-animation-delay: 15s; 
 
} 
 
@-webkit-keyframes slide { 
 
    5% { 
 
    transform: translateX(0); /* 1s 6s 11s 16s */ 
 
    } 
 
    25% { 
 
    transform: translateX(0); /* 5s 10s 14s 19s */ 
 
    } 
 
    30% { 
 
    transform: translateX(-100%); /* 6s 11s 16s 20s */ 
 
    } 
 
    100% { 
 
    transform: translateX(-100%); 
 
    }  
 
}
<div class="slider"> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/300/200" /> 
 
    <h1>Slide 1</h1> 
 

 
    <p>Text to do with slide 1</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/600/400" /> 
 
    <h1>Slide 2</h1> 
 

 
    <p>Text to do with slide 2</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/1200/800" /> 
 
    <h1>Slide 3</h1> 
 

 
    <p>Text to do with slide 3</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/1800/1200" /> 
 
    <h1>Slide 4</h1> 
 

 
    <p>Text to do with slide 4</p> 
 
    </div> 
 
</div>

0

我最終使用ghant-like圖表來處理髮生的事情,並最終改變了我的關鍵幀動畫。我還添加了第五個圖像集,以保持整秒。

我結束了:

.slider { 
 
    position: relative; 
 
    height: 200px; 
 
    width: 300px; 
 
    background: gray; 
 
    overflow: hidden; 
 
} 
 
.slide, 
 
.slide img { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    height: inherit; 
 
    width: inherit; 
 
} 
 
.slide { 
 
    transform: translateX(100%); 
 
    -webkit-animation: slide 50s ease-in-out 0s infinite; 
 
} 
 
.slide h1 { 
 
    position: absolute; 
 
    top: 20%; 
 
    right: 0; 
 
    height: 10%; 
 
    color: white; 
 
} 
 
.slide p { 
 
    position: absolute; 
 
    top: 40%; 
 
    left: 0; 
 
    height: 60%; 
 
    color: white; 
 
} 
 
.slide:nth-child(2) { 
 
    -webkit-animation-delay: 10s; 
 
} 
 
.slide:nth-child(3) { 
 
    -webkit-animation-delay: 20s; 
 
} 
 
.slide:nth-child(4) { 
 
    -webkit-animation-delay: 30s; 
 
} 
 
.slide:nth-child(5) { 
 
    -webkit-animation-delay: 40s; 
 
} 
 
@-webkit-keyframes slide { 
 
    0% { 
 
    transform: translateX(100%); 
 
    } 
 
    2% { 
 
    transform: translateX(0); 
 
    } 
 
    20% { 
 
    transform: translateX(0); 
 
    } 
 
    22% { 
 
    transform: translateX(-100%); 
 
    } 
 
    100% { 
 
    transform: translateX(-100%); 
 
    } 
 
}
<div class="slider"> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/1800/1200" /> 
 
    <h1>Slide 1</h1> 
 
    <p>Text to do with slide 1</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/1000/500" /> 
 
    <h1>Slide 2</h1> 
 
    <p>Text to do with slide 2</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/200/100" /> 
 
    <h1>Slide 3</h1> 
 
    <p>Text to do with slide 3</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/300/400" /> 
 
    <h1>Slide 4</h1> 
 
    <p>Text to do with slide 4</p> 
 
    </div> 
 
    <div class="slide"> 
 
    <img src="http://lorempixel.com/600/800" /> 
 
    <h1>Slide 5</h1> 
 
    <p>Text to do with slide 5</p> 
 
    </div> 
 
</div>

這讓我順利保持的動畫過渡之間,並且也讓我有足夠的時間,以便用戶閱讀的內容的幻燈片。


此功能還允許我使用動畫播放狀態暫停滑塊徘徊,當它:)

.slider:hover .slide{ 
    -webkit-animation-play-state: paused; 
}