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 ...
随机推荐
- WPF实现斜纹圆角进度条样式
原文:WPF实现斜纹圆角进度条样式 运行效果: 进度条样式: <!--进度条样式--> <LinearGradientBrush x:Key="ProgressBar.Pr ...
- Catalan&Stirling数
Catalan&Stirling数 Tags:数学 作业部落 评论地址 Catalan数 \(1,1,2,5,14,42,132,429,1430,4862,16796,58786...\) ...
- 前端- css - 总结
1.css层叠样式表 1.什么是CSS? CSS是指层叠样式表(Cascading Style Sheets),样式定义如何显示HTML元素,样式通常又会存在于样式表中. 也就是说把HTML元素的样式 ...
- 【CQOI2017】小Q的棋盘
题面 题解 根据题意,不回头是最好的(显然法) \(dfs\)找到最长链,设长度为\(\mathrm{L}\),然后分类讨论: 如果\(\mathrm{L} > m\),答案就是\(m + 1\ ...
- Asp.net中使用缓存(cache)
做了一个时间优化的项目,目的就是缩短程序过程中的时间花费,最后发现了asp.net和asp.net core 中都有缓存工具来进行缓存,以加快访问速度. 找了官方demo来进行分析: ObjectCa ...
- PCA人脸识别的python实现
这几天看了看PCA及其人脸识别的流程,并在网络上搜相应的python代码,有,但代码质量不好,于是自己就重新写了下,对于att_faces数据集的识别率能达到92.5%~98.0%(40种类型,每种随 ...
- jQuery中attr()函数 VS prop()函数
http://www.365mini.com/page/jquery-attr-vs-prop.htm 在jQuery中,attr()函数和prop()函数都用于设置或获取指定的属性,它们的参数和用法 ...
- Apache和Nginx比较
Apache和Nginx对比 功能对比 Nginx和Apache一样,都是HTTP服务器软件,在功能实现上都采用模块化结构设计,都支持通用的语言接口,如PHP.Perl.Python等,同时还支持正向 ...
- 第一个Django demo
平台:Pycharm Django 使用 Pycharm 进行开发,需要提前在 Pycharm 中(File > Settings > Project: Python > Proje ...
- CentOS7.2下配置SOCKS5代理
SOCKS介绍: 1.这里只做简单介绍. SOCKS5 是一个代理协议,它在使用TCP/IP协议通讯的前端机器和服务器机器之间扮演一个中介角色,使得内部网中的前端机器变得能够访问Int ...