css3實現的3D立體旋轉效果

antzone發表於2017-03-07

分享一段程式碼例項,它實現了3D立體旋轉效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
html{
  background:linear-gradient(#000000 0%,#0bb20c 100%);
  height:100%;
}
.wrap{
  margin-top:250px;
  perspective: 1000px;
}
@keyframes tuoluo{
  0%{
    transform: rotateY(0deg)  rotateX(0deg);
  }
  100%{
    transform: rotateY(360deg)  rotateX(360deg);
  }
}
.wrap .cube{
  margin:auto;
  width:200px;
  height:200px;
  position:relative;
  transform-style:preserve-3d;
  /*迴圈動畫*/
  animation: tuoluo 3s linear infinite;
}
.cube div{
  width: 100%;
  height: 100%;
  font-size: 36px;
  background: rgba(255, 25, 47, 0.5);
  color: #fff;
  border: 1px solid #ffffff;
  text-align: center;
  line-height: 200px;
  position: absolute;
  transition: all .2s ease-in;
}
.cube span{
  display:block;
  width:50%;
  height:50%;
  background:rgba(26, 3, 4, 1);
  border:1px solid #000000;
  text-align:center;
  position:absolute;
  top:25%;
  left:25%;
}
.cube .out-front{
  transform: translateZ(100px);
}
.cube .out-back{
  transform: rotateY(180deg)  translateZ(100px);
}
.cube .out-left{
  transform: rotateY(270deg) rotateX(0deg)  translateZ(100px);
}
.cube .out-right{
  transform: rotateY(90deg) rotateX(0deg)  translateZ(100px);
}
.cube .out-top{
  transform: rotateY(0deg) rotateX(90deg)  translateZ(100px);
}
.cube .out-bottom{
  transform: rotateY(0deg) rotateX(-90deg)  translateZ(100px);
}
.cube:hover .out-front{
  transform: translateZ(200px);
}
.cube:hover .out-back{
  transform: rotateY(180deg)  translateZ(200px);
}
.cube:hover .out-left{
  transform: rotateY(270deg) rotateX(0deg)  translateZ(200px);
}
.cube:hover .out-right{
  transform: rotateY(90deg) rotateX(0deg)  translateZ(200px);
}
.cube:hover .out-top{
  transform: rotateY(0deg) rotateX(90deg)  translateZ(200px);
}
.cube:hover .out-bottom{
  transform: rotateY(0deg) rotateX(-90deg)  translateZ(200px);
}
.cube .in-front{
  transform: translateZ(50px);
}
.cube .in-back{
  transform: rotateY(180deg)  translateZ(50px);
}
.cube .in-left{
  transform: rotateY(270deg) rotateX(0deg)  translateZ(50px);
}
.cube .in-right{
  transform: rotateY(90deg) rotateX(0deg)  translateZ(50px);
}
.cube .in-top{
  transform: rotateY(0deg) rotateX(90deg)  translateZ(50px);
}
.cube .in-bottom{
  transform: rotateY(0deg) rotateX(-90deg)  translateZ(50px);
}
</style>
</head>
<body>
<div class="wrap">
  <div class="cube">
    <div class="out-front">前</div>
    <div class="out-back">後</div>
    <div class="out-left">左</div>
    <div class="out-right">右</div>
    <div class="out-top">上</div>
    <div class="out-bottom">下</div>
 
    <span class="in-front"></span>
    <span class="in-back"></span>
    <span class="in-left"></span>
    <span class="in-right"></span>
    <span class="in-top"></span>
    <span class="in-bottom"></span>
  </div>
</div>
</body>
</html>

相關文章