[Js]碰撞运动
描述:撞到目标点弹回来(速度反转)
一、无重力的漂浮div
var div1=document.getElementById("div1");
var iSpeedX=6;
var iSpeedY=8;
setInterval(function(){
var l=div1.offsetLeft+iSpeedX;
var t=div1.offsetTop+iSpeedY;
if(t>=document.documentElement.clientHeight-div1.offsetHeight){
iSpeedY*=-1; //速度反向
t=document.documentElement.clientHeight-div1.offsetHeight; //超出下边界时,把它拉回到下边界,不然右边滚动条会闪动出现一下
}
else if(t<=0){
iSpeedY*=-1;
t=0;
}
if(l>=document.documentElement.clientWidth-div1.offsetWidth){
iSpeedX*=-1;
l=document.documentElement.clientWidth-div1.offsetWidthl;
}
else if(l<=0){
iSpeedX*=-1;
l=0;
}
div1.style.left=l+'px';
div1.style.top=t+'px';
},30);
二、碰撞+重力
所谓重力就是Y轴的速度不断增加
setInterval(function(){
iSpeedY+=3;
var l=div1.offsetLeft+iSpeedX;
var t=div1.offsetTop+iSpeedY;
if(t>=document.documentElement.clientHeight-div1.offsetHeight){
iSpeedY*=-0.8;
iSpeedX*=0.8; //横向速度也要变慢,否则碰到地面时会停不下来
t=document.documentElement.clientHeight-div1.offsetHeight;
}
else if(t<=0){
iSpeedY*=-1;
iSpeedX*=0.8;
t=0;
}
if(l>=document.documentElement.clientWidth-div1.offsetWidth){
iSpeedX*=-0.8;
l=document.documentElement.clientWidth-div1.offsetWidthl;
}
else if(l<=0){
iSpeedX*=-0.8;
l=0;
}
if(Math.abs(iSpeedX)<1){ //解决小数的问题,因为假如速度是小数,正的没问题,100(iSpeed=0.5)-->100.5-->100,负的100(iSpeed=-0.5)--->99.5-->99,可能会出现
x轴反向时滑行
iSpeedX=0;
}
if(Math.abs(iSpeedY)<1){
iSpeedY=0;
}
div1.style.left=l+'px';
div1.style.top=t+'px';
},30);
三、碰撞+重力+拖拽
window.onload=function ()
{
var oDiv=document.getElementById('div1');
var lastX=0;
var lastY=0;
oDiv.onmousedown=function (ev)
{
var oEvent=ev||event;
var disX=oEvent.clientX-oDiv.offsetLeft;
var disY=oEvent.clientY-oDiv.offsetTop;
document.onmousemove=function (ev)
{
var oEvent=ev||event;
var l=oEvent.clientX-disX;
var t=oEvent.clientY-disY;
oDiv.style.left=l+'px';
oDiv.style.top=t+'px';
iSpeedX=l-lastX; //拖拽结束时的速度
iSpeedY=t-lastY;
lastX=l; //更新上一个点的位置
lastY=t;
};
document.onmouseup=function ()
{
document.onmousemove=null;
document.onmouseup=null;
startMove(); //拖拽结束时执行
};
clearInterval(timer);
};
};
var timer=null;
var iSpeedX=0;
var iSpeedY=0;
function startMove()
{
clearInterval(timer);
timer=setInterval(function (){
var oDiv=document.getElementById('div1');
iSpeedY+=3;
var l=oDiv.offsetLeft+iSpeedX;
var t=oDiv.offsetTop+iSpeedY;
if(t>=document.documentElement.clientHeight-oDiv.offsetHeight)
{
iSpeedY*=-0.8;
iSpeedX*=0.8;
t=document.documentElement.clientHeight-oDiv.offsetHeight;
}
else if(t<=0)
{
iSpeedY*=-1;
iSpeedX*=0.8;
t=0;
}
if(l>=document.documentElement.clientWidth-oDiv.offsetWidth)
{
iSpeedX*=-0.8;
l=document.documentElement.clientWidth-oDiv.offsetWidth;
}
else if(l<=0)
{
iSpeedX*=-0.8;
l=0;
}
if(Math.abs(iSpeedX)<1)
{
iSpeedX=0;
}
if(Math.abs(iSpeedY)<1)
{
iSpeedY=0;
}
if(iSpeedX==0 && iSpeedY==0 && t==document.documentElement.clientHeight-oDiv.offsetHeight)
{
clearInterval(timer);
}
else
{
oDiv.style.left=l+'px';
oDiv.style.top=t+'px';
}
document.title=iSpeedX;
}, 30);
}
[Js]碰撞运动的更多相关文章
- JS运动基础(四) 碰撞运动
碰撞运动撞到目标点,速度反转无重力的漂浮Div速度反转滚动条闪烁的问题过界后直接拉回来 加入重力反转速度的同时,减小速度纵向碰撞,横向速度也减小横向速度小数问题(负数) <!DOCTYPE HT ...
- javascript运动系列第九篇——碰撞运动
× 目录 [1]碰撞检测 [2]无损碰撞 [3]有损碰撞 前面的话 碰撞可以分为碰壁和互碰两种形式,上篇介绍了碰壁运动,本文将从浅入深地介绍碰撞运动的互碰形式 碰撞检测 对于互碰形式的碰撞运动来说,首 ...
- HTML5 Canvas彩色小球碰撞运动特效
脚本简介 HTML5 Canvas彩色小球碰撞运动特效是一款基于canvas加面向对象制作的运动小球动画特效. 效果展示 http://hovertree.com/texiao/html5/39/ ...
- JS缓冲运动案例:右侧居中悬浮窗
JS缓冲运动案例:右侧居中悬浮窗 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta cha ...
- JS缓冲运动案例:右下角悬浮窗
JS缓冲运动案例:右下角悬浮窗 红色区块模拟页面的右下角浮窗,在页面进行滚动时,浮窗做缓冲运动,最终在页面右下角停留. <!DOCTYPE html> <html lang=&quo ...
- js 碰撞 + 重力 运动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS完美运动框架
这套框架实现了多物体,任意值,链式运动,多值运动,基本满足常见的需求. /* 功能:完美运动框架,可以实现多物体,任意值,链式运动,多值运动 版本:V1.0 兼容性:Chrome,FF,IE8+ (o ...
- js缓冲运动
缓冲运动 现象:逐渐变慢,最后停止 原理:距离越远,速度越大 速度的计算方式: 1,速度由距离决定 2,速度=(目标值-当前值)/缩放系数 说明:速度为正负数时,也决定了物体移动的方向 示例:div缓 ...
- 原生js小球运动
//html代码 <input type="button" value="小球运动" /> <div></div> //js ...
随机推荐
- spring常用的工具类
spring给我们提供了很多的工具类, 应该在我们的日常工作中很好的利用起来. 它可以大大的减轻我们的平时编写代码的长度. 因我们只想用spring的工具类, 而不想把一个大大的spring工程给引入 ...
- iOS进阶收藏
XCode自带重构方式http://www.cocoachina.com/ios/20160127/15097.html iOS迭代多语言开发http://www.cocoachina.com/ios ...
- js问的我醉的不要不要的。
function a(b){ console.log(b); function b(){ console.log(b); } b();} a(1); 两个console.log会输出什么?竟然一个1都 ...
- mysql: 1045 access denied for user 'root'@'localhost' using password yes
原因是:root的密码错误了. 解决思路:关闭mysql服务,重新启动mysql服务,启动mysql的时候,指定不需要校验密码.然后登陆mysql,修改密码,退出.再重新启动mysql服务. 1.关闭 ...
- mvn编写主代码与测试代码
maven编写主代码与测试代码 3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目 ...
- Java源码初学_HashMap
一.概念 HashMap的实例有两个参数影响其性能:初始容量和加载因子.容量是哈希表中桶的数量,初始容量只是哈希表在创建时的容量.加载因子 是哈希表在其容量自动增加之前可以达到多满的一种尺度.当哈希表 ...
- Java编程思想学习笔记_6(并发)
一.从任务中产生返回值,Callable接口的使用 Callable是一种具有泛型类型参数的泛型,它的类型参数表示的是从方法call返回的值,而且必须使Executor.submit来去调用它.sub ...
- 如何设置DIV水平、垂直居中
一.水平居中 需要设置两点: 1 设置DIV 的width属性即宽度. 2 设置div的margin-left和margin-right属性即可 代码: <div style="w ...
- python 集合
面向对象的集合: #coding:utf-8 __author__ = 'similarface' class Set: ''' list实现集合,及其集合操作 ''' def __init__(se ...
- centos7 修改selinux 开机导致 faild to load SELinux policy freezing 错误
centos7 修改selinux 开机导致 faild to load SELinux policy freezing 错误 之前把selinux关闭了,这次想打开selinux,于是修改了 /e ...