CSS3实现轮播图效果

CSS3实现轮播图主要是由css:background-position和css3:animation实现。且实现此轮播需要一张四个图横着相连的图片。

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

HTML:

; ;<pre> ;&lt;div ;class="slide-box"&gt;&lt;/div&gt;</pre> ; ;

CSS:

; ;<pre>&lt;style&gt;

; ; ; ;@-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;

; ; ; ;}

&lt;/style&gt;</pre> ; ;

animation ;属性是一个简写属性,JavaScript ;语法: ;<em>object</em>.style.animation=&quot;<span ;style="color: ;#000000;"> ;slide ;8s ;linear ;infinite</span>&quot;,其参数如下:

; ;<table ;class="dataintable"> ; ;<tbody> ; ;<tr> ; ;<td> <em>animation-name</em></td> ; ;<td>规定需要绑定到选择器的 ;keyframe ;名称。。</td> ; ;</tr> ; ;<tr> ; ;<td><em>animation-duration</em></td> ; ;<td>规定完成动画所花费的时间,以秒或毫秒计。</td> ; ;</tr> ; ;<tr> ; ;<td><em>animation-timing-function</em></td> ; ;<td>规定动画的速度曲线。</td> ; ;</tr> ; ;<tr> ; ;<td><em>animation-delay</em></td> ; ;<td>规定在动画开始之前的延迟。</td> ; ;</tr> ; ;<tr> ; ;<td><em>animation-iteration-count</em></td> ; ;<td>规定动画应该播放的次数。</td> ; ;</tr> ; ;<tr> ; ;<td><em>animation-direction</em></td> ; ;<td> ; ;

规定是否应该轮流反向播放动画。

让图片在8秒内进行位移,每次向左移动600px,最后回到原点,div宽600px,刚好容下一个图,这样就构成了轮播效果。

相关推荐