运动的球(1)
2015-11-28 16:59
281 查看
<!doctype html> <html> <head> <title>抛物运动</title> <style> html{height:100%;} body{font-family:Arial;height:100%;} .wrap{width:1200px;margin:0 auto;padding:60px 0;position:relative;height:100%;} .x{border-top:1px solid #000;position:absolute;left:0;top:400px;right:0;} .x:before{content:"X";font-size:40px;position:absolute;right:5px;} .y{border-left:1px solid #000;position:absolute;left:600px;top:0;bottom:0;} .y:before{content:"Y";font-size:40px;position:absolute;left:5px;} #target{width:100px;height:40px;border-radius:20px;cursor:move;background:#f0f3f9;border:1px solid #3453d8;position:absolute;top:100px;left:100px;line-height:40px;} .mid{display:block;text-align:center;} #roll{width:30px;height:30px;border-radius:50%;position:absolute;top:400px;left:600px;margin-top:-15px;margin-left:-15px;background:#f2c823;} </style> </head> <body> <div class="wrap"> <i class="x"></i> <i class="y"></i> <div id="target"><span class="mid">X:<span id="xt"></span> Y:<span id="yt"></span></span></div> <div id="roll"></div> </div> <script> target.addEventListener("mousedown",dragstart, false); var orignX, orignY, startX, startY, targetX, targetY; xt.innerHTML = target.offsetLeft - 600 + 50; yt.innerHTML = target.offsetTop - 400 + 20; function dragstart(e){ orignX = e.clientX; orignY = e.clientY; startX = this.offsetLeft; startY = this.offsetTop; this.addEventListener("mousemove",drag, false); this.addEventListener("mouseup",dragend, false); } function drag(e){ var x = e.clientX, y = e.clientY, deltaX = e.clientX - orignX, deltaY = e.clientY - orignY; this.style.left = startX + deltaX + 'px'; this.style.top = startY + deltaY + 'px'; xt.innerHTML = this.offsetLeft - 600 + 50; yt.innerHTML = this.offsetTop - 400 + 20; } function dragend(){ this.removeEventListener("mousemove", drag, false); this.removeEventListener("mouseup", dragend, false); targetX = target.offsetLeft - 600 + 50; targetY = target.offsetTop - 400 + 20; b = (targetY - a * targetX * targetX) / targetX; shot(); } var frame1 = 167, a = 0.003, flag = true, b; function shot(){ if(!flag) return; var moveX = 0, rate = targetX > 0 ? 1 : -1; var step = function(){ var t = 2 * a * moveX + b; moveX = moveX + rate * Math.sqrt(frame1 / (t * t + 1)); if(Math.abs(moveX) > Math.abs(targetX)){ moveX = targetX; } var x1 = moveX, y1 = a * x1 * x1 + b * x1; roll.style["transform"] = "translate(" +x1 + "px, "+ y1 +"px"+")"; if(x1 !== targetX){ window.requestAnimationFrame(step); }else{ var xx = 10; } }; window.requestAnimationFrame(step); } </script> </body> </html>
View Code
相关文章推荐
- 1-4-08:判断一个数能否同时被3和5整除
- sql clr项目注意
- VUE(现代库) VS jquery(传统库)
- nyoj 138 找球号(二)
- HTML中<form>表单method的get方式和post方式
- HDFS Rolling Upgrade
- 在Legacy BIOS与UEFI 两种模式安装Windows 8操作系统的方法
- 1-4-07:收集瓶盖赢大奖
- 关于浏览器兼容的细节
- HDFS NFS Gateway
- cocoa和cocoa Touch的区别
- Spark中加载本地(或者hdfs)文件以及SparkContext实例的textFile使用
- myeclipse 没有提示时的设置
- PHP如何检测一个UTF-8字符串里有没有繁体字
- android launcher3修改内置的系统图标显示
- qt.pro相关注意点
- 更新iOS 9之后,你需要留意的两个游戏新特性
- 算法兴趣-----求两个已排序数组的交集和并集(算法复杂度O(N+M))
- 【Leetcode】Subsets II
- 《你一定爱读的极简欧洲史》读书笔记