- 使用定位将6个div盒子放在一起(子绝父相)
<section> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </section>
section { position: relative; width: 300px; height: 200px; margin: 100px auto; //控制子元素开启三维立体环境 transform-style: preserve-3d; } section div { position: absolute; left: 0; top: 0; width: 100%; height: 100%; background: url(./images/7.jpg) no-repeat; }
- 使用3D转换知识,将子盒子分别旋转然后移动
section div:first-child { transform: translateZ(300px); } section div:nth-child(2) { transform: rotateY(60deg) translateZ(300px); } section div:nth-child(3) { transform: rotateY(-60deg) translateZ(300px); } section div:nth-child(4) { transform: rotateY(120deg) translateZ(300px); } section div:nth-child(5) { transform: rotateY(-120deg) translateZ(300px); } section div:nth-child(6) { transform: rotateY(180deg) translateZ(300px); }
- 开启透视,查看效果
body { perspective: 1000px; }
4.使用动画,绕Y轴旋转360deg
@keyframes move {
100% {
transform: rotateY(360deg);
}
}
5.全部代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>旋转木马</title>
<style>
body {
perspective: 1000px;
}
section {
position: relative;
width: 300px;
height: 200px;
margin: 100px auto;
transform-style: preserve-3d;
animation: move 4s linear infinite
}
section div {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: url(./images/7.jpg) no-repeat;
}
section div:first-child {
transform: translateZ(300px);
}
section div:nth-child(2) {
transform: rotateY(60deg) translateZ(300px);
}
section div:nth-child(3) {
transform: rotateY(-60deg) translateZ(300px);
}
section div:nth-child(4) {
transform: rotateY(120deg) translateZ(300px);
}
section div:nth-child(5) {
transform: rotateY(-120deg) translateZ(300px);
}
section div:nth-child(6) {
transform: rotateY(180deg) translateZ(300px);
}
@keyframes move {
100% {
transform: rotateY(360deg);
}
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
</body>
</html>