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 相关文章推荐
js事件冒泡实例分享(已测试)
Apr 23 Javascript
JS实现模拟风力的雪花飘落效果
May 13 Javascript
jQuery实现的仿百度分页足迹效果代码
Oct 30 Javascript
js实现四舍五入完全保留两位小数的方法
Aug 02 Javascript
Vue.js每天必学之指令系统与自定义指令
Sep 07 Javascript
jquery删除table当前行的实例代码
Oct 07 Javascript
Javascript中call,apply,bind方法的详解与总结
Dec 12 Javascript
用Vue写一个分页器的示例代码
Apr 22 Javascript
JS加密插件CryptoJS实现AES加密操作示例
Aug 16 Javascript
Cookbook组件形式:优化 Vue 组件的运行时性能
Nov 25 Javascript
使用JavaScript和MQTT开发物联网应用示例解析
Aug 07 Javascript
Vue-cli打包后部署到子目录下的路径问题说明
Sep 02 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
投票管理程序
2006/10/09 PHP
php 计算两个时间戳相隔的时间的函数(小时)
2009/12/18 PHP
PHP 字符串长度判断效率更高的方法
2014/03/02 PHP
myFocus slide3D v1.1.0 使用方法与下载
2011/01/12 Javascript
JS实现金额转换(将输入的阿拉伯数字)转换成中文的实现代码
2013/09/30 Javascript
js登录弹出层特效
2014/03/07 Javascript
选择复选框按钮置灰否则按钮可用
2014/05/22 Javascript
js类定义函数时用prototype与不用的区别示例介绍
2014/06/10 Javascript
js中this的用法实例分析
2015/01/10 Javascript
JS实现的手机端精简幻灯片效果
2016/09/05 Javascript
JS实现仿百度文库评分功能
2017/01/12 Javascript
Vue实现typeahead组件功能(非常靠谱)
2017/08/26 Javascript
nodejs 使用nodejs-websocket模块实现点对点实时通讯
2018/11/28 NodeJs
详解vue在项目中使用百度地图
2019/03/26 Javascript
微信小程序自定义toast组件的方法详解【含动画】
2019/05/11 Javascript
JS简单表单验证功能完整示例
2020/01/26 Javascript
javascript设计模式 ? 状态模式原理与用法实例分析
2020/04/22 Javascript
python+VTK环境搭建及第一个简单程序代码
2017/12/13 Python
Python学习笔记之变量、自定义函数用法示例
2019/05/28 Python
python获取当前文件路径以及父文件路径的方法
2019/07/10 Python
Django中使用MySQL5.5的教程
2019/12/18 Python
django queryset相加和筛选教程
2020/05/18 Python
python pandas dataframe 去重函数的具体使用
2020/07/20 Python
Django Auth用户认证组件实现代码
2020/10/13 Python
python中remove函数的踩坑记录
2021/01/04 Python
css3 transform及原生js实现鼠标拖动3D立方体旋转
2016/06/20 HTML / CSS
SheIn俄罗斯:时尚女装网上商店
2017/02/28 全球购物
Canal官网:巴西女性时尚品牌
2019/10/16 全球购物
社区学习十八大感想
2014/01/22 职场文书
优秀民警事迹材料
2014/01/29 职场文书
策划创业计划书
2014/02/06 职场文书
《台湾的蝴蝶谷》教学反思
2014/02/20 职场文书
服务行业演讲稿
2014/09/02 职场文书
2014年行政人事工作总结
2014/12/09 职场文书
新娘父亲婚礼致辞
2015/07/27 职场文书
彻底解决MySQL使用中文乱码的方法
2022/01/22 MySQL