jQ 小球碰撞检测
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>碰撞检测</title>
<script src="../js/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.wp {
width: 600px;
height: 500px;
border: 1px solid;
position: relative;
}
.box {
width: 30px;
height: 30px;
background: red;
position: absolute;
border-radius: 50%;
}
.box2 {
width: 100px;
height: 100px;
background: orange;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="wp">
<div class="box"></div>
<div class="box2"></div>
</div>
</body>
<script type="text/javascript">
var wp = $(".wp");
var box = $(".box");
var box2 = $(".box2")
var bl = box.offset().left;
var bt = box.offset().top;
var bv = 1;
var bv2 = 1;
//随机数
function randomNum(min,max){
var choices = max - min + 1;
var num = Math.floor(Math.random() * choices + min );
return num;
}
//随机颜色
function colorFun(num){
if(num==1){
wp.css('border',"1px solid rgb("+randomNum(0,255)+","+randomNum(0,255)+","+randomNum(0,255)+")");
}else{
box2.css('background',"rgb("+randomNum(0,255)+","+randomNum(0,255)+","+randomNum(0,255)+")");
}
box.css('background',"rgb("+randomNum(0,255)+","+randomNum(0,255)+","+randomNum(0,255)+")");
}
setInterval(function() {
bl += bv;
bt += bv2;
if(bl == (wp.width() - box.width()) || bl <= 0) {
bv *= -1;
colorFun(1);
}
if(bt >= (wp.height() - box.height()) || bt <= 0) {
bv2 *= -1;
colorFun(1);
}
// 左侧
if(bl == (box2.offset().left - box.width()) && bt >= (box2.offset().top-box.height()) && bt <= (box2.offset().top + box2.height())) {
bv *= -1;
colorFun();
}
// 上侧
if(bt == (box2.offset().top - box.height()) && bl >= (box2.offset().left- box.width()) && bl <= (box2.offset().left + box2.width())) {
bv2 *= -1;
colorFun();
}
// 右侧
if(bl == (box2.offset().left + box2.width()) && bt >= (box2.offset().top-box.height()) && bt <= (box2.offset().top + box2.height())) {
bv *= -1;
colorFun();
}
// 下侧
if(bt == (box2.offset().top + box2.height()) && bl >= (box2.offset().left- box.width()) && bl <= box2.offset().left + box2.width()) {
bv2 *= -1;
colorFun();
}
box.css('left',bl + "px");
box.css("top" , bt + "px");
}, )
</script>
</html>
jQ 小球碰撞检测的更多相关文章
- js实现小球的弹性碰撞。
前 言 MYBG 小编最近在做自己的个人网站,其中就用到了一个小球碰撞检测的功能,想自己写,无奈本人能力不足啊(毕竟还是一个菜鸟)!!就想着找个插件用一下也好,可是找了好久也没有找到一个比较好用 ...
- “AS3.0高级动画编程”学习:第一章高级碰撞检测
AdvancED ActionScript 3.0 Animation 是Keith Peters大师继"Make Things Move"之后的又一力作,网上已经有中文翻译版本了 ...
- Scrum立会报告+燃尽图(Final阶段第七次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2486 项目地址:https://coding.net/u/wuyy694 ...
- Scrum立会报告+燃尽图(Final阶段第二次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...
- Scrum立会报告+燃尽图(Final阶段第三次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...
- Scrum立会报告+燃尽图(Final阶段第四次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...
- Scrum立会报告+燃尽图(Final阶段第五次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2484 项目地址:https://coding.net/u/wuyy694 ...
- Scrum立会报告+燃尽图(Final阶段第六次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2485 项目地址:https://coding.net/u/wuyy694 ...
- Scrum立会报告+燃尽图(Final阶段第一次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2480 项目地址:https://coding.net/u/wuyy694 ...
随机推荐
- 06 django的用户认证组件
1.用户认证组件 用户认证组件: 功能:用session记录登录验证状态 前提:用户表:django自带的auth_user 创建超级用户: python3 manage.py createsuper ...
- Kubernetes学习之路(十二)之Pod控制器--ReplicaSet、Deployment
一.Pod控制器及其功用 Pod控制器是用于实现管理pod的中间层,确保pod资源符合预期的状态,pod的资源出现故障时,会尝试 进行重启,当根据重启策略无效,则会重新新建pod的资源. pod控制器 ...
- 电子设备 Kindle如何删除书籍或漫画
需要在亚马逊的官方设置好国家: 再在这里就可以管理了 要在kindle的上方小齿轮那里设置(飞行模式右边): 同步kindle 这样就能管理了
- 【Java源码解析】Thread
简介 线程本质上也是进程.线程机制提供了在同一程序内共享内存地址空间运行的一组线程.对于内核来讲,它就是进程,只是该进程和其他一下进程共享某些资源,比如地址空间.在Java语言里,Thread类封装了 ...
- Nopcommerce主要用到的技术及特点
很多人都说通过阅读.学习大神们高质量的代码是提高自己技术能力最快的方式之一.我觉得通过阅读NopCommerce的源码,可以从中学习很多企业系统.软件开发的规范和一些新的技术.技巧,可以快速地提高我们 ...
- Tomcat学习(二)------Tomcat原理详解及请求过程
Tomcat: Tomcat是一个JSP/Servlet容器.其作为Servlet容器,有三种工作模式:独立的Servlet容器.进程内的Servlet容器和进程外的Servlet容器. Tomcat ...
- 利用shell连接服务器
#应用 连接timesten 数据库 host = Linux(ip, 'user', 'pwd') # 传入Ip,用户名,密码host.connect() #主机开启cdsql = host.sen ...
- centos7安装vim以及在vim中显示中文
1.centos7安装vim yum -y install vim(简单粗暴安装方法) 2.在vim中显示中文不出现乱码 (1).vim ~/.vimrc (~/.vimrc为vim配置文件) (2) ...
- mac指令备忘
在这里简单记录下最近使用的快捷键,备忘,随时更新. 简单指令记录 mkdir 创建路径 pwd 输出当前路径 ls 查看目录 cd touch 创建文件 tree 输出目录树 mv 源文件 目标文件或 ...
- 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...