用 CSS 做輪播圖

來源:互聯網
上載者:User

標籤:

對於用 css 實現一個輪播圖的緣由,是那時候剛開始接觸前端,完全還不懂 js。但是有一個項目(就是一個用來應付面試的作品)需要做一個輪播的效果,當時第一反應就是用 css3 自訂動畫 -webkit-animation。做出來的效果自己覺得還可以,唯一的不足就是滑鼠點擊切換 banner 圖和指定到某個圖。如果項目中不需要切換變換圖片的需求,用 css3 做,何嘗不是一種辦法。不多說,先上代碼:

 

html 代碼如下:

<div class="test">    <a href="#" ><img class="img1" src="1.jpg" ></a>    <a href="#" ><img class=‘img2‘ src="2.jpg" ></a>    <a href="#" ><img class=‘img3‘ src="3.jpg" ></a>    <a href="#" ><img class=‘img4‘ src="4.jpg" ></a></div>

 

接著 css 代碼:

@-webkit-keyframes t1{    0%{ left: 0; }    20%{ left: 0; }    25%{ left: -960px; }    45%{ left: -960px; }    50%{ left: -1920px; }    70%{ left: -1920px; }    75%{ left: -2880px; }    95%{ left: -2880px; }    100%{ left: 0; }}@-webkit-keyframes t2{    0%{ left: 960px; }    20%{ left: 960px; }    25%{ left: 0; }    45%{ left: 0; }    50%{ left: -960px; }    70%{ left: -960px; }    75%{ left: -1920px; }    95%{ left: -1920px; }    100%{ left: 960px; }}@-webkit-keyframes t3{    0%{ left: 1920px; }    20%{ left: 1920px; }    25%{ left: 960px; }    45%{ left: 960px; }    50%{ left: 0; }    70%{ left: 0; }    75%{ left: -960px; }    95%{ left: -960px; }    100%{ left: 1920px; }}@-webkit-keyframes t4{    0%{ left: 2880px; }    20%{ left: 2880px; }    25%{ left: 1920px; }    45%{ left: 1920px; }    50%{ left: 960px; }    70%{ left: 960px; }    75%{ left: 0; }    95%{ left: 0; }    100%{ left: 2880px; }}

這裡定義了 t1、t2、t3、t4 四個動畫名稱,主要是動畫裡的幀數的設定技巧,因為只有4張圖,所以就取每次幀數增加 25% 的時候才讓 left 值減去圖片寬度,而在改變 left 值之前的 5%(這個值根據情況調整)處 left 值保持不變,而這個 5% 表示的就是輪播圖中圖片移動的時間,另外 20% 表示的就是圖片禁止的狀態。然後把這4個自訂動畫一一放到每一個 img 裡,例如:

.test .img1{    left: 0;    -webkit-animation: t1 linear 12s infinite;       -moz-animation: t1 linear 12s infinite;        -ms-animation: t1 linear 12s infinite;         -o-animation: t1 linear 12s infinite;            animation: t1 linear 12s infinite;}.test .img2{    left: 960px;    -webkit-animation: t2 linear 12s infinite;       -moz-animation: t2 linear 12s infinite;        -ms-animation: t2 linear 12s infinite;         -o-animation: t2 linear 12s infinite;            animation: t2 linear 12s infinite;}.test .img3{    left: 1920px;    -webkit-animation: t3 linear 12s infinite;       -moz-animation: t3 linear 12s infinite;        -ms-animation: t3 linear 12s infinite;         -o-animation: t3 linear 12s infinite;            animation: t3 linear 12s infinite;}.test .img4{    left: 2880px;    -webkit-animation: t4 linear 12s infinite;       -moz-animation: t4 linear 12s infinite;        -ms-animation: t4 linear 12s infinite;         -o-animation: t4 linear 12s infinite;            animation: t4 linear 12s infinite;}.test:hover img{    -webkit-animation-play-state: paused;       -moz-animation-play-state: paused;        -ms-animation-play-state: paused;         -o-animation-play-state: paused;            animation-play-state: paused;}

代碼最後給了一個滑鼠移上去,動畫停止的效果,整個輪播的時間自行設定,‘infinite’ 表示一直持續輪播。

ps:這個是一個正序的輪播,同理也可以實現正反序輪播,在定義動畫那裡改改幀數設定就行,有疑問,歡迎在評論中提出,謝謝大家。

 

用 CSS 做輪播圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.