CSS演武場今天繼續,今天看一個純css實現的3D圖像輪轉效果,請大家猛戳研究效果先,也可下載收藏先。 首先看html文件,div.billboard為效果的容器,利用10個div.poster分割圖像,每個poster中有三個face,分別用來承載三個圖像。 <div class="billboa
CSS演武場今天繼續,今天看一個純css實現的3D圖像輪轉效果,請大家猛戳研究效果先,也可下載收藏先。
首先看html文件,div.billboard為效果的容器,利用10個div.poster分割圖像,每個poster中有三個face,分別用來承載三個圖像。
- <div class="billboard">
- <div class="poster">
- <div class="face panel1 p1"></div>
- <div class="face panel2 p1"></div>
- <div class="face panel3 p1"></div>
- </div>
- <div class="poster">
- <div class="face panel1 p2"></div>
- <div class="face panel2 p2"></div>
- <div class="face panel3 p2"></div>
- </div>
- <div class="poster">
- <div class="face panel1 p3"></div>
- <div class="face panel2 p3"></div>
- <div class="face panel3 p3"></div>
- </div>
- <div class="poster">
- <div class="face panel1 p4"></div>
- <div class="face panel2 p4"></div>
- <div class="face panel3 p4"></div>
- </div>
- <div class="poster">
- <div class="face panel1 p5"></div>
- <div class="face panel2 p5"></div>
- <div class="face panel3 p5"></div>
- </div>
- <div class="poster">
- <div class="face panel1 p6"></div>
- <div class="face panel2 p6"></div>
- <div class="face panel3 p6"></div>
- </div>
- <div class="poster">
- <div class="face panel1 p7"></div>
- <div class="face panel2 p7"></div>
- <div class="face panel3 p7"></div>
- </div>
- <div class="poster">
- <div class="face panel1 p8"></div>
- <div class="face panel2 p8"></div>
- <div class="face panel3 p8"></div>
- </div>
- <div class="poster">
- <div class="face panel1 p9"></div>
- <div class="face panel2 p9"></div>
- <div class="face panel3 p9"></div>
- </div>
- <div class="poster">
- <div class="face panel1 p10"></div>
- <div class="face panel2 p10"></div>
- <div class="face panel3 p10"></div>
- </div>
- </div>
CSS文件這裡我們用到了sass,用的是scss語法。
- //變數初始化
- //圖像分塊個數,如要更改,html需要進行相應的修改
- $numPoster:10;
- //輪換圖像個數,如要更改,html需要進行相應的修改
- $numFace:3;
- //圖像寬度
- $width:600px;
- //圖像高度
- $height:320px;
- //盒子的設置
- .billboard {
- width:$width;
- margin:100px auto;
- }
- //圖像條左浮動
- .poster {
- float:left;
- width:$width/$numPoster;
- height:$height;
- }
- //圖像條面的統一設置,絕對定位、3d動畫設置
- .face {
- position:absolute;
- height:$height;
- width:$width/$numPoster;
- transform-origin:50% 50% -17px;
- backface-visibility: hidden;
- transform-style:preserve-3d;
- perspective:350px;
- }
- //圖像條面分別設置背景圖像、動畫
- @for $i from 1 through $numFace{
- .poster .panel#{$i} {
- background:url(http://gx.zptc.cn/whqet/img/#{$i}.jpg);
- transform:transformY(360deg/$numFace*($i - 1));
- animation: rotateMe#{$i} 10s infinite;
- }
- @keyframes rotateMe#{$i} {
- 0% {
- transform:rotateY(360deg/$numFace*($i - 1));
- }
- 9% {
- transform:rotateY(360deg/$numFace*($i - 1));
- }
- 24% {
- transform:rotateY(360deg/$numFace*($i));
- }
- 42% {
- transform:rotateY(360deg/$numFace*($i));
- }
- 57% {
- transform:rotateY(360deg/$numFace*($i + 1));
- }
- 75% {
- transform:rotateY(360deg/$numFace*($i + 1));
- }
- 90% {
- transform:rotateY(360deg/$numFace*($i + 2));
- }
- 100% {
- transform:rotateY(360deg/$numFace*($i + 2));
- }
- }
- }
- //圖像條面的背景偏移
- @for $i from 1 through $numPoster {
- .poster .p#{$i} {background-position:-($width/$numPoster*($i - 1)) top;}
- }
使用sass可以使得代碼結構清晰,邏輯性強,請大家研讀提升!