CSS3實作輪播圖主要是由css:background-position和css3:animation實作。且實現此輪播需要一張四個圖橫著相連的圖片。
注(Internet Explorer 10、Firefox 以及 Opera 支援 animation 屬性。Safari 和 Chrome 支援替代的 -webkit-animation 屬性。)
HTML:
<span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="slide-box"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
CSS:
<span style="color: #800000;"><style> @-webkit-keyframes slide </span>{<span style="color: #ff0000;"> 0% { background-position</span>:<span style="color: #0000ff;"> 0 0</span>; }<span style="color: #800000;"> 10%, 25% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -600px 0</span>; }<span style="color: #800000;"> 35%, 50% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -1200px 0</span>; }<span style="color: #800000;"> 60%, 75% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -1800px 0</span>; }<span style="color: #800000;"> 85%, 100% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> 0 0</span>; }<span style="color: #800000;"> } @-moz-keyframes slide </span>{<span style="color: #ff0000;"> 0% { background-position</span>:<span style="color: #0000ff;"> 0 0</span>; }<span style="color: #800000;"> 10%, 25% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -600px 0</span>; }<span style="color: #800000;"> 35%, 50% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -1200px 0</span>; }<span style="color: #800000;"> 60%, 75% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -1800px 0</span>; }<span style="color: #800000;"> 85%, 100% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> 0 0</span>; }<span style="color: #800000;"> } @-o-keyframes slide </span>{<span style="color: #ff0000;"> 0% { background-position</span>:<span style="color: #0000ff;"> 0 0</span>; }<span style="color: #800000;"> 10%, 25% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -600px 0</span>; }<span style="color: #800000;"> 35%, 50% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -1200px 0</span>; }<span style="color: #800000;"> 60%, 75% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -1800px 0</span>; }<span style="color: #800000;"> 85%, 100% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> 0 0</span>; }<span style="color: #800000;"> } @keyframes slide </span>{<span style="color: #ff0000;"> 0% { background-position</span>:<span style="color: #0000ff;"> 0 0</span>; }<span style="color: #800000;"> 10%, 25% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -600px 0</span>; }<span style="color: #800000;"> 35%, 50% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -1200px 0</span>; }<span style="color: #800000;"> 60%, 75% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -1800px 0</span>; }<span style="color: #800000;"> 85%, 100% </span>{<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> 0 0</span>; }<span style="color: #800000;"> } .slide-box </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0 auto</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 600px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 400px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid #ddd</span>;<span style="color: #ff0000;"> background</span>:<span style="color: #0000ff;"> url(http://sandbox.runjs.cn/uploads/rs/376/uazzmdfd/bg.png) 0 0 no-repeat</span>;<span style="color: #ff0000;"> -webkit-animation</span>:<span style="color: #0000ff;"> slide 8s linear infinite</span>;<span style="color: #ff0000;"> -o-animation</span>:<span style="color: #0000ff;"> slide 8s linear infinite</span>;<span style="color: #ff0000;"> animation</span>:<span style="color: #0000ff;"> slide 8s linear infinite</span>; }<span style="color: #800000;"> </style></span>
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,剛好容下一個圖,這樣就構成了輪播效果。