1. 程式人生 > >純css實現輪播效果

純css實現輪播效果

#frame {/*----------圖片輪播相框容器----------*/ position:absolute;width:300px; height:200px;overflow:hidden;border-radius:5px;} #photos {/*---設定總的圖片寬度--通過位移來達到輪播效果----*/ position:absolute;z-index:9;width:calc(300px * 5);/*---修改圖片數量的話需要修改下面的動畫引數*/} #photos img { float
:left
;width:300px;height:200px;}
.play{ animation:ma 20s ease-out infinite alternate;/**/ } @keyframes ma {/*---每圖片切換有兩個階段:位移切換和靜置。中間的效果可以任意定製----*/ 0%,20% {margin-left:0; } 25%,40% {margin-left:-300px;} 45%,60% {margin-left:-600px;} 65%,80% {margin-left:-900px;} 85%,100% {margin-left
:-1200px
;}
} .num{ position:absolute;z-index:10;display:inline-block;right:10px;top:165px;border-radius:100%; background:#f00;width:25px;height:25px;line-height:25px;cursor:pointer;color:#fff; text-align:center;opacity:0.8;} .num:hover{background:#00f;} .num
:hover,#photos:hover{animation-play-state:paused;} .num:nth-child(4){margin-right:30px} .num:nth-child(3){margin-right:60px} .num:nth-child(2){margin-right:90px} .num:nth-child(1){margin-right:120px} #a1:hover ~ #photos{animation:ma1 .5s ease-out forwards;} #a2:hover ~ #photos{animation:ma2 .5s ease-out forwards;} #a3:hover ~ #photos{animation:ma3 .5s ease-out forwards;} #a4:hover ~ #photos{animation:ma4 .5s ease-out forwards;} #a5:hover ~ #photos {animation:ma5 .5s ease-out forwards;} @keyframes ma1 {0%{margin-left:-1200px;}100%{margin-left:0;} } @keyframes ma2 {0%{margin-left:0;}100%{margin-left:-300px;} } @keyframes ma3 {0%{margin-left:-300px;}100%{margin-left:-600px;} } @keyframes ma4 {0%{margin-left:-600px;}100%{margin-left:-900px;} } @keyframes ma5 {0%{margin-left:-900px;}100%{margin-left:-1200px;} }