線上展示:https://hovertree.com/texiao/css/14/
這個效果全靠 CSS3 的 animation 完成:十張圖片各沿 Y 軸旋轉 36 度後向外平移,排成一圈,再讓整個容器持續旋轉,就形成了一座立體轉盤。
animation 是六個子屬性的合寫:
預設值為 none 0 ease 0 1 normal,不繼承,版本為 CSS3。JavaScript 寫法:object.style.animation = "mymove 5s infinite"。
最容易忽略的一點:一定要設定 animation-duration。它的預設值是 0,也就是動畫時長為零,看起來就像「完全沒寫動畫」。
#wrap {
width: 300px;
height: 400px;
position: relative;
margin: 100px auto;
-webkit-perspective: 3000px;
-moz-perspective: 3000px;
-ms-perspective: 3000px;
}
#head {
width: 100%;
height: 100%;
position: absolute;
-webkit-transform-style: preserve-3d;
-webkit-animation: donghua 15s linear 0s infinite;
-moz-transform-style: preserve-3d;
-moz-animation: donghua 15s linear 0s infinite;
}
#head div {
position: absolute;
top: 0; left: 0;
width: 300px;
height: 400px;
border: 1px solid red;
}
#head div:nth-child(1) {
-webkit-transform: rotateY(0deg) translateZ(400px);
-moz-transform: rotateY(0deg) translateZ(400px);
background: url(https://hovertree.com/texiao/css/14/hovertreepic/01.jpg);
background-size: cover;
}
#head div:nth-child(2) {
-webkit-transform: rotateY(36deg) translateZ(500px);
-moz-transform: rotateY(36deg) translateZ(500px);
background: url(https://hovertree.com/texiao/css/14/hovertreepic/02.jpg);
background-size: cover;
}
/* 其餘 nth-child 依此類推,角度每格 +36deg */
@-webkit-keyframes donghua {
0% { transform: rotateX(5deg) rotateY(360deg); }
50% { transform: rotateX(-5deg) rotateY(180deg); }
100% { transform: rotateX(5deg) rotateY(0deg); }
}
動畫中刻意讓 rotateX 在 5 度與 -5 度之間擺動,木馬因此有了輕微的上下俯仰,不再是死板地水平旋轉。若想讓每張圖的 translateZ 保持一致(示範中 400px 與 500px 混用了),圓柱半徑才會均勻,這一點值得在實作時留意。
其他範例:https://hovertree.com/texiao/css/14/1.htm