简单模仿掷骰子的情景,正面的数字是随机的
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>掷骰子</title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
background-color: #000;
}
.wrap {
position: absolute;
height: 100%;
-webkit-transform-style: preserve-3d;
-webkit-animation: myanimal 6s forwards;
-webkit-perspective: 0px;
}
/* 先将骰子各个面显示在界面中心点 */
.num {
position: absolute;
height: 200px;
width: 200px;
font-size: 48px;
text-align: center;
line-height: 200px;
top: 50%;
left: 50%;
color: wheat;
margin: -100px 0 0 -100px;
}
/* 移动六个面的位置,形成立体空间 */
.num6 {
-webkit-transform: translatez(100px);
}
.num1 {
-webkit-transform: translatez(-100px);
}
.num4 {
-webkit-transform: rotatex(90deg) translatez(100px);
}
.num3 {
-webkit-transform: rotatex(-90deg) translatez(100px);
}
.num5 {
-webkit-transform: rotatey(-90deg) translatez(100px);
}
.num2 {
-webkit-transform: rotatey(90deg) translatez(100px);
}
/* 画出为每个面点数 */
[class$="dice"] {
box-sizing: border-box;
width: 100%;
height: 100%;
background: #e7e7e7;
border: 1px solid #000;
padding: 10%;
-webkit-animation: diceanimal 1s forwards;
}
.circle {
display: block;
width: 24px;
height: 24px;
border-radius: 50%;
background: #000;
}
.first-dice {
display: flex;
align-items: center;
justify-content: center;
}
.second-dice,
.third-dice {
display: flex;
justify-content: space-between;
}
.second-dice .circle:nth-of-type(2) {
align-self: flex-end;
}
.third-dice .circle:nth-of-type(2) {
align-self: center;
}
.third-dice .circle:nth-of-type(3) {
align-self: flex-end;
}
.fourth-dice,
.fifth-dice,
.sixth-dice {
display: flex;
justify-content: space-between;
}
.fourth-dice .column,
.fifth-dice .column,
.sixth-dice .column {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.fifth-dice .column:nth-of-type(2) {
justify-content: center;
}
/* 为了实现随机显示数字,动画的代码在Js里面添加 */
/* @-webkit-keyframes myanimal {
0% {
-webkit-transform: rotateX(0) rotateY(0) rotateZ(0);
-webkit-transform-origin: 40% 40%;
left: 0%;
top: -50%;
}
40% {
left: 40%;
top: 30%;
}
70% {
left: 50%;
top: 10%;
}
100% {
-webkit-transform: rotateX(180deg) rotateY(180deg) rotateZ(180deg);
-webkit-transform-origin: center center;
left: 50%;
top: 25%;
}
}*/
</style>
</head>
<body>
<!-- 前6后1 上4下3 左5右2 -->
<div class="wrap">
<!-- 点数1 -->
<div class="num1 num">
<div class="first-dice">
<span class="circle"></span>
</div>
</div>
<!-- 点数2 -->
<div class="num2 num">
<div class="second-dice">
<span class="circle"></span>
<span class="circle"></span>
</div>
</div>
<!-- 点数3 -->
<div class="num3 num">
<div class="third-dice">
<span class="circle"></span>
<span class="circle"></span>
<span class="circle"></span>
</div>
</div>
<!-- 点数4 -->
<div class="num4 num">
<div class="fourth-dice">
<div class="column">
<span class="circle"></span>
<span class="circle"></span>
</div>
<div class="column">
<span class="circle"></span>
<span class="circle"></span>
</div>
</div>
</div>
<!-- 点数5 -->
<div class="num5 num">
<div class="fifth-dice">
<div class="column">
<span class="circle"></span>
<span class="circle"></span>
</div>
<div class="column">
<span class="circle"></span>
</div>
<div class="column">
<span class="circle"></span>
<span class="circle"></span>
</div>
</div>
</div>
<!-- 点数6 -->
<div class="num6 num">
<div class="sixth-dice">
<div class="column">
<span class="circle"></span>
<span class="circle"></span>
<span class="circle"></span>
</div>
<div class="column">
<span class="circle"></span>
<span class="circle"></span>
<span class="circle"></span>
</div>
</div>
</div>
</div>
<script>
//设置旋转的角度随机,必须是90的倍数结束时才能是立正的状态,
//以90为基数是为了保证骰子会转动
let endRotateX = 90 + Math.floor(Math.random() * 5) * 90;
endRotateY = 90 + Math.floor(Math.random() * 5) * 90;
endRotateZ = 90 + Math.floor(Math.random() * 5) * 90;
//实现动画效果
document.styleSheets[0].insertRule(
"@-webkit-keyframes myanimal {" +
" 0% {-webkit-transform: rotateX(0) rotateY(0) rotateZ(0);" +
// "-webkit-transform-origin: 40% 40%;" +
"left: 0%;top: -50%; }" +
"30% {left: 30%;top: 20%;-webkit-transform: rotateX(10)}" +
"75% {left: 48%;top: 15%;-webkit-transform: rotateX(10)}" +
"95% {left: 50%;top: 20%;-webkit-transform: rotateX(90) rotateY(90) rotateZ(10);}" +
"100% {-webkit-transform: rotateX(" + endRotateX + "deg) rotateY(" + endRotateY + "deg) rotateZ(" + endRotateZ + "deg);" +
"-webkit-transform-origin: center center;" +
"left: 50%;top: 20%;}}"
)
</script>
</body>
</html>
效果图