CSS進階之純CSS投影片

來源:互聯網
上載者:User

標籤:css css動畫 css投影片

CSS進階之純CSS投影片

   昨天寫的博文有人點了贊,讓我激動不已,晚飯賞自己一個雞腿,650) this.width=650;" src="http://img.baidu.com/hi/jx2/j_0028.gif" alt="j_0028.gif" />。如果我的博文對您有協助的話,就請您點個贊,您的鼓勵是我前進的動力650) this.width=650;" src="http://img.baidu.com/hi/babycat/C_0019.gif" alt="C_0019.gif" />。如果發現文中有錯誤的地方,也煩請各位大牛指出650) this.width=650;" src="http://img.baidu.com/hi/face/i_f28.gif" alt="i_f28.gif" />。以下開始本文。

  首先用HTML搭建出基本的架構,如下:

<div id="container">    <div class="slide">    <!--將需要展示的圖片放在一個ul裡面-->        <ul>            <li class="first animation">                <img src="img/014.jpg" alt="天堂和地獄">                <div class="tooltips"><p>天堂和地獄</p></div>            </li>            <li class="second animation">                <img src="img/015.jpg" alt="雪山">                <div class="tooltips"><p>雪山</p></div>            </li>            <li class="third animation">                <img src="img/016.jpg" alt="山穀">                <div class="tooltips"><p>山穀</p></div>            </li>            <li class="fourth animation">                <img src="img/017.jpg" alt="花朵">                <div class="tooltips"><p>花朵</p></div>            </li>            <li class="fifth animation">                <img src="img/018.jpg" alt="藍天白雲">                <div class="tooltips"><p>藍天白雲</p></div>            </li>        </ul>        <!--這個是一個進度條-->        <div class="progressBar"></div>    </div></div>

然後使用CSS給它加上基本的樣式,讓它看起來沒有那麼亂。

*{    margin: 0;    padding: 0;}body{    min-width: 900px;}#container{    width: 830px;    margin: 50px auto;}.slide{    border: 15px solid #ccc;    border-radius: 5px;    width: 800px;    height: 450px;    overflow: hidden;    position: relative;}.slide ul{    position: relative;}.slide ul li{    list-style: none;    position: absolute;    left: -800px;}.slide ul li img{    width: 800px;    height: 450px;}.tooltips{    background: rgba(0,0,0,0);    width: 300px;    height: 60px;    position: relative;    top: -80px;    left: -300px;    /*-webkit-transition: all 0.3s ease-in-out;*/    /*-moz-transition: all 0.3s ease-in-out;*/    /*-ms-transition: all 0.3s ease-in-out;*/    /*-o-transition: all 0.3s ease-in-out;*/    /*transition: all 0.3s ease-in-out;*/}.tooltips p{    color:#fff;    font-size:24px;    font-weight:300;    line-height:60px;    padding:0 0 0 20px;}.slide .animation:hover .tooltips{    left:0;    background: rgba(0,0,0,0.7);}.progressBar{    position: absolute;    width: 800px;    bottom: 0px;    height: 5px;    background: #000;    z-index: 1;    /*animation: progress 25s linear infinite;*/    /*-moz-animation: progress 25s linear infinite;*/    /*-webkit-animation: progress 25s linear infinite;*/}

加完樣式之後瞬間感覺清爽多了。接下來就是最關鍵的部分,使用CSS動畫使圖片動起來達到輪播的效果。(是本文實現的效果,映像有點不清晰,主要功能為五張圖片依次輪播,滑鼠划過停止動畫以及出現提示文字和下方進度條)


650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/85/8B/wKiom1eoJ_KziFTmABEaFkZ8nQg676.gif" title="錄影2.gif" alt="wKiom1eoJ_KziFTmABEaFkZ8nQg676.gif" />

在搜羅了一大堆前輩的代碼之後,明白了輪播的基本原理。

  在一個播放周期內(文中用的是25s),讓五張輪播圖片依次出現在視窗內,通過更改圖片的 opacity 和 z-index來實現。想要實現圖片從左划到右邊的動畫,我們只需更改圖片的 left屬性即可。

我用一張時間表大概描述一下實現的原理。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/85/8B/wKiom1eoJGfAxOJtAAKf0Be4wqU098.jpg-wh_500x0-wm_3-wmp_4-s_1407561244.jpg" title="01.jpg" alt="wKiom1eoJGfAxOJtAAKf0Be4wqU098.jpg-wh_50" />

  以第一張圖片為例,其CSS動畫如下:

/*定義動畫*/.slide li.first{    animation: cycle01 25s linear infinite;    -moz-animation: cycle01 25s linear infinite;    -webkit-animation: cycle01 25s linear infinite;}/*如表上一樣定義主要畫面格*/@keyframes cycle01 {    0%{left: 0;opacity: 1;z-index: 0;}    10%{left:0;opacity: 1;z-index: 0;}    20%{left: 800px;opacity: 0;z-index: 0;}    21%{        left: -800px;        opacity:0;        z-index: -1;    }    90%{        left: -800px;        opacity: 0;        z-index: -1;    }    100%{        left:0px;        opacity: 1;        z-index: 0;    }}

然後依次定義後面的四張。到此,CSS投影片的準系統已經實現。接下來為其添加滑鼠停留,動畫停止和下方進度條的功能。

.slide:hover li,.slide:hover .progressBar{      /*滑鼠經過的時候停止動畫*/    animation-play-state: paused;}/*進度條出現的原理跟上面圖片出現的原理一樣*/.progressBar{    position: absolute;    width: 800px;    bottom: 0px;    height: 5px;    background: #000;    z-index: 1;    animation: progress 25s linear infinite;    -moz-animation: progress 25s linear infinite;    -webkit-animation: progress 25s linear infinite;}@keyframes progress {                /*定義主要畫面格*/    0%,20%,40%,60%,80%{        width:0;        opacity: 0;    }    5%,25%,45%,65%,85%{        width: 50%;        opacity: 0.7;    }    10%,30%,50%,70%,90%{        width: 100%;        opacity: 0.3;    }    11%,31%,51%,71%,91%{        width: 0;        opacity: 0;    }}

完整原始碼和圖片在下方的附件中。未處理相容問題,所以請使用最新版本的瀏覽器。

本文出自 “Fcheng” 部落格,請務必保留此出處http://fcheng.blog.51cto.com/10644114/1835631

CSS進階之純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.