CSS3 3d旋轉魔方效果

admin發表於2017-02-23

分享一段程式碼例項,它使用css3實現了3d旋轉魔方效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
* {
  margin: 0;
  padding: 0;
}
html {
  height: 100%;
  background: radial-gradient(#adbf41, #328a26);
}
.stage {
  width: 20em;
  height: 20em;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  perspective: 1000px;
  -webkit-perspective: 1000px;
  perspective: 1000px;
}
.cube * {
  position: absolute;
  width: 100%;
  height: 100%;
  box-shadow: 0 0 5em rgba(255, 255, 255, 0.4);
  background: -webkit-linear-gradient(left, rgba(85, 85, 85, 1) 0px, rgba(85, 85, 85, 1) 3px,rgba(0, 0, 0, 0) 0px), 
              -webkit-linear-gradient(top, rgba(85, 85, 85, 1) 0px, rgba(85, 85, 85, 1) 3px, rgba(0, 0, 0, 0) 0px);
  background-size: 2.5em 2.5em;
  border: 2px solid rgba(85, 85, 85, 1);
  border-top: none;
  border-left: none;
  border-radius: 0.2em;
}
.font {
  background-color: rgba(255, 255, 255, 1);
  transform: translateZ(10em);
}
.back {
  background-color: rgba(255, 213, 0, 1);
  transform: rotateX(180deg) translateZ(10em);
}
.left {
  background-color: rgba(0, 70, 173, 1);
  transform: rotateY(-90deg) translateZ(10em);
}
.right {
  background-color: rgba(0, 155, 72, 1);
  transform: rotateY(90deg) translateZ(10em);
}
.top {
  background-color: rgba(183, 18, 52, 1);
  transform: rotateX(90deg) translateZ(10em);
}
.bottom {
  background-color: rgba(255, 88, 0, 1);
  transform: rotateX(-90deg) translateZ(10em);
}
.cube {
  position: absolute;
  width: 100%;
  height: 100%;
  -o-animation: spin linear 6s infinite, bigchange linear 6s;
  animation: spin linear 6s infinite, bigchange linear 6s;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d; /*使被轉換的子元素保留其 3D 轉換:*/
}
@-webkit-keyframes spin {
  0% {
    -webkit-transform: translateZ(-10em) rotateX(0) rotateY(0deg);
    -ms-transform: translateZ(-10em) rotateX(0) rotateY(0deg);
    -o-transform: translateZ(-10em) rotateX(0) rotateY(0deg);
    transform: translateZ(-10em) rotateX(0) rotateY(0deg);
  }
  100% {
    -webkit-transform: translateZ(-10em) rotateX(360deg) rotateY(360deg);
    -ms-transform: translateZ(-10em) rotateX(360deg) rotateY(360deg);
    -o-transform: translateZ(-10em) rotateX(360deg) rotateY(360deg);
    transform: translateZ(-10em) rotateX(360deg) rotateY(360deg);
  }
}
@-webkit-keyframes bigchange {
  0% {
    width: 0em;
    height: 0em;
  }
  100% {
    width: 20em;
    height: 20em;
  }
}
</style>
</head>
<body>
  <div class="stage">
    <div class="cube">
      <div class="font"></div>
      <div class="back"></div>
      <div class="left"></div>
      <div class="right"></div>
      <div class="top"></div>
      <div class="bottom"></div>
    </div>
  </div>
</body>
</html>

相關文章