可以拖動立方體3D效果程式碼例項

antzone發表於2017-04-13

本章節分享一段程式碼例項,它實現了可以拖動的立方體3D效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
* {
  padding: 0;
  margin: 0;
}
#div1 {
  width: 200px;
  height: 200px;
  background: #ccc;
  position: relative;
  margin: 100px auto;
  transform-style: preserve-3d;
  transform: perspective(800px) rotateX(0deg) rotateY(0deg);
}
#div1 div {
  position: absolute;
  left: 0;
  top: 0;
  width: 198px;
  height: 198px;
  border: 1px solid #000;
  line-height: 198px;
  text-align: center;
  background: green;
  opacity: 1;
}
#div1 .front {
  transform: translateZ(100px);
}
#div1 .back {
  transform: translateZ(-100px);
}
#div1 .left {
  transform: rotateY(-90deg) translateZ(100px);
}
#div1 .right {
  transform: rotateY(+90deg) translateZ(100px);
}
#div1 .top {
  transform: rotateX(90deg) translateZ(100px);
}
#div1 .bottom {
  transform: rotateX(-90deg) translateZ(100px);
}
</style>
<script>
window.onload = function () {
  var oDiv = document.getElementById('div1');
 
  var x = 0;
  var lastX = 0;
  var speedX = 0;
  var y = 0;
  var lastY = 0;
  var speedY = 0;
  var timer = null;
 
  oDiv.onmousedown = function (ev) {
    clearInterval(timer)
    disX = ev.clientX - y;  // 減去之前的位置
    disY = ev.clientY - x;
    document.onmousemove = function (ev) {
      clearInterval(timer);
      y = ev.clientX - disX; //y軸旋轉
      x = ev.clientY - disY;  //x軸旋轉
 
      speedY = y - lastY; //速度
      lastY = y;
 
      speedX = x - lastX;
      lastX = x;
 
      oDiv.style.transform = 'perspective(800px) rotateX(' + -x + 'deg) rotateY(' + y + 'deg)';
    };
    document.onmouseup = function () {
      clearInterval(timer);
      document.onmousemove = null;
      document.onmouseup = null;
 
      timer = setInterval(function () {
        y += speedY;
        x += speedX;
        speedY *= 0.95;
        speedX *= 0.95; //摩擦
 
 
        if (Math.abs(speedY) < 1) speedY = 0;
        if (Math.abs(speedX) < 1) speedX = 0;
        if (Math.abs(speedY) == 0 && speedX == 0) clearInterval(timer)
 
        oDiv.style.transform = 'perspective(800px) rotateX(' + -x + 'deg) rotateY(' + y + 'deg)';
      }, 30)
    };
    return false;
  };
}
</script>
</head>
<body>
  <div id="div1">
    <div class="front">前面</div>
    <div class="back">後面</div>
    <div class="left">左邊</div>
    <div class="right">右邊</div>
    <div class="top">上面</div>
    <div class="bottom">下面</div>
  </div>
</body>
</html>

相關文章