实现旋转木马的思路
- 创建一个大盒子,里面放若干个小盒子
- 为小盒子设置绝对定位,大盒子设置相对定位
- 第一张沿着Z轴运动,后续小盒子沿着Y轴递增旋转60度,并且都再顺着Z轴运动
- 为body设置perspective值,定义 3D 元素距视图的距离
- 最后一步写动画,让大盒子section沿着Y轴进行旋转
<head>
<style>
body {
perspective: 800px;
}
section {
position: relative;
margin: 100px auto;
width: 300px;
height: 200px;
transform-style: preserve-3d;
animation: action 10s linear infinite;
background: url('pic/horse.png') no-repeat;
}
@keyframes action {
0% {
transform: rotateY(0);
}
100% {
transform: rotateY(360deg);
}
}
section div {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: url('pic/dog.png') no-repeat;
background-size: cover;
}
section div:nth-child(1) {
transform: rotateY(0) translateZ(300px);
}
section div:nth-child(2) {
transform: rotateY(60deg) translateZ(300px);
}
section div:nth-child(3) {
transform: rotateY(120deg) translateZ(300px);
}
section div:nth-child(4) {
transform: rotateY(180deg) translateZ(300px);
}
section div:nth-child(5) {
transform: rotateY(240deg) translateZ(300px);
}
section div:nth-child(6) {
transform: rotateY(300deg) translateZ(300px);
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
</body>