<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
img{ width:200px; height:200px; position:absolute;}
.box{ width: 5px; height: 5px; background: #000; position: absolute; display: none;}
</style> <script type="text/javascript">
window.onload = function(){ var oPic = document.getElementById("pic");
var iSpeedX = 0;  //定义在X方向上的速度
var iSpeedY = 0;  //定义在Y方向上的速度
var timer = null; oPic.style.width = 0;
oPic.style.height = 0;
var beginX = document.documentElement.clientWidth /2;  //获取可视区在X方向上的中心点
var beginY = document.documentElement.clientHeight /2;  //获取可视区在Y方向上的中心点
oPic.style.left = beginX + "px";  //初始化oPic的left值
oPic.style.top = beginY + "px";  //初始化oPic的Top值
console.log(beginX + " , " + beginY); toChange(200); function toChange(iTraget){
timer = setInterval(function (){
if(oPic.offsetWidth == iTraget){
clearInterval(timer);
startMove();
}else{//加载时显示oPic
oPic.style.width = oPic.offsetWidth + 10 + "px";
oPic.style.height = oPic.offsetHeight + 10 +"px";
oPic.style.left = beginX - oPic.offsetWidth /2 + "px";
oPic.style.top = beginY - oPic.offsetHeight /2 +"px";
} },30); }
oPic.onmousedown = function(ev){
var oEvent = ev || event;
var disX = oEvent.clientX - oPic.offsetLeft;  //获取鼠标按下时在X方向上的的坐标位置
var disY = oEvent.clientY - oPic.offsetTop;  //获取鼠标按下时在Y方向上的坐标位置
var preX = oEvent.clientX;  //计算速度的第一个点-X
var preY = oEvent.clientY;  //计算速度的第一个点-Y
clearInterval(timer);
document.onmousemove = function (ev){
var oEvent = ev || event;
iSpeedX = oEvent.clientX - preX;  //速度-X
iSpeedY = oEvent.clientY - preY;  //速度-Y
preX = oEvent.clientX;
preY = oEvent.clientY;
var l = oEvent.clientX - disX;  //鼠标拖动时的坐标-X
var t = oEvent.clientY - disY;  //鼠标拖动时的坐标-Y
       //拖动时通过DOM来创建DIV计算速度
var oBox = document.createElement("div");
oBox.className = "box";
oBox.style.left = oEvent.clientX + "px";
oBox.style.top = oEvent.clientY + "px";
document.body.appendChild(oBox); oPic.style.left = l + "px";  //拖动时oPic的left或top值
oPic.style.top = t + "px"; //document.title = l + "," +t + "," + iSpeedX + "," + iSpeedY;
}
document.onmouseup = function (){
document.onmousemove = null;
startMove();  //回弹
} return false; }
function startMove(){
clearInterval(timer);
timer = setInterval(function (ev){
iSpeedY +=3;
var L = oPic.offsetLeft + iSpeedX;
var T = oPic.offsetTop + iSpeedY;
if(L < 0){
L = 0;
iSpeedX *= -1;
iSpeedX *=0.75;
}else if(L > document.documentElement.clientWidth - oPic.offsetWidth){
L= document.documentElement.clientWidth - oPic.offsetWidth;
iSpeedX *= -1;
iSpeedX *=0.75;
}
if(T < 0){
T = 0;
iSpeedY *=-1;
iSpeedY *=0.75;
}else if(T > document.documentElement.clientHeight - oPic.offsetHeight){
T = document.documentElement.clientHeight - oPic.offsetHeight;
iSpeedY *=-1;
iSpeedY *=0.75;
iSpeedX *=0.75;
}
oPic.style.left = L + "px";
oPic.style.top = T + "px"; console.log(L + " , " + T + " , " + iSpeedX + " , " + iSpeedY);
},30)
}
}
</script> </head> <body> <img src="f.jpg" id="pic"> </body>
</html>

  • 有时候发现会有很多难点,有时候又发现没有难点。
  • 各种坐标的获取。
  • 根据坐标来计算各种值(比如:速度大小)
  • 边界值的判断
  • 速度*一个小数(0.nn)摩擦力

JS回弹原理-高级的更多相关文章

  1. JS垂直落体回弹原理

    /* *JS垂直落体回弹原理 */ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  2. JS简单回弹原理

    /* *JS简单回弹原理 */ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...

  3. Immutable.js 实现原理

    Immutable.js 实现原理 Immutable collections for JavaScript v4.0.0-rc.12 released on Oct 31, 2018 https:/ ...

  4. js 深入原理讲解系列-Promise

    js 深入原理讲解系列-Promise 能看懂这一题你就掌握了 js Promise 的核心原理 不要专业的术语,说人话,讲明白! Q: 输出下面 console.log 的正确的顺序? const ...

  5. js 深入原理讲解系列-事件循环

    js 深入原理讲解系列-事件循环 能看懂这一题你就掌握了 js 事件循环的核心原理 不要专业的术语,说人话,讲明白! Q: 输出下面 console.log 的正确的顺序? console.log(' ...

  6. js 深入原理讲解系列-currying function

    js 深入原理讲解系列-currying function 能看懂这一题你就掌握了 js 科里函数的核心原理 不要专业的术语,说人话,讲明白! Q: 实现 sum 函数使得以下表达式的值正确 cons ...

  7. js高级---js运行原理

    概述 浏览器组成可分两部分:Shell+内核.浏览器内核又可以分成两部分:渲染引擎(layout engineer或者Rendering Engine)和JS引擎.渲染引擎功能作用 渲染引擎,负责对网 ...

  8. 浅谈JS中的高级函数

    在JavaScript中,函数的功能十分强大.它们是第一类对象,也可以作为另一个对象的方法,还可以作为参数传入另一个函数,不仅如此,还能被一个函数返回!可以说,在JS中,函数无处不在,无所不能,堪比孙 ...

  9. 谈谈JS中的高级函数

    博客原文地址:Claiyre的个人博客如需转载,请在文章开头注明原文地址 在JavaScript中,函数的功能十分强大.它们是第一类对象,也可以作为另一个对象的方法,还可以作为参数传入另一个函数,不仅 ...

随机推荐

  1. 7、源与值(Source/Values)

    学习目录:树莓派学习之路-GPIO Zero 官网地址:https://gpiozero.readthedocs.io/en/stable/source_values.html 环境:UbuntuMe ...

  2. 解决ifarme在ios下无法使用

    在第一层的config 添加 <access origin="*" /><allow-navigation href="*" />< ...

  3. MySQL:ALTER COLUMN、MODIFY COLUMN 和 CHANGE COLUMN

    ALTER COLUMN.MODIFY COLUMN 和 CHANGE COLUMN 语句修改列: ALTER COLUMN:改变.删除列的默认值(备注:列的默认值存储在 .frm 文件中). 这个语 ...

  4. 四、Python文件操作

    描述:Python的文件操作涉及对文件的读/写与编码的处理. 1.两种写法 1.1 第一种 1 f = open('文件路径', '文件操作方式', encoding='utf-8') 2 对文件操作 ...

  5. canvas的beginPath和closePath

    https://www.cnblogs.com/xuehaoyue/p/6549682.html https://segmentfault.com/a/1190000010330319 https:/ ...

  6. 【STM32H7教程】第58章 STM32H7的硬件JPEG应用之图片解码显示

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第58章       STM32H7的硬件JPEG应用之图片解 ...

  7. springboot学习4使用日志:logback

    springboot学习4使用日志:logback 一.基本知识说明 SpringBoot默认使用logback作为日志框架 ,所以引入起步依赖后就可以直接使用logback,不需要其他依赖. Spr ...

  8. 《JavaScript高级程序设计》读书笔记(三)基本概念第三小节 String、Object类型

    内容---语法 上一小节---数据类型 本小节 String类型---流程控制语句---理解函数 String类型--零个或者多个16位Unicode字符组成字符序列,即字符串--可以由双引号&quo ...

  9. Django框架之模板语言特殊标记(将后端的数据显示到前端)

    后端代码 def GetMain(request): return render( request, "main.html", { "user1":" ...

  10. Spring Boot Shiro 使用教程

    Apache Shiro 已经大名鼎鼎,搞 Java 的没有不知道的,这类似于 .Net 中的身份验证 form 认证.跟 .net core 中的认证授权策略基本是一样的.当然都不知道也没有关系,因 ...