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 相关文章推荐
baidu博客的编辑友情链接的新的层窗口!经典~支持【FF】
Feb 09 Javascript
优化 JavaScript 代码的方法小结
Jul 16 Javascript
根据IP的地址,区分不同的地区,查看不同的网站页面的js代码
Feb 26 Javascript
HTML页面登录时的JS验证方法
May 28 Javascript
简单实现轮播图效果的实例
Jul 15 Javascript
Html5 js实现手风琴效果
Apr 17 Javascript
JS正则获取HTML元素的方法
Mar 31 Javascript
jQuery:unbind方法的使用详解
Aug 14 jQuery
vue vuex vue-rouert后台项目——权限路由(适合初学)
Dec 29 Javascript
jquery判断滚动条距离顶部的距离方法
Sep 05 jQuery
vue2.0 实现富文本编辑器功能
May 26 Javascript
VUE Elemen-ui之穿梭框使用方法详解
Jan 19 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中检查PHP文件是否有语法错误的方法
2009/12/23 PHP
PHP缩略图等比例无损压缩,可填充空白区域补充色
2011/06/10 PHP
浅谈php数组array_change_key_case() 函数和array_chunk()函数
2016/10/22 PHP
javascript 隔行换色函数代码
2010/10/24 Javascript
jQuery1.4.2与老版本json格式兼容的解决方法
2011/02/12 Javascript
分享10篇优秀的jQuery幻灯片制作教程及应用案例
2011/04/16 Javascript
js 编码转换 gb2312 和 utf8 互转的2种方法
2013/08/07 Javascript
JavaScript模块随意拖动示例代码
2014/05/27 Javascript
js动态修改表格行colspan列跨度的方法
2015/03/30 Javascript
JavaScript中模拟实现jsonp
2015/06/19 Javascript
PageSwitch插件实现100种不同图片切换效果
2015/07/28 Javascript
js实现具有高亮显示效果的多级菜单代码
2015/09/01 Javascript
jQuery实现的无限级下拉菜单功能示例
2016/09/12 Javascript
JS中的数组转变成JSON格式字符串的方法
2017/05/09 Javascript
Node.js实现连接mysql数据库功能示例
2017/09/15 Javascript
小程序实现页面顶部选项卡效果
2018/11/06 Javascript
基于mpvue小程序使用echarts画折线图的方法示例
2019/04/24 Javascript
深入理解Vue keep-alive及实践总结
2019/08/21 Javascript
详解如何修改 node_modules 里的文件
2020/05/22 Javascript
[02:53]DOTA2亚洲邀请赛 NewBee战队巡礼
2015/02/03 DOTA
haskell实现多线程服务器实例代码
2013/11/26 Python
Python 模拟员工信息数据库操作的实例
2017/10/23 Python
python opencv 读取本地视频文件 修改ffmpeg的方法
2019/01/26 Python
给Python学习者的文件读写指南(含基础与进阶)
2020/01/29 Python
通过CSS3的object-fit来调整图片适配尺寸的技巧简介
2016/02/27 HTML / CSS
荷兰网上鞋店:Ziengs.nl
2017/01/02 全球购物
英国受欢迎的运动鞋和街头服装商店:Footasylum
2018/06/12 全球购物
英国领先的隐形眼镜在线供应商:Lenstore.co.uk
2019/11/24 全球购物
c/c++某大公司的两道笔试题
2014/02/02 面试题
库房保管员岗位职责
2014/04/07 职场文书
婚前协议书范本
2014/04/15 职场文书
财务整改报告范文
2014/11/05 职场文书
实习科室评语
2015/01/04 职场文书
2016年“12.3”国际残疾人日活动总结
2016/04/01 职场文书
Python 中random 库的详细使用
2021/06/03 Python
Python爬虫实战之爬取京东商品数据并实实现数据可视化
2021/06/07 Python