JavaScript元素上下彈性運動

admin發表於2018-05-24

分享一段程式碼例項,它實現了元素上下彈性運動效果,類似於一個小球能夠上下彈動的現象。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
body {
  margin: 0;
  padding: 0;
}
#box {
  width: 50px;
  height: 50px;
  background: red;
  position: absolute;
  left: 0;
  top: 0;
  margin: 0;
}
</style>
<script>
window.onload = function() {
  oDiv = document.getElementById('box');
  var l = 0;
  var t = 0;
  var ispeedX = 0;
  var ispeedY = 0;
  var timer = null;
  oDiv.onmousedown = function(ev) {
    clearInterval(timer);
    var ev = ev || event;
    var disX = ev.clientX - oDiv.offsetLeft;
    var disY = ev.clientY - oDiv.offsetTop;
    document.onmousemove = function(ev) {
        var ev = ev || event;
        oDiv.style.left = ev.clientX - disX + 'px';
        oDiv.style.top = ev.clientY - disY + 'px';
 
        ispeedX = oDiv.offsetLeft - l;
        ispeedY = oDiv.offsetTop - t;
        l = oDiv.offsetLeft;
        t = oDiv.offsetTop;
      } //onmousemove
 
    document.onmouseup = function() {
      document.onmousemove = null;
      clearInterval(timer);
      boomMove(oDiv);
 
      function boomMove(obj) {
 
        var clientX = document.documentElement.clientWidth - obj.offsetWidth || document.body.clientWidth - obj.offsetWidth;
        var clientY = document.documentElement.clientHeight - obj.offsetHeight || document.body.clientHeight - obj.offsetHeight;
 
        timer = setInterval(function() {
 
          ispeedY += 3;
 
          var x = obj.offsetLeft + ispeedX;
          var t = obj.offsetTop + ispeedY;
 
 
 
          if (x > clientX) {
            x = clientX;
            ispeedX *= -1;
          };
          if (x < 0) {
            x = 0;
            ispeedX *= -1;
          };
          if (t > clientY) {
            t = clientY;
 
            ispeedY *= -0.8;
            ispeedX *= 0.9;
          };
          if (t < 0) {
            t = 0;
            ispeedY *= -1
          };
 
 
          obj.style.left = x + 'px';
          obj.style.top = t + 'px';
 
          if (Math.abs(ispeedX) < 1) {
            ispeedX = 0;
          };
          if (Math.abs(ispeedY) < 1) {
            ispeedY = 0;
          };
          if (ispeedX == 0 && ispeedY == 0 && oDiv.offsetTop == clientY) {
            clearInterval(timer);
          }
 
        }, 30);
      }
    }
  }
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>

相關文章