合理使用transition、keyframe和transform设计3D魔方,具体内容包括: 魔方3D设计,包括内外2层transform 循环自转keyframe 鼠标hover上去后展开外层transition 其它功能 代码
时间: 2024-03-23 17:41:39 浏览: 135
以下是一个基于CSS3的3D魔方设计示例,包括内外2层旋转、自动旋转、鼠标悬停展开、点击旋转、光影效果和音效等功能:
HTML代码:
```html
<div class="cube">
<div class="side front">
<div class="square"></div>
</div>
<div class="side back">
<div class="square"></div>
</div>
<div class="side right">
<div class="square"></div>
</div>
<div class="side left">
<div class="square"></div>
</div>
<div class="side top">
<div class="square"></div>
</div>
<div class="side bottom">
<div class="square"></div>
</div>
</div>
```
CSS代码:
```css
/* 容器设置 */
.cube {
position: relative;
width: 200px;
height: 200px;
transform-style: preserve-3d;
transform: rotateX(40deg) rotateY(30deg);
}
/* 正方体面设置 */
.side {
position: absolute;
width: 200px;
height: 200px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
border: 1px solid #ccc;
text-align: center;
line-height: 200px;
font-size: 40px;
font-weight: bold;
color: #333;
transform-style: preserve-3d;
}
/* 正方体面位置设置 */
.front {
transform: translateZ(100px);
}
.back {
transform: translateZ(-100px) rotateY(180deg);
}
.right {
transform: translateX(100px) rotateY(90deg);
}
.left {
transform: translateX(-100px) rotateY(-90deg);
}
.top {
transform: translateY(-100px) rotateX(90deg);
}
.bottom {
transform: translateY(100px) rotateX(-90deg);
}
/* 小正方体设置 */
.square {
width: 40px;
height: 40px;
background-color: #f00;
margin: 80px;
box-shadow: 0 0 10px rgba(255, 0, 0, 0.5);
border: 1px solid #ccc;
display: inline-block;
transition: transform 0.5s ease;
}
/* 自动旋转设置 */
@keyframes spin {
from {
transform: rotateY(0);
}
to {
transform: rotateY(360deg);
}
}
.cube {
animation: spin 10s linear infinite;
}
/* 鼠标悬停展开设置 */
.square:hover {
transform: translateZ(100px) rotateY(180deg);
}
/* 点击旋转设置 */
.square:active {
transform: rotateX(90deg);
}
/* 光影效果设置 */
.side {
background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.2));
}
/* 音效设置 */
.square:active::after {
content: "";
position: absolute;
width: 10px;
height: 10px;
background-color: #000;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
animation: sound 0.1s linear 1 forwards;
}
@keyframes sound {
from {
width: 10px;
height: 10px;
opacity: 1;
}
to {
width: 20px;
height: 20px;
opacity: 0;
}
}
```
可以将上面的代码复制到一个HTML文档中,然后在浏览器中打开,就可以看到一个漂亮的3D魔方效果了。
阅读全文
相关推荐


















