原理是使用css3中的3D旋转属性,需要一个父盒子和两个子盒子,将第一个子盒子作为正面,第二个子盒子作为反面,所以先把第二个子盒子沿y轴旋转180度。然后设置父盒子的hover属性,当鼠标悬停时,让父盒子反转180度即可。
让人很迷惑的一点是,这个效果在chrome上可以正常实现,但是在firefox上,第一个子盒子永远处于上方,即使翻到反面也显示第一个盒子处在上面,很奇怪。。。
效果展示页面:3D旋转
<!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>Document</title>
<style>
body{
perspective: 500px;
}
div{
width: 100px;
height: 100px;
margin: 100px auto;
text-align: center;
}
.box{
transform-style:preserve-3d ;
position: relative;
width: 300px;
height: 300px;
margin:100 px auto;
transition: all .4s;
}
.box:hover{
transform:rotateY(180deg);
}
.front,.back{
width: 100%;
height: 100%;
transition: all 1s;
border-radius: 50%;
position:absolute;
font-size: 30px;
color:#fff;
line-height: 300px;
}
.front{
background-color: pink;
z-index: 1;
}
.back{
background-color: purple;
transform:rotateY(180deg);
}
</style>
</head>
<body>
<div class="box">
<div class="front">这是正面</div>
<div class="back">这是反面</div>
</div>
</body>
</html>