CSS3|两面翻转的盒子案例
要实现 鼠标经过就会盒子翻转
要有一个大盒子,大盒子里面装着两个小盒子 两个小盒子要加定位,其中前面的盒子加上z-index:1;这样就会覆盖后面的盒子 然后让后面的小盒子y轴旋转180deg,这样当让大盒子旋转180deg的时候后面的小盒子可以出现 当让大盒子旋转的时候,小盒子也要保持3d效果,给小盒子的父亲加上transform-style:preserve-3d; 如果想让盒子翻转效果更明显,给body加上透视(要在谷歌浏览器打开)
<!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;
}
.box {
position: relative;
width: 200px;
height: 200px;
margin: 100px auto;
/* 让北面的紫色盒子保留立体空间 */
transform-style: preserve-3d;
transition: all 1s;
}
.box .front,
.box .back {
position: absolute;
width: 100%;
height: 100%;
border-radius: 50%;
font-size: 20px;
line-height: 200px;
text-align: center;
}
.box .front {
background-color: pink;
z-index: 1;
backface-visibility: hidden;
}
.box .back {
background-color: rgb(180, 226, 245);
transform: rotateY(180deg);
}
.box:hover {
transform: rotateY(180deg);
}
</style>
</head>
<body>
<div class="box">
<div class="front">黑马程序员</div>
<div class="back">pink老师等你</div>
</div>
</body>
</html>
补充:发现之前的代码翻转后没有显示之前背面的盒子了.....我也不知道什么原因,但是之前是可以的!!! 但是学到了新的CSS属性,在前面的盒子的css里面加上这个就可以了
backface-visibility: hidden;
