js实现div色块碰撞

 更新时间:2020年01月16日 10:35:09   作者:SSSkyCong  
这篇文章主要为大家详细介绍了js实现div色块碰撞,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了js实现div色块碰撞的具体代码,供大家参考,具体内容如下

描述:

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

效果:

实现:

js文件:

function DragObj(_obj) {
 this.obj=_obj;
 this.point={};
 this.moveBool=false;
 this.obj.self=this;
 this.obj.addEventListener("mousedown",this.mouseHandler);
 this.obj.addEventListener("mouseup",this.mouseHandler);
 this.obj.addEventListener("mousemove",this.mouseHandler);
 this.obj.addEventListener("mouseleave",this.mouseHandler);
}
DragObj.prototype={
 mouseHandler:function (e) {
  if(!e){
   e=window.event;
  }
  if(e.type=="mousedown"){
   this.self.moveBool=true;
   this.self.point.x=e.offsetX;
   this.self.point.y=e.offsetY;
  }else if(e.type=="mousemove"){
   if(!this.self.moveBool) return;
   this.self.obj.style.left=(e.clientX-this.self.point.x)+"px"
   this.self.obj.style.top=(e.clientY-this.self.point.y)+"px"
  }else if(e.type=="mouseup" || e.type=="mouseleave"){
   this.self.moveBool=false;
  }
 },
 removeEvent:function () {
 
  this.obj.removeEventListener("mousedown",this.mouseHandler);
  this.obj.removeEventListener("mouseup",this.mouseHandler);
  this.obj.removeEventListener("mousemove",this.mouseHandler);
  this.obj.removeEventListener("mouseleave",this.mouseHandler);
 
  this.obj=null;
  this.point=null;
 }
};
var HitTest=HitTest || (function () {
 return {
  to:function (display0,display1) {
   var rect=display0.getBoundingClientRect();
   var rect1=display1.getBoundingClientRect();
   if(rect.left>=rect1.left && 
rect.left<=rect1.left+rect1.width
 && rect.top>=rect1.top && rect.top<=rect1.top+rect1.height){
    return true;
   }
   if(rect.left+rect.width>=rect1.left && rect.left+rect.width<=rect1.left+rect1.width && rect.top>=rect1.top
 && rect.top<=rect1.top+rect1.height){
    return true;
   }
   if(rect.left>=rect1.left && 
rect.left<=rect1.left+rect1.width 
&& rect.top+rect.height>=rect1.top && 
rect.top+rect.height<=rect1.top+rect1.height){
    return true;
   }
   if(rect.left+rect.width>=rect1.left && rect.left+rect.width<=rect1.left+rect1.width && 
rect.top+rect.height>=rect1.top 
&& rect.top+rect.height<=rect1.top+rect1.height){
    return true;
   }
  }
 }
})();
var LoadImg=LoadImg || (function () {
 return {
  load:function (listSrc,callBack) {
   this.callBack=callBack;
   this.listSrc=listSrc;
   this.num=0;
   this.imgArr=[];
   var img=new Image();
   img.addEventListener("load",this.loadHandler.bind(this));
   img.src=listSrc[0];
  },
  loadHandler:function (e) {
   if(!e){
    e=window.event;
   }
   e.currentTarget.removeEventListener
("load",this.loadHandler.bind(this));
   this.imgArr[this.num]=e.currentTarget;
   if(this.num==this.listSrc.length-1){
    this.callBack(this.imgArr)
    return;
   }
   var img=new Image();
   this.num++;
   img.addEventListener("load",this.loadHandler.bind(this));
   img.src=this.listSrc[this.num];
  }
 }
})();

html:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <style>
  div{
   width: 200px;
   height: 200px;
   position: absolute;
  }
 </style>
 <script src="js/dragObj.js"></script>
</head>
<body>
<div id="div0"></div>
<div id="div1"></div>
<script>
 window.addEventListener("mousedown",mousedownHandler);//生成一个
 function mousedownHandler(e) {
  if(!e){
   e=window.event;
  }
  e.preventDefault();
 }
 var div0=document.getElementById("div0");
 var div1=document.getElementById("div1");
 div0.style.backgroundColor=randomColor();
 div1.style.backgroundColor=randomColor();
 
 randomPosition(div0)
 randomPosition(div1)
 var drag0=new DragObj(div0);
 div0.addEventListener("mousemove",mouseMoveHandler)
 function randomColor() {
  var str="#";
  for(var i=0;i<3;i++){
   var color=Math.floor(Math.random()*256).toString(16);
   if(color.length<2){
    color="0"+color;
   }
   str+=color;
  }
  return str;
 }
 
 function randomPosition(div) {
  div.style.left=Math.random()*(document.documentElement.clientWidth-50)+"px";
  div.style.top=Math.random()*(document.documentElement.clientHeight-50)+"px";
 
 
 }
 
 function mouseMoveHandler(e) {
  if(!e){
   e=window.event;
  }
  if(!drag0.moveBool) return;
 
  if(HitTest.to(div0,div1)){
   randomPosition(div1);
   div1.style.backgroundColor=randomColor();
 
  }
 }
</script>
 
</body>
</html>

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

相关文章

  • JavaScript将数据转换成整数的方法

    JavaScript将数据转换成整数的方法

    这篇文章主要介绍了JavaScript将数据转换成整数的方法,有需要的朋友可以参考一下
    2014-01-01
  • 通用于ie和firefox的函数 GetCurrentStyle (obj, prop)

    通用于ie和firefox的函数 GetCurrentStyle (obj, prop)

    通用于ie和firefox的函数 GetCurrentStyle (obj, prop)...
    2006-12-12
  • 学会javascript之迭代器

    学会javascript之迭代器

    本文主要讲解javascript之迭代器,在 JavaScript 中,迭代器是一个对象,它定义一个序列,并在终止时可能返回一个返回值。需要详细了解相关知识的小伙伴可以参考一下这篇文章
    2021-09-09
  • javascript十个最常用的自定义函数(中文版)

    javascript十个最常用的自定义函数(中文版)

    如果不使用类库或者没有自己的类库,储备一些常用函数总是有好处的。
    2009-09-09
  • JavaScript Date对象功能与用法学习记录

    JavaScript Date对象功能与用法学习记录

    这篇文章主要介绍了JavaScript Date对象功能与用法,结合实例形式总结分析了JavaScript Date对象基本功能、用法及操作注意事项,需要的朋友可以参考下
    2020-04-04
  • RxJS在TypeScript中的简单使用详解

    RxJS在TypeScript中的简单使用详解

    这篇文章主要介绍了RxJS在TypeScript中的简单使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • 如何使用js获取扩展名详解

    如何使用js获取扩展名详解

    给你一个文件名,你能获得它的扩展名吗?这篇文章主要给大家介绍了关于如何使用js获取扩展名的相关资料,文中介绍了几种实现的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-02-02
  • 微信小程序实现课程选择器

    微信小程序实现课程选择器

    这篇文章主要为大家详细介绍了微信小程序实现课程选择器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • Javascript的并行运算实现代码

    Javascript的并行运算实现代码

    随着多核cpu的普级,并发/并行多线程运算在主流的编程语言越来越流行,而在目前Javascript实现中还看不到在语言方面支持多线程,现在Javascript如此流行,真希望今后会在语言的层面有很大的变化.
    2010-11-11
  • js浏览器html5表单验证

    js浏览器html5表单验证

    这篇文章主要为大家详细介绍了js浏览器html5表单验证,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10

最新评论