懂得放弃,才会有新的收获!

Javascript摸拟自由落体与上抛运动

By weisnet On 2010-05-01 At 11:30 View:862

XML/HTML
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head runat="server">
  4.     <title>模拟自由落体与上抛运动</title>
  5.     <script type="text/javascript">
  6.         var $ = function(el) { return document.getElementById(el); };
  7.         function LuoRun()
  8.         {
  9.             this.h = 0;
  10.             this.s = 0;
  11.             this.g = 9.8;
  12.             this.isup = false;
  13.             this.rh = 0;
  14.             this.t = 0;
  15.             this.timer = null;
  16.             this.mt = 0;
  17.             this.top = 0;
  18.             this.left = 0;    
  19.             this.id = null;    
  20.         }
  21.         
  22.         LuoRun.prototype.Po = function(obj) {
  23.             this.left += 0.3;
  24.             obj.style.left = (this.left)+'px';
  25.             
  26.             if (!this.isup) {
  27.                 if (this.t ="=" 0)
  28.                 {
  29.                     this.top = obj.offsetTop;
  30.                     this.h = 570 - 22 - this.top;
  31.                     this.mt = Math.sqrt(2*this.h/(this.g*100));
  32.                     //alert(mt+' '+isup+' '+t)
  33.                 }
  34.                 
  35.                 this.t+="0.01;"
  36.                 
  37.                 if (this.t >= this.mt)
  38.                 {
  39.                     this.t = this.mt;
  40.                     this.rh = (1/2)*this.g*this.t*this.t*100;
  41.                     this.s = this.g*this.t*100;
  42.                     obj.style.top = (this.rh+this.top)+'px';
  43.                     //t = 0;
  44.                     this.s = this.s-50>0 ? this.s-50 : 0;
  45.                     this.isup = true;
  46.                     this.t = 0;
  47.                 }
  48.                 else
  49.                 {
  50.                     this.rh = (1/2)*this.g*this.t*this.t*100;
  51.                     this.s = this.g*this.t*100;
  52.                     
  53.                     obj.style.top = (this.rh+this.top)+'px';
  54.                 }
  55.             } else { //up
  56.                 //return;
  57.                 
  58.                 if (this.s ="=" 0) {
  59.                     clearInterval(this.timer);
  60.                     this.id.parentNode.removeChild(this.id);
  61.                     return;
  62.                 }
  63.                 
  64.                 if (this.t ="=" 0) {
  65.                     this.h = this.s*this.s/(2*this.g*100);
  66.                     this.mt = this.s/(this.g*100);
  67.                     this.top = obj.offsetTop;
  68.                     //alert(mt+' '+isup+' '+t)
  69.                 }
  70.                 
  71.                 this.t+="0.01;"
  72.                 if (this.t>="this.mt)" {
  73.                     this.t = this.mt;
  74.                     
  75.                     this.rh = this.s*this.t - (1/2)*this.g*this.t*this.t*100;
  76.                     obj.style.top = (this.top - this.rh)+'px';
  77.                     this.s = 0;
  78.                     this.isup = false;
  79.                     this.t = 0;
  80.                 }else {
  81.                     this.rh = this.s*this.t - (1/2)*this.g*this.t*this.t*100;
  82.                     
  83.                     obj.style.top = (this.top - this.rh)+'px';
  84.                 }
  85.             }
  86.         }
  87.         
  88.         LuoRun.prototype.Go = function(obj) {
  89.             var self = this;
  90.             if (obj ="=" null)
  91.                 obj = $('box');
  92.             self.timer = setInterval(function() {
  93.                 
  94.                 self.Po(obj);
  95.                 
  96.                 if (self.h<="0)" {
  97.                     clearInterval(self.timer);
  98.                     self.id.parentNode.removeChild(self.id);
  99.                 }
  100.             },10);
  101.         }
  102.         
  103.         document.onmousedown = function(e) {
  104.             e = e?e:window.event;
  105.             
  106.             var crtDiv = document.createElement('div');
  107.             crtDiv.style.position = 'absolute';
  108.             crtDiv.style.left = e.clientX + 'px';
  109.             crtDiv.style.top = e.clientY + 'px';
  110.             crtDiv.style.background = '#333';
  111.             crtDiv.style.width = '22px';
  112.             crtDiv.style.height = '22px';
  113.             
  114.             document.body.appendChild(crtDiv);
  115.             
  116.             crtDiv.innerHTML = '&nbsp;';
  117.             var C = new LuoRun();
  118.             C.left = e.clientX;
  119.             C.id = crtDiv;
  120.             document.onmouseup = function() {
  121.                 document.onmousemove = null;
  122.                 window.setTimeout(function() { C.Go(crtDiv); },1000);
  123.             }
  124.         }
  125.     </script>
  126.     <style type="text/css">
  127.         td,body {font-size:12px;}
  128.         .css1 {width:240px;display:table;position:absolute;left:20px;top:20px;border:1px solid green;background:#CAF4BD;line-height:18px;padding:3px;}
  129.         .css2 {width:900px;height:22px;border-top:1px solid #333;position:absolute;top:570px;left:60px;}
  130.     </style>
  131. </head>
  132. <body>
  133.     <form id="form1">
  134.     
  135.     <div class="css1">
  136.        名称:Javascript摸拟自由落体与上抛运动!<br />
  137.        作者:Gloot<br />
  138.        邮箱:glootz@gmail.com <br />
  139.          QQ:345268267 <br />
  140.        网站:www.weiseditor.com <br />
  141.        操作:在页面不同地方点几下
  142.     </div>
  143.     
  144.     <div id="line" class="css2">&nbsp;</div>
  145.     </form>
  146. </body>
  147. </html>

 

·上一篇:简单易懂的C#委托与事件
·下一篇:简单易懂的MS sql 2005 表达式分区

for this post

 
gloot Says:

查看实例
http://www.weiseditor.com/javascript/zylt.htm

 

Leave a Reply

首页 [1] 尾页   总共 1条记录.
请先 登陆  还没帐号

Hot Posts Powered by weiseditor

  • Add to Google

Friend links