用flex和动画做个旋转的骰子
前言: 前两天写了一下关于flex布局的相关知识的回顾,今天就用flex和动画做一个旋转的骰子吧,单独写一个六点的骰子有点单调,加上动画效果就好看多了.至于点数有没有一一对应哪个面我就没整,放到哪个面就是哪个面了.
小点
首先把骰子的小点写出来,后面直接用就可以了
.active {
width: 50px;
height: 50px;
border-radius: 25px;
background-color: red;
}
一点
一点肯定是最好写的啦,用flex然后主轴和侧轴都居中就可以了.代码如下
<style>
.front {
display: flex;
justify-content: center;
align-items: center;
transform: translateZ(200px);
}
</style>
<body>
<div class="front">
<div class="active"></div>
</div>
</body>
二点
二点跟一点一样,侧轴仍然是居中,不同的地方在于,有两个点,然后主轴是space-evenly.代码如下
<style>
.back {
display: flex;
align-items: center;
justify-content: space-evenly;
}
</style>
<body>
<div class="back">
<div class="active"></div>
<div class="active"></div>
</div>
</body>
三点
三点我是用了三个盒子,然后把点分别放在三个盒子里.三个盒子将平面分成三等分.第一个点是从头排列,第二个点是居中排列,第三个点是从末尾开始排列.代码如下
<style>
.left>div {
display: flex;
width: 100%;
height: 33.33%;
}
.left .box1 {
justify-content: start;
padding: 8px;
}
.left .box2 {
justify-content: center;
}
.left .box3 {
justify-content: end;
padding: 8px;
}
</style>
<body>
<div class="left">
<div class="box1">
<div class="active"></div>
</div>
<div class="box2">
<div class="active"></div>
</div>
<div class="box3">
<div class="active"></div>
</div>
</div>
</body>
四点
四点用了两个标准流的盒子,里面分别装两个小点.主轴小点间间距相等,侧轴直接居中就可以了.
<style>
.right .box {
display: flex;
justify-content: space-evenly;
align-items: center;
width: 100%;
height: 50%;
}
</style>
<body>
<div class="right">
<div class="box">
<div class="active"></div>
<div class="active"></div>
</div>
<div class="box">
<div class="active"></div>
<div class="active"></div>
</div>
</div>
</body>
五点
五点用三个标准流的盒子,上下两个点跟二点的做法一样,中间的点跟一点的做法类似.
<style>
.top>div {
display: flex;
width: 100%;
height: 33.33%;
}
.top .box1 {
justify-content: space-evenly;
align-items: center;
}
.top .box2 {
justify-content: center;
align-items: center;
}
.top .box3 {
justify-content: space-evenly;
align-items: center;
}
</style>
<body>
<div class="top">
<div class="box1">
<div class="active"></div>
<div class="active"></div>
</div>
<div class="box2">
<div class="active"></div>
</div>
<div class="box3">
<div class="active"></div>
<div class="active"></div>
</div>
</div>
</body>
六点
六点用三个标准流的盒子,然后里面放两个点.做法跟二点一样.
<style>
.bottom>div {
display: flex;
justify-content: space-evenly;
align-items: center;
width: 100%;
height: 33.33%;
}
</style>
<body>
<div class="bottom">
<div>
<div class="active"></div>
<div class="active"></div>
</div>
<div>
<div class="active"></div>
<div class="active"></div>
</div>
<div>
<div class="active"></div>
<div class="active"></div>
</div>
</div>
</body>
叨叨半天,写完了,下面是完整的代码和效果图,效果图是可以转起来,就截图了一个转了一半的图.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>立体呈现</title>
<style>
* {
box-sizing: border-box;
}
body {
background-color: #f4f4f4;
}
.cube {
position: relative;
width: 200px;
height: 200px;
margin: 400px auto;
transition: all 15s;
transform-origin: top left;
/* 让子盒子开启3d空间 */
transform-style: preserve-3d;
}
.cube>div {
position: absolute;
left: 0;
top: 0;
width: 200px;
height: 200px;
text-align: center;
transition: all 15s;
background-size: contain;
background-color: #fff;
}
.front {
display: flex;
justify-content: center;
align-items: center;
transform: translateZ(200px);
}
.back {
display: flex;
align-items: center;
justify-content: space-evenly;
}
.left {
transform: translateX(-100px) translateZ(100px) rotateY(-90deg);
}
.left>div {
display: flex;
width: 100%;
height: 33.33%;
}
.left .box1 {
justify-content: start;
padding: 8px;
}
.left .box2 {
justify-content: center;
}
.left .box3 {
justify-content: end;
padding: 8px;
}
.right {
transform: translateX(100px) translateZ(100px) rotateY(-90deg);
}
.right .box {
display: flex;
justify-content: space-evenly;
align-items: center;
width: 100%;
height: 50%;
}
.top {
transform: translateY(-100px) translateZ(100px) rotateX(-90deg);
}
.top>div {
display: flex;
width: 100%;
height: 33.33%;
}
.top .box1 {
justify-content: space-evenly;
align-items: center;
}
.top .box2 {
justify-content: center;
align-items: center;
}
.top .box3 {
justify-content: space-evenly;
align-items: center;
}
.bottom {
transform: translateY(100px) translateZ(100px) rotateX(-90deg);
}
.bottom>div {
display: flex;
justify-content: space-evenly;
align-items: center;
width: 100%;
height: 33.33%;
}
/* cube hover 为了看空间感效果 */
.cube:hover {
transform: rotate3d(1, 1, 1, 1080deg);
}
.cube:hover .front {
transform: translateZ(100px);
}
.cube:hover .back {
transform: translateZ(-100px);
}
.cube:hover .left {
transform: translateX(-200px) translateZ(100px) rotateY(-90deg);
}
.cube:hover .right {
transform: translateX(200px) translateZ(100px) rotateY(-90deg);
}
.cube:hover .top {
transform: translateY(-200px) translateZ(100px) rotateX(-90deg);
}
.cube:hover .bottom {
transform: translateY(200px) translateZ(100px) rotateX(-90deg);
}
.active {
width: 50px;
height: 50px;
border-radius: 25px;
background-color: red;
}
</style>
</head>
<body>
<div class="cube">
<div class="front">
<div class="active"></div>
</div>
<div class="back">
<div class="active"></div>
<div class="active"></div>
</div>
<div class="left">
<div class="box1">
<div class="active"></div>
</div>
<div class="box2">
<div class="active"></div>
</div>
<div class="box3">
<div class="active"></div>
</div>
</div>
<div class="right">
<div class="box">
<div class="active"></div>
<div class="active"></div>
</div>
<div class="box">
<div class="active"></div>
<div class="active"></div>
</div>
</div>
<div class="top">
<div class="box1">
<div class="active"></div>
<div class="active"></div>
</div>
<div class="box2">
<div class="active"></div>
</div>
<div class="box3">
<div class="active"></div>
<div class="active"></div>
</div>
</div>
<div class="bottom">
<div>
<div class="active"></div>
<div class="active"></div>
</div>
<div>
<div class="active"></div>
<div class="active"></div>
</div>
<div>
<div class="active"></div>
<div class="active"></div>
</div>
</div>
</div>
</body>
</html>
转载自:https://juejin.cn/post/7087482350606483469