js实现div色块碰撞

前端之家收集整理的这篇文章主要介绍了js实现div色块碰撞前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

描述:

生成两个div色块,一个可以拖动,一个不可以,用拖动的去撞击不能动的,会将这个色块随机撞到一个位置,改变颜色。

效果

js实现div色块碰撞


js实现div色块碰撞


实现:

js文件

  1. function DragObj(_obj) {
  2. this.obj=_obj;
  3. this.point={};
  4. this.moveBool=false;
  5. this.obj.self=this;
  6. this.obj.addEventListener("mousedown",this.mouseHandler);
  7. this.obj.addEventListener("mouseup",this.mouseHandler);
  8. this.obj.addEventListener("mousemove",this.mouseHandler);
  9. this.obj.addEventListener("mouseleave",this.mouseHandler);
  10. }
  11. DragObj.prototype={
  12. mouseHandler:function (e) {
  13. if(!e){
  14. e=window.event;
  15. }
  16. if(e.type=="mousedown"){
  17. this.self.moveBool=true;
  18. this.self.point.x=e.offsetX;
  19. this.self.point.y=e.offsetY;
  20. }else if(e.type=="mousemove"){
  21. if(!this.self.moveBool) return;
  22. this.self.obj.style.left=(e.clientX-this.self.point.x)+"px"
  23. this.self.obj.style.top=(e.clientY-this.self.point.y)+"px"
  24. }else if(e.type=="mouseup" || e.type=="mouseleave"){
  25. this.self.moveBool=false;
  26. }
  27. },removeEvent:function () {
  28.  
  29. this.obj.removeEventListener("mousedown",this.mouseHandler);
  30. this.obj.removeEventListener("mouseup",this.mouseHandler);
  31. this.obj.removeEventListener("mousemove",this.mouseHandler);
  32. this.obj.removeEventListener("mouseleave",this.mouseHandler);
  33.  
  34. this.obj=null;
  35. this.point=null;
  36. }
  37. };
  38. var HitTest=HitTest || (function () {
  39. return {
  40. to:function (display0,display1) {
  41. var rect=display0.getBoundingClientRect();
  42. var rect1=display1.getBoundingClientRect();
  43. if(rect.left>=rect1.left &&
  44. rect.left<=rect1.left+rect1.width
  45. && rect.top>=rect1.top && rect.top<=rect1.top+rect1.height){
  46. return true;
  47. }
  48. if(rect.left+rect.width>=rect1.left && rect.left+rect.width<=rect1.left+rect1.width && rect.top>=rect1.top
  49. && rect.top<=rect1.top+rect1.height){
  50. return true;
  51. }
  52. if(rect.left>=rect1.left &&
  53. rect.left<=rect1.left+rect1.width
  54. && rect.top+rect.height>=rect1.top &&
  55. rect.top+rect.height<=rect1.top+rect1.height){
  56. return true;
  57. }
  58. if(rect.left+rect.width>=rect1.left && rect.left+rect.width<=rect1.left+rect1.width &&
  59. rect.top+rect.height>=rect1.top
  60. && rect.top+rect.height<=rect1.top+rect1.height){
  61. return true;
  62. }
  63. }
  64. }
  65. })();
  66. var LoadImg=LoadImg || (function () {
  67. return {
  68. load:function (listSrc,callBack) {
  69. this.callBack=callBack;
  70. this.listSrc=listSrc;
  71. this.num=0;
  72. this.imgArr=[];
  73. var img=new Image();
  74. img.addEventListener("load",this.loadHandler.bind(this));
  75. img.src=listSrc[0];
  76. },loadHandler:function (e) {
  77. if(!e){
  78. e=window.event;
  79. }
  80. e.currentTarget.removeEventListener
  81. ("load",this.loadHandler.bind(this));
  82. this.imgArr[this.num]=e.currentTarget;
  83. if(this.num==this.listSrc.length-1){
  84. this.callBack(this.imgArr)
  85. return;
  86. }
  87. var img=new Image();
  88. this.num++;
  89. img.addEventListener("load",this.loadHandler.bind(this));
  90. img.src=this.listSrc[this.num];
  91. }
  92. }
  93. })();

html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <Meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. div{
  8. width: 200px;
  9. height: 200px;
  10. position: absolute;
  11. }
  12. </style>
  13. <script src="js/dragObj.js"></script>
  14. </head>
  15. <body>
  16. <div id="div0"></div>
  17. <div id="div1"></div>
  18. <script>
  19. window.addEventListener("mousedown",mousedownHandler);//生成一个
  20. function mousedownHandler(e) {
  21. if(!e){
  22. e=window.event;
  23. }
  24. e.preventDefault();
  25. }
  26. var div0=document.getElementById("div0");
  27. var div1=document.getElementById("div1");
  28. div0.style.backgroundColor=randomColor();
  29. div1.style.backgroundColor=randomColor();
  30.  
  31. randomPosition(div0)
  32. randomPosition(div1)
  33. var drag0=new DragObj(div0);
  34. div0.addEventListener("mousemove",mouseMoveHandler)
  35. function randomColor() {
  36. var str="#";
  37. for(var i=0;i<3;i++){
  38. var color=Math.floor(Math.random()*256).toString(16);
  39. if(color.length<2){
  40. color="0"+color;
  41. }
  42. str+=color;
  43. }
  44. return str;
  45. }
  46.  
  47. function randomPosition(div) {
  48. div.style.left=Math.random()*(document.documentElement.clientWidth-50)+"px";
  49. div.style.top=Math.random()*(document.documentElement.clientHeight-50)+"px";
  50.  
  51. }
  52.  
  53. function mouseMoveHandler(e) {
  54. if(!e){
  55. e=window.event;
  56. }
  57. if(!drag0.moveBool) return;
  58.  
  59. if(HitTest.to(div0,div1)){
  60. randomPosition(div1);
  61. div1.style.backgroundColor=randomColor();
  62.  
  63. }
  64. }
  65. </script>
  66.  
  67. </body>
  68. </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的JavaScript相关文章