多方运动+碰撞检测+重力加速+能量流失
    重力的速度 X = 初始速度 + 路程
    重力速度Y = 初始速度 + 重力加速的速度 + 路程
    每次碰撞都会有能量流失

    1. <div class="box" style="width: 100px; height: 100px; background: red; position: absolute;"></div>
    2. <script>
    3. var box = document.getElementsByClassName('box')[0];
    4. var timer = null;
    5. function move(dom){
    6. clearInterval(timer);
    7. var iSpeedX = 6;
    8. var iSpeedY = 8;
    9. var gravity = 3; //重力加速
    10. dom.timer = setInterval(() => {
    11. iSpeedY = iSpeedY + gravity; //重力加速
    12. var newTop = dom.offsetTop + iSpeedY;
    13. var newLeft = dom.offsetLeft + iSpeedX;
    14. // 碰撞检测
    15. if(newTop >= document.documentElement.clientHeight - dom.clientHeight){
    16. iSpeedY*= -1;
    17. iSpeedX *= 0.8; //能量流失
    18. iSpeedY *= 0.8; //能量流失
    19. newTop = document.documentElement.clientHeight - dom.clientHeight;
    20. }
    21. if(newTop <= 0){
    22. iSpeedY *= -1;
    23. newTop = 0;
    24. iSpeedX *= 0.8;
    25. iSpeedY *= 0.8;
    26. }
    27. if(newLeft >= document.documentElement.clientWidth - dom.clientWidth){
    28. iSpeedX*= -1;
    29. iSpeedX *= 0.8;
    30. iSpeedY *= 0.8;
    31. newLeft = document.documentElement.clientWidth - dom.clientWidth;
    32. }
    33. if(newLeft <= 0){
    34. iSpeedX *= -1;
    35. iSpeedX *= 0.8;
    36. iSpeedY *= 0.8;
    37. newLeft = 0;
    38. }
    39. if(Math.abs(iSpeedX) < 1){
    40. iSpeedX = 0;
    41. }
    42. if(Math.abs(iSpeedY) < 1){
    43. iSpeedY = 0;
    44. }
    45. if(iSpeedY == 0 && iSpeedX == 0 && newTop == document.documentElement.clientHeight - dom.clientHeight){
    46. clearInterval(dom.timer)
    47. console.log(1)
    48. }else{
    49. dom.style.top = newTop + 'px';
    50. dom.style.left = newLeft + 'px';
    51. }
    52. }, (30));
    53. }
    54. box.onclick = function(){
    55. move(this);
    56. }
    57. </script>

    小 demo

    1. <body>
    2. <div class="box" style="width: 100px; height: 100px; background: red; position: absolute;"></div>
    3. <script>
    4. var box = document.getElementsByClassName('box')[0];
    5. var timer = null;
    6. box.onmousedown = function(e){
    7. clearInterval(this.timer);
    8. var evnent = event || e;
    9. var disX = event.clientX - this.offsetLeft; //clientX 获取的是光标位于屏幕的位置
    10. var disY = event.clientY - this.offsetTop;
    11. var iSpeedX = 0;
    12. var iSpeedY = 0;
    13. var lastX = 0;
    14. var lastY = 0;
    15. var self = this;
    16. var ev = evnent;
    17. document.onmousemove = function(e){
    18. var evnent = evnent || e;
    19. var newLeft = event.clientX - disX;
    20. var newTop = event.clientY - disY;
    21. iSpeedX = newLeft -lastX;
    22. iSpeedY = newTop - lastY;
    23. lastX = newLeft;
    24. lastY = newTop;
    25. self.style.left = newLeft + 'px';
    26. self.style.top = newTop + 'px';
    27. }
    28. document.onmouseup = function(e){
    29. document.onmousemove = null;
    30. document.onmouseup = null;
    31. move(self, iSpeedX,iSpeedY)
    32. }
    33. }
    34. function move(dom,iSpeedX,iSpeedY){
    35. clearInterval(timer);
    36. var gravity = 3; //重力加速
    37. dom.timer = setInterval(() => {
    38. iSpeedY = iSpeedY + gravity; //重力加速
    39. var newTop = dom.offsetTop + iSpeedY;
    40. var newLeft = dom.offsetLeft + iSpeedX;
    41. // 碰撞检测
    42. if(newTop >= document.documentElement.clientHeight - dom.clientHeight){
    43. iSpeedY*= -1;
    44. iSpeedX *= 0.8; //能量流失
    45. iSpeedY *= 0.8; //能量流失
    46. newTop = document.documentElement.clientHeight - dom.clientHeight;
    47. }
    48. if(newTop <= 0){
    49. iSpeedY *= -1;
    50. newTop = 0;
    51. iSpeedX *= 0.8;
    52. iSpeedY *= 0.8;
    53. }
    54. if(newLeft >= document.documentElement.clientWidth - dom.clientWidth){
    55. iSpeedX*= -1;
    56. iSpeedX *= 0.8;
    57. iSpeedY *= 0.8;
    58. newLeft = document.documentElement.clientWidth - dom.clientWidth;
    59. }
    60. if(newLeft <= 0){
    61. iSpeedX *= -1;
    62. iSpeedX *= 0.8;
    63. iSpeedY *= 0.8;
    64. newLeft = 0;
    65. }
    66. if(Math.abs(iSpeedX) < 1){
    67. iSpeedX = 0;
    68. }
    69. if(Math.abs(iSpeedY) < 1){
    70. iSpeedY = 0;
    71. }
    72. if(iSpeedY == 0 && iSpeedX == 0 && newTop == document.documentElement.clientHeight - dom.clientHeight){
    73. clearInterval(dom.timer)
    74. console.log(1)
    75. }else{
    76. dom.style.top = newTop + 'px';
    77. dom.style.left = newLeft + 'px';
    78. }
    79. }, (30));
    80. }
    81. </script>