1 回答
TA贡献1874条经验 获得超12个赞
我在 codpen 上找到了这个并对其进行了一些修改,看看它是否可以应用于您的代码
var cube = document.querySelector('.cube');
var currentClass = '';
var side = "front";
function changeSide() {
side = side == "front" ? "top" : "front";
var showClass = 'show-' + side;
if (currentClass) {
cube.classList.remove(currentClass);
}
cube.classList.add(showClass);
currentClass = showClass;
}
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
.scene {
width: 200px;
height: 200px;
border: 1px solid #CCC;
margin: 80px;
perspective: 400px;
}
.cube {
width: 200px;
height: 200px;
position: relative;
transform-style: preserve-3d;
transform: translateZ(-100px);
transition: transform 1s;
}
.cube.show-front {
transform: translateZ(-100px) rotateY( 0deg);
}
.cube.show-top {
transform: translateZ(-100px) rotateX( -90deg);
}
.cube__face {
position: absolute;
width: 200px;
height: 200px;
border: 2px solid black;
line-height: 200px;
font-size: 40px;
font-weight: bold;
color: white;
text-align: center;
}
.cube__face--front {
background-color: red;
}
.cube__face--top {
background-color: blue;
}
.cube__face--front {
transform: rotateY( 0deg) translateZ(100px);
}
.cube__face--top {
transform: rotateX( 90deg) translateZ(100px);
}
label {
margin-right: 10px;
}
<div class="scene">
<div class="cube" onmouseover="changeSide()">
<div class="cube__face cube__face--front">front</div>
<div class="cube__face cube__face--top">top</div>
</div>
</div>
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报