纯CSS3制作旋转的魔方
一、搭建魔方的框架
代码如下图所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.container{
width: 200px;
height: 200px;
border: 1px solid red;
margin: 250px auto;
}
.box{
width: 200px;
height: 200px;
position: relative;
}
.box div{
width: 200px;
height: 200px;
position: absolute;
text-align: center;
line-height: 200px;
color: white;
font-size: 25px;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</div>
</body>
</html>
浏览器运行结果如下图所示:
二、搭建3D空间并给出合适的视距
**perspective** 属性定义 3D 元素距视图的距离,以像素计。
**transform--style**属性指定嵌套元素是怎样在三维空间中呈现。
**perspective-origin** 属性定义 3D 元素所基于的 X 轴和 Y 轴,该属性允许您改变 3D 元素的底部位置。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.container{
width: 200px;
height: 200px;
border: 1px solid red;
margin: 250px auto;
perspective: 800px;
}
.box{
width: 200px;
height: 200px;
position: relative;
transform-style: preserve-3d;
transform-origin:center center -100px;
}
.box div{
width: 200px;
height: 200px;
position: absolute;
text-align: center;
line-height: 200px;
color: white;
font-size: 25px;
}
.box div:nth-child(1){
background-color: red;
transform:translateX(-200px);
}
.box div:nth-child(2){
background-color: blue;
transform:translateX(200px);
}
.box div:nth-child(3){
background-color: green;
transform:translateY(200px);
}
.box div:nth-child(4){
background-color: pink;
transform:translateY(-200px);
}
.box div:nth-child(5){
background-color: sandybrown;
transform: translateZ(-200px);
}
.box div:nth-child(6){
background-color: slateblue;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</div>
</body>
</html>
浏览器运行结果如下图所示:
-
设置rotateY(-30deg)、rotateY(30deg)、rotateX(-30deg)、rotateX(30deg)效果图如下
-
设置rotateY(-50deg)、rotateY(50deg)、rotateX(-50deg)、rotateX(50deg)效果图如下
-
设置rotateY(-70deg)、rotateY(70deg)、rotateX(-70deg)、rotateX(70deg)效果图如下
-
设置rotateY(-90deg)、rotateY(90deg)、rotateX(-90deg)、rotateX(90deg)效果图如下
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.container{
width: 200px;
height: 200px;
margin: 250px auto;
perspective: 800px;
}
.box{
width: 200px;
height: 200px;
position: relative;
transform-style: preserve-3d;
transform-origin:center center -100px;
}
.box div{
width: 200px;
height: 200px;
position: absolute;
text-align: center;
line-height: 200px;
color: white;
font-size: 25px;
}
.box div:nth-child(1){
background-color: red;
transform:translateX(-200px) rotateY(-90deg);
transform-origin: right;
}
.box div:nth-child(2){
background-color: blue;
transform:translateX(200px) rotateY(90deg);
transform-origin: left;
}
.box div:nth-child(3){
background-color: green;
transform:translateY(200px) rotateX(-90deg);
transform-origin: top;
}
.box div:nth-child(4){
background-color: pink;
transform:translateY(-200px) rotateX(90deg);
transform-origin: bottom;
}
.box div:nth-child(5){
background-color: sandybrown;
transform: translateZ(-200px);
}
.box div:nth-child(6){
background-color: slateblue;
}
@keyframes mofang_change{
0%{
transform: rotateX(0deg) rotateZ(0deg);
}
100%{
transform: rotateX(270deg) rotateY(-270deg) rotateX(-270deg) rotateY(270deg) ;
}
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</div>
</body>
</html>
三、给类名box添加动画
.box{
animation: mofang_change 5s linear infinite;
}
@keyframes mofang_change{
0%{
transform: rotateX(0deg) rotateZ(0deg);
}
100%{
transform: rotateX(270deg) rotateY(-270deg) rotateX(-270deg) rotateY(270deg) ;
}
}
四、完整代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.container{
width: 200px;
height: 200px;
margin: 250px auto;
perspective: 800px;
}
.box{
width: 200px;
height: 200px;
position: relative;
transform-style: preserve-3d;
transform-origin:center center -100px;
animation: mofang_change 5s linear infinite;
}
.box div{
width: 200px;
height: 200px;
position: absolute;
text-align: center;
line-height: 200px;
color: white;
font-size: 25px;
}
.box div:nth-child(1){
background-color: red;
transform:translateX(-200px) rotateY(-90deg);
transform-origin: right;
}
.box div:nth-child(2){
background-color: blue;
transform:translateX(200px) rotateY(90deg);
transform-origin: left;
}
.box div:nth-child(3){
background-color: green;
transform:translateY(200px) rotateX(-90deg);
transform-origin: top;
}
.box div:nth-child(4){
background-color: pink;
transform:translateY(-200px) rotateX(90deg);
transform-origin: bottom;
}
.box div:nth-child(5){
background-color: sandybrown;
transform: translateZ(-200px);
}
.box div:nth-child(6){
background-color: slateblue;
}
@keyframes mofang_change{
0%{
transform: rotateX(0deg) rotateZ(0deg);
}
100%{
transform: rotateX(270deg) rotateY(-270deg) rotateX(-270deg) rotateY(270deg) ;
}
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</div>
</body>
</html>
效果如下所示: