CSS3實現輪播圖效果


CSS3實現輪播圖主要是由css:background-position和css3:animation實現。且實現此輪播需要一張四個圖橫着相連的圖片。

注(Internet Explorer 10、Firefox 以及 Opera 支持 animation 屬性。Safari 和 Chrome 支持替代的 -webkit-animation 屬性。)

HTML:

 <div class="slide-box"></div>

CSS:

<style>
    @-webkit-keyframes slide {
        0% {
            background-position: 0 0;
        }
        10%, 25% {
            background-position: -600px 0;
        }
        35%, 50% {
            background-position: -1200px 0;
        }
        60%, 75% {
            background-position: -1800px 0;
        }
        85%, 100% {
            background-position: 0 0;
        }
    }
    @-moz-keyframes slide {
        0% {
            background-position: 0 0;
        }
        10%, 25% {
            background-position: -600px 0;
        }
        35%, 50% {
            background-position: -1200px 0;
        }
        60%, 75% {
            background-position: -1800px 0;
        }
        85%, 100% {
            background-position: 0 0;
        }
    }
    @-o-keyframes slide {
        0% {
            background-position: 0 0;
        }
        10%, 25% {
            background-position: -600px 0;
        }
        35%, 50% {
            background-position: -1200px 0;
        }
        60%, 75% {
            background-position: -1800px 0;
        }
        85%, 100% {
            background-position: 0 0;
        }
    }
    @keyframes slide {
        0% {
            background-position: 0 0;
        }
        10%, 25% {
            background-position: -600px 0;
        }
        35%, 50% {
            background-position: -1200px 0;
        }
        60%, 75% {
            background-position: -1800px 0;
        }
        85%, 100% {
            background-position: 0 0;
        }
    }
    .slide-box {
        margin: 0 auto;
        width: 600px;
        height: 400px;
        border: 1px solid #ddd;
        background: url(http://sandbox.runjs.cn/uploads/rs/376/uazzmdfd/bg.png) 0 0 no-repeat;
        -webkit-animation: slide 8s linear infinite;
        -o-animation: slide 8s linear infinite;
        animation: slide 8s linear infinite;
    }
</style>

animation 屬性是一個簡寫屬性,JavaScript 語法: object.style.animation=" slide 8s linear infinite",其參數如下:

 animation-name 規定需要綁定到選擇器的 keyframe 名稱。。
animation-duration 規定完成動畫所花費的時間,以秒或毫秒計。
animation-timing-function 規定動畫的速度曲線。
animation-delay 規定在動畫開始之前的延遲。
animation-iteration-count 規定動畫應該播放的次數。
animation-direction

規定是否應該輪流反向播放動畫。

讓圖片在8秒內進行位移,每次向左移動600px,最后回到原點,div寬600px,剛好容下一個圖,這樣就構成了輪播效果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM