js实现div色块碰撞


Posted in Javascript onJanuary 16, 2020

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

描述:

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

效果:

js实现div色块碰撞

js实现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 相关文章推荐
如何使Chrome控制台支持多行js模式——意外发现
Jun 13 Javascript
js特殊字符过滤的示例代码
Mar 05 Javascript
jQuery学习总结之jQuery事件
Jun 30 Javascript
正则表达式优化JSON字符串的技巧
Dec 24 Javascript
Angularjs 创建可复用组件实例代码
Oct 09 Javascript
微信小程序 出现错误:{&quot;baseresponse&quot;:{&quot;errcode&quot;:-80002,&quot;errmsg&quot;:&quot;&quot;}}解决办法
Feb 23 Javascript
.net MVC+Bootstrap下使用localResizeIMG上传图片
Apr 21 Javascript
详解Angular Reactive Form 表单验证
Jul 06 Javascript
Koa2微信公众号开发之消息管理
May 16 Javascript
浅谈webpack-dev-server的配置和使用
May 17 Javascript
vue自定义指令实现方法详解
Feb 11 Javascript
js布局实现单选按钮控件
Jan 17 Javascript
Vue实现 点击显示再点击隐藏效果(点击页面空白区域也隐藏效果)
Jan 16 #Javascript
vue列表数据发生变化指令没有更新问题及解决方法
Jan 16 #Javascript
使用Karma做vue组件单元测试的实现
Jan 16 #Javascript
js实现div色块拖动录制
Jan 16 #Javascript
微信小程序实现二维码签到考勤系统
Jan 16 #Javascript
解决vue+ element ui 表单验证有值但验证失败问题
Jan 16 #Javascript
JavaScript实现简单的计算器
Jan 16 #Javascript
You might like
PHP中require和include路径问题详解
2014/12/25 PHP
php版微信开发Token验证失败或请求URL超时问题的解决方法
2016/09/23 PHP
PHP数组生成XML格式数据的封装类实例
2016/11/10 PHP
php 生成加密公钥加密私钥实例详解
2017/06/16 PHP
PHP实现基于面向对象的mysqli扩展库增删改查操作工具类
2017/07/18 PHP
PHP+Redis 消息队列 实现高并发下注册人数统计的实例
2018/01/29 PHP
Prototype使用指南之form.js
2007/01/10 Javascript
我也种棵OO树JXTree[js+css+xml]
2007/04/02 Javascript
location对象的属性和方法应用(解析URL)
2013/04/12 Javascript
表单元素与非表单元素刷新区别详细解析
2013/11/06 Javascript
Javascript实现通过选择周数显示开始日和结束日的实现代码
2016/05/30 Javascript
仿百度换肤功能的简单实例代码
2016/07/11 Javascript
常用原生js自定义函数总结
2016/11/20 Javascript
vuejs使用FormData实现ajax上传图片文件
2017/08/08 Javascript
微信小程序实现顶部普通选项卡效果(非swiper)
2020/06/19 Javascript
vue2.0设置proxyTable使用axios进行跨域请求的方法
2017/10/19 Javascript
玩转vue的slot内容分发
2018/09/22 Javascript
js数组中去除重复值的几种方法
2020/08/03 Javascript
[01:34]2014DOTA2展望TI 剑指西雅图VG战队专访
2014/06/30 DOTA
Python与Java间Socket通信实例代码
2017/03/06 Python
Python如何快速实现分布式任务
2017/07/06 Python
用python写扫雷游戏实例代码分享
2018/05/27 Python
python一键去抖音视频水印工具
2018/09/14 Python
python读取txt文件并取其某一列数据的示例
2019/02/19 Python
如何基于Python pygame实现动画跑马灯
2020/11/18 Python
意大利珠宝店:Luxury Zone
2019/01/05 全球购物
戴尔新加坡官网:Dell Singapore
2020/12/13 全球购物
美国电子产品购物网站:BuyDig.com
2020/06/17 全球购物
师范教师大学生职业生涯规划范文
2014/01/05 职场文书
关于爱情的广播稿
2014/01/16 职场文书
数控技术应用个人求职信范文
2014/02/03 职场文书
霸王洗发水广告词
2014/03/14 职场文书
入职担保书怎么写
2014/05/12 职场文书
餐饮食品安全责任书
2015/01/29 职场文书
北京颐和园导游词
2015/01/30 职场文书
2016年会开场白台词
2015/06/01 职场文书