之前一直对css动画和过渡这块写的少,今天那一个转动的骰子来练练手
代码块 :
<!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>
.container {
margin: 80px;
padding: 20px;
width: 200px;
height: 200px;
position: relative;
perspective: 700px;
}
.cube {
animation: run 5s;
animation-iteration-count: infinite;
width: 200px;
height: 200px;
/* 3D透视 */
transform-style: preserve-3d;
}
@keyframes run {
0% {
transform: rotateX(0deg) rotateY(0) rotateZ(0deg);
}
50% {
transform: rotateX(360deg) rotateY(180deg) rotateZ(360deg);
}
100% {
transform: rotateX(360deg) rotateY(360deg) rotateZ(360deg);
}
}
.cube div {
width: 200px;
height: 200px;
position: absolute;
line-height: 200px;
font-size: 50px;
text-align: center;
}
.front {
background-color: rgba(185, 138, 167, 0.527);
}
.back {
transform: translateZ(-200px) rotateY(180deg);
background-color: rgba(255, 2, 0, 0.3);
;
}
.left {
transform: translateX(-100px) translateZ(-100px) rotateY(-90deg);
background-color: rgba(55, 21, 12, 0.3);
;
}
.right {
transform: translateX(100px) translateZ(-100px) rotateY(90deg);
background-color: rgba(25, 55, 110, 0.3);
;
}
.top {
transform: translateY(-100px) translateZ(-100px) rotateX(90deg);
background-color: rgba(255, 255, 22, 0.3);
;
}
.bottom {
transform: translateY(100px) translateZ(-100px) rotateX(-90deg);
background-color: rgba(0, 255, 0, 0.3);
;
}
</style>
</head>
<body>
<div class="container">
<div class="cube">
<div class="front">1</div>
<div class="back">2</div>
<div class="left">3</div>
<div class="right">4</div>
<div class="top">5</div>
<div class="bottom">6</div>
</div>
</div>
</body>
</html>
实现效果 :