👏transform-style+transform实现多个小方块,速速来Get吧~
🥇文末分享源代码。记得点赞+关注+收藏!
:root {
--w: 30px;
}
<div class="container"></container>
.container {
height: 150px;
width: 150px;
padding: 30px;
box-shadow: 0 12px 20px 6px rgb(104 112 118 / 0.2);
border-radius: 15px;
transform-style: preserve-3d;
}
<div class="container">
<div class="square"></div>
...// 共9个
<div class="square"></div>
</div>
.container {
- height: 150px;
- width: 150px;
- padding: 30px;
- border-radius: 15px;
+ height: calc(var(--w) * 5);
+ width: calc(var(--w) * 5);
+ padding: var(--w);
+ border-radius: calc(var(--w) / 2);
+ display: flex;
+ flex-wrap: wrap;
box-shadow: 0 12px 20px 6px rgb(104 112 118 / 0.2);
transform-style: preserve-3d;
}
.square {
height: var(--w);
width: var(--w);
border: 1px solid red;
background-color: rgb(207, 52, 40);
position: relative;
transform-style: preserve-3d;
}
.square {
+ background-color: rgb(207, 52, 40);
}
.container {
+ transform: rotateX(45deg) rotateZ(45deg);
}
.square:after,
.square:before {
content: "";
position: absolute;
height: var(--w);
width: var(--w);
left: 0;
top: 0;
}
.square:before {
background-color: rgb(56, 35, 35);
transform: rotateY(-90deg);
}
.square:before {
+ transform-origin: right center;
}
.square:after {
background-color: rgb(80, 39, 36);
transform: rotateX(0deg);
}
.square:after {
+transform-origin: right bottom;
}
.square {
+ transform: translateZ(var(--w));
}
.square:hover {
cursor: pointer;
filter: brightness(2.1);
}
.square {
+ box-shadow: calc(var(--w) * 3) calc(var(--w) * 3) 15px rgba(0, 0, 0, 0.2);
}
.square {
+ animation: beating 1s infinite;
}
@keyframes beating {
50% {
transform: translateZ(calc(var(--w) / 2));
}
}
<div class="container">
<div class="square" style="--d: 1"></div>
..// 依次
<div class="square" style="--d: 9"></div>
</div>
.square {
- border: 1px solid red;
+ animation-delay: calc(0.05s * var(--d));
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<style>
:root {
--w: 30px;
}
.container {
height: calc(var(--w) * 5);
width: calc(var(--w) * 5);
padding: var(--w);
box-shadow: 0 12px 20px 6px rgb(104 112 118 / 0.2);
border-radius: calc(var(--w) / 2);
display: flex;
flex-wrap: wrap;
transform-style: preserve-3d;
transform: rotateX(45deg) rotateZ(45deg);
}
.square {
height: var(--w);
width: var(--w);
background-color: rgb(207, 52, 40);
position: relative;
transform-style: preserve-3d;
transform: translateZ(var(--w));
box-shadow: calc(var(--w) * 3) calc(var(--w) * 3) 15px rgba(0, 0, 0, 0.2);
animation: beating 1s infinite;
animation-delay: calc(0.05s * var(--d));
}
.square:hover {
cursor: pointer;
filter: brightness(2.1);
}
.square:after,
.square:before {
content: "";
position: absolute;
height: var(--w);
width: var(--w);
left: 0;
top: 0;
}
.square:before {
background-color: rgb(56, 35, 35);
transform: rotateY(-90deg);
transform-origin: right center;
}
.square:after {
background-color: rgb(80, 39, 36);
transform: rotateX(90deg);
transform-origin: right bottom;
}
@keyframes beating {
50% {
transform: translateZ(calc(var(--w) / 2));
}
}
</style>
<body>
<div class="container">
<div class="square" style="--d: 1"></div>
<div class="square" style="--d: 2"></div>
<div class="square" style="--d: 3"></div>
<div class="square" style="--d: 4"></div>
<div class="square" style="--d: 5"></div>
<div class="square" style="--d: 6"></div>
<div class="square" style="--d: 7"></div>
<div class="square" style="--d: 8"></div>
<div class="square" style="--d: 9"></div>
</div>
</body>
</html>