<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>弹性势能动画(弹力球效果)</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
} #box {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
font-size: 30px;
text-align: center;
line-height: 100px;
background-color: greenyellow;
border-radius: 50%;
cursor: move;
user-select: none;
}
</style>
</head>
<body>
<div id="box">弹</div> <script type="text/javascript">
var oDiv = document.getElementById('box');
oDiv.addEventListener('mousedown', down, false);
function down(e) {
this.startX = e.clientX;
this.startY = e.clientY;
this.startL = this.offsetLeft;
this.startT = this.offsetTop; var _this = this;
this.MOVE = function (e) {
move.call(_this, e);
};
this.UP = function (e) {
up.call(_this);
};
document.addEventListener('mousemove', this.MOVE, false);
document.addEventListener('mouseup', this.UP, false); window.clearInterval(this.flyTimer);
window.clearInterval(this.dropTimer);
}
function move(e) {
var curL = e.clientX - this.startX + this.startL,
curT = e.clientY - this.startY + this.startT; var minL = 0, minT = 0, maxL = (document.documentElement.clientWidth || document.body.clientWidth) - this.offsetWidth, maxT = (document.documentElement.clientHeight || document.body.clientHeight) - this.offsetHeight; curL = curL < minL ? minL : (curL > maxL ? maxL : curL);
curT = curT < minT ? minT : (curT > maxT ? maxT : curT);
this.style.left = curL + 'px';
this.style.top = curT + 'px'; if (!this.pre) {
this.pre = this.offsetLeft;
} else {
this.speedFly = this.offsetLeft - this.pre;
this.pre = this.offsetLeft;
}
}
function up() {
document.removeEventListener('mousemove', this.MOVE, false);
document.removeEventListener('mouseup', this.UP, false); fly.call(this); drop.call(this);
} function fly() {
var _this = this;
_this.flyTimer = setInterval(function () {
if (Math.abs(_this.speedFly) < 0.5) {
clearInterval(_this.flyTimer);
return;
}
_this.speedFly *= 0.98;
var curL = _this.offsetLeft + _this.speedFly;
var minL = 0, maxL = (document.documentElement.clientWidth || document.body.clientWidth) - _this.offsetWidth;
if (curL <= minL) {
_this.style.left = 0;
_this.speedFly *= -1;
} else if (curL >= maxL) {
_this.style.left = maxL + 'px';
_this.speedFly *= -1;
}
else {
_this.style.left = curL + 'px';
}
}, 10);
} function drop() {
var _this = this;
_this.dropFlag = 0;
_this.dropTimer = setInterval(function () {
if (_this.dropFlag > 1) {
clearInterval(_this.dropTimer);
return;
}
!_this.dropSpeed ? _this.dropSpeed = 10 : _this.dropSpeed += 10;
_this.dropSpeed *= 0.98;
var curT = _this.offsetTop + _this.dropSpeed;
var maxT = (document.documentElement.clientHeight || document.body.clientHeight) - _this.offsetHeight;
if (curT >= maxT) {
_this.style.top = maxT + 'px';
_this.dropSpeed *= -1;
_this.dropFlag++;
} else {
_this.style.top = curT + 'px';
_this.dropFlag = 0;
}
}, 10);
}
</script>
</body>
</html>

最新文章

  1. html在一个页面显示另一个页面的部分内容
  2. Android(java)学习笔记83:finally关键字的作用
  3. mysql建表且某字段内不允许出现重复值
  4. javascript将毫秒还原为可读时间格式
  5. jquery第三期:js与jquery对象转换
  6. I/O重定向与管道
  7. hibernate---一对一单向外键关联--XML
  8. 微服务从设计到部署(二)使用 API 网关
  9. 查看linux网卡硬件名称
  10. Python 面向对象基础知识
  11. 【总结】关于YUV-RGB格式转换的一些个人理解
  12. 基础篇:8.如何定义变量?js变量有什么特点?
  13. websocket 群聊单聊
  14. SpringBoot配置文件application.properties详解
  15. Android Studio添加so文件并打包到APK的lib文件夹中
  16. java读取网页图片路径并下载到本地
  17. js正则表达式汇集
  18. hbase 跳转过滤器skipfilter
  19. Kruskal-Wallis test
  20. 【2018.06.26NOIP模拟】T3节目parade 【支配树】*

热门文章

  1. ASP.NET中的几种弹出框提示
  2. Android插件化(二):使用DexClassLoader动态载入assets中的apk
  3. javascript中易犯的错误有哪些
  4. SQL事务+异常
  5. recyclerview23+出现多个item只显示第一个item的问题
  6. Spark scheduler
  7. nodejs学习(二)--express热更新nodemon,自启动项目
  8. 【DRF频率】
  9. logback 生成日志
  10. spinner -样式实现