css3實現的立體滾動效果

antzone發表於2017-03-13

分享一段程式碼例項,它實現了柱狀體立體滾動效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
/*容器*/
.container {
  -webkit-perspective: 1000px;
  -moz-perspective: 1000px;
  -ms-perspective: 1000px;
  perspective: 1000px;
}
/*piece盒子*/
.piece-box {
  perspective-origin: 50% 50%;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
}
/*piece盒子*/
.piece-box {
  position: relative;
  width: 200px;
  height: 200px;
  margin: 300px auto;
}
/*piece通用樣式*/
.piece {
  position: absolute;
  width: 200px;
  height: 200px;
  background: red;
  opacity: 0.5;
}
.piece-1 {
  background: #FF6666;
  -webkit-transform: rotateY(0deg) translateZ(173.2px);
  -ms-transform: rotateY(0deg) translateZ(173.2px);
  -o-transform: rotateY(0deg) translateZ(173.2px);
  transform: rotateY(0deg) translateZ(173.2px);
}
.piece-2 {
  background: #FFFF00;
  -webkit-transform: rotateY(60deg) translateZ(173.2px);
  -ms-transform: rotateY(60deg) translateZ(173.2px);
  -o-transform: rotateY(60deg) translateZ(173.2px);
  transform: rotateY(60deg) translateZ(173.2px);
}
.piece-3 {
  background: #006699;
  -webkit-transform: rotateY(120deg) translateZ(173.2px);
  -ms-transform: rotateY(120deg) translateZ(173.2px);
  -o-transform: rotateY(120deg) translateZ(173.2px);
  transform: rotateY(120deg) translateZ(173.2px);
}
.piece-4 {
  background: #009999;
  -webkit-transform: rotateY(180deg) translateZ(173.2px);
  -ms-transform: rotateY(180deg) translateZ(173.2px);
  -o-transform: rotateY(180deg) translateZ(173.2px);
  transform: rotateY(180deg) translateZ(173.2px);
}
.piece-5 {
  background: #FF0033;
  -webkit-transform: rotateY(240deg) translateZ(173.2px);
  -ms-transform: rotateY(240deg) translateZ(173.2px);
  -o-transform: rotateY(240deg) translateZ(173.2px);
  transform: rotateY(240deg) translateZ(173.2px);
}
.piece-6 {
  background: #FF6600;
  -webkit-transform: rotateY(300deg) translateZ(173.2px);
  -ms-transform: rotateY(300deg) translateZ(173.2px);
  -o-transform: rotateY(300deg) translateZ(173.2px);
  transform: rotateY(300deg) translateZ(173.2px);
}
/*piece盒子*/
.piece-box {
  perspective-origin: 50% 50%;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  animation: pieceRotate 5s infinite;
  -moz-animation: pieceRotate 5s; /* Firefox */
  -webkit-animation: pieceRotate 5s; /* Safari and Chrome */
  -o-animation: pieceRotate 5s; /* Opera */
}
/*走馬燈動畫*/
@keyframes pieceRotate {
  0% {
    -webkit-transform: rotateY(0deg);
    -ms-transform: rotateY(0deg);
    -o-transform: rotateY(0deg);
    transform: rotateY(0deg);
  }
  100% {
    -webkit-transform: rotateY(360deg);
    -ms-transform: rotateY(360deg);
    -o-transform: rotateY(360deg);
    transform: rotateY(360deg);
  }
  0% {
    -webkit-transform: rotateY(0deg);
    -ms-transform: rotateY(0deg);
    -o-transform: rotateY(0deg);
    transform: rotateY(0deg);
  }
}
/* Firefox */
@-moz-keyframes pieceRotate {
  0% {
    -webkit-transform: rotateY(0deg);
    -ms-transform: rotateY(0deg);
    -o-transform: rotateY(0deg);
    transform: rotateY(0deg);
  }
  100% {
    -webkit-transform: rotateY(360deg);
    -ms-transform: rotateY(360deg);
    -o-transform: rotateY(360deg);
    transform: rotateY(360deg);
  }
}
/* Safari and Chrome */
 @-webkit-keyframes pieceRotate {
  0% {
    -webkit-transform: rotateY(0deg);
    -ms-transform: rotateY(0deg);
    -o-transform: rotateY(0deg);
    transform: rotateY(0deg);
  }
  100% {
    -webkit-transform: rotateY(360deg);
    -ms-transform: rotateY(360deg);
    -o-transform: rotateY(360deg);
    transform: rotateY(360deg);
  }
}
/* Opera */
@-o-keyframes pieceRotate {
  0% {
    -webkit-transform: rotateY(0deg);
    -ms-transform: rotateY(0deg);
    -o-transform: rotateY(0deg);
    transform: rotateY(0deg);
  }
  100% {
    -webkit-transform: rotateY(360deg);
    -ms-transform: rotateY(360deg);
    -o-transform: rotateY(360deg);
    transform: rotateY(360deg);
  }
}
</style>
</head>
<body>
  <div class="container">
    <div class="piece-box">
      <div class="piece piece-1"></div>
      <div class="piece piece-2"></div>
      <div class="piece piece-3"></div>
      <div class="piece piece-4"></div>
      <div class="piece piece-5"></div>
      <div class="piece piece-6"></div>
    </div>
  </div>
</body>
</html>

相關文章