jQuery類比物體自由落體運動(附示範與demo源碼下載),jquery源碼下載

來源:互聯網
上載者:User

jQuery類比物體自由落體運動(附示範與demo源碼下載),jquery源碼下載

本文執行個體講述了jQuery類比物體自由落體運動的方法。分享給大家供大家參考,具體如下:

運行效果如下:

點擊此處查看線上示範效果。

完整執行個體代碼點擊此處本站下載。

具體代碼如下:

<!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> <title>自由落體</title> <script src="jquery-1.7.1.min.js" type="text/javascript"></script> <script type="text/javascript" >  $(document).ready(function () {  });  var all_Height = 500;  var a = 9.8; // 加速度  var v = 0;  var prev_Time = 0;  var prev_Speed = 0;  var prev_Height = 0;  var speed = 0;  // 擷取當前的高度  var getHeight = function (obj) {   return obj.offset().top;  }  // 擷取當前的速度  var getSpeed = function (time, a) {   return time * a;  }  // 向下走時獲得當前物體所走的路徑  var getCurrentHeight = function (time) {   return 1 / 2 * a * time * time;  }  // 向上走時獲得當前物體所走的路徑  var getCurrentHeight2 = function (speed, time) {   return speed * time - 1 / 2 * a * time * time;  }  // 向下跑  function down() {   prev_Time = 0;   var interval = setInterval(function () {    if (getHeight($(".obj")) < all_Height) {     prev_Time = prev_Time + 0.1;     var height = getCurrentHeight(prev_Time) + prev_Height;     $(".obj").css("top", height + "px");     $(".info").append("<div>" + height + "</div>");    } else {     speed = getSpeed(a, prev_Time);     prev_Speed = speed;     clearInterval(interval);     up();    }   }, 5);  }  // 向上跑  function up() {   prev_Time = 0;   prev_Speed -= 4; // 動能損耗   var interval = setInterval(function () {    if (speed > 0) {     speed = prev_Speed - getSpeed(a, prev_Time);     prev_Time = prev_Time + 0.1;     var height = all_Height - getCurrentHeight2(prev_Speed, prev_Time);     $(".obj").css("top", height + "px");    } else {     clearInterval(interval);     prev_Height = $(".obj").offset().top;     if (prev_Height < all_Height) {      down();     }    }   }, 5);  }  function play() {   alert('play');   down();   document.getElementById("obj").onclick = null;  } </script> <style type="text/css"> body{ margin:0 0 0 0 ; background:#FFFFFF;} .title { text-align:center; color:#666666; border-bottom:2px solid gray; font-size:30px; line-height:100px; font-weight:bolder;} .obj { background-image:url('a.jpg'); width:50px; height:50px; position:absolute; top:0; left:0; } </style></head><body><div style="height:550px; border-bottom:2px solid gray;"><div class="title" >自由落體大類比<span style="font-size:12px;">(點擊圖片讓它落下)</span></div></div><div class="obj" id="obj" onclick="play();" ></div></body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.