成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? jQuery實現的分子運動小球碰撞效果
全站頭部文字 我要出現在這里
jQuery實現的分子運動小球碰撞效果 715 2024-03-12   

本文實例講述了jQuery實現的分子運動小球碰撞效果。分享給大家供大家參考,具體如下:

先看效果圖吧,因為小球是運動狀態的,不好截圖,這里就先大體畫了一下路線,表示大體意思吧,如果想看真實的效果,自己粘貼下去運行:

查看圖片

小球有點小喲,嘿嘿,沒有對細節進行詳細的處理,如果像讓它完美一點,自己處理下吧!這里是模擬的理想狀態下的,沒有摩擦力與組里的分子碰撞運動,高科技喲~~~~~~mu~a
代碼也沒有整理,如果有心的話,把它整理成面向對象形式的吧!

代碼如下:

<!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="http://www.gimoo.net/t/1903/jquery-1.7.1.min.js" type="text/javascript"></script>
  <script type="text/javascript" >
    var dim_half_past_PI = dimAngle(Math.PI / 2); // Math.PI/2的約數
    var lastAngle = dimAngle(Math.PI/8); // 發射角度(0-dimAngle(Math.PI))
    var v = 120; //飛行速度【>0】
    var lastPosition = {}; // 最后一次碰撞坐標
    var lastTime = ""; // 最后一次碰撞時間
    var lastDirection = "top"; // 開始發射方向(top,bottom,left,right)
    var horizen = 1; // 水品方向的積數
    var vertical = 1; // 豎直方向的積數
    var box = {};
    function dimAngle(angle) {
      var tempAngle = angle + "";
      return parseFloat(tempAngle.substring(0, 6));
    }
    function getWillDirection(position, box) {
      var direction = lastDirection;
      if (position.x < box.left) {
        direction = "right";
      }
      if (position.x > box.right) { 
        direction = "left"
      }
      if (position.y < box.top) {
        direction = "bottom";
      }
      if (position.y > box.bottom) {
        direction = "top";
      }
      return direction;
    }
    function getScale(direction, angle) { 
      switch(direction){
        case "top":
          vertical = -1;
          if (angle < dim_half_past_PI) {
            horizen = 1;
          }
          if (angle > dim_half_past_PI) {
            horizen = -1;
          }
          if (angle == dim_half_past_PI) {
            horizen = 0;
          }
          break;
        case "left":
          horizen = -1;
          if (angle > dim_half_past_PI) {
            vertical = 1;
          }
          if (angle < dim_half_past_PI) {
            vertical = -1;
          }
          if (angle == dim_half_past_PI) {
            vertical = 0;
          }
          break;
        case "bottom":
          vertical = 1;
          if(angle > dim_half_past_PI) {
            horizen = 1;
          }
          if(angle < dim_half_past_PI) {
            horizen = -1;
          }
          if(angle == dim_half_past_PI) {
            horizen = 0;
          }
          break;
        case "right":
          horizen = 1;
          if (angle > dim_half_past_PI) {
            vertical = -1;
          }
          if (angle < dim_half_past_PI) {
            vertical = 1;
          }
          if (angle == dim_half_past_PI) {
            vertical = 0;
          }
          break;
      }
    }
    function getOutAngle(inAngle) {
      if (inAngle == dim_half_past_PI || inAngle == 0) {
        return inAngle;
      } else {
        return dim_half_past_PI - inAngle;
      }
    }
    function setPosition(obj, position) {
      obj.css({ "left": position.x +"px", "top": position.y +"px"});
    }
    function run(obj) {
      var vx = Math.cos(lastAngle) * v;
      var vy = Math.sin(lastAngle) * v;
      var runTime = (new Date().getTime() - lastTime) / 1000;
      getScale(lastDirection, lastAngle);
      var sx = vx * runTime * horizen;
      var sy = vy * runTime * vertical;
      var position = {
        x: lastPosition.x + sx,
        y: lastPosition.y + sy
      };
      setPosition(obj, position);
      var currentDirection = getWillDirection(position, box);
      console.log(currentDirection +":"+lastDirection+":"+vertical+":"+horizen+":"+lastAngle+":"+dim_half_past_PI);
      // 如果沒有發生碰撞
      if (currentDirection != lastDirection) {
        // 如果發生了碰撞
        lastDirection = currentDirection;
        lastPosition = position;
        lastTime = new Date().getTime();
        lastAngle = getOutAngle(lastAngle);
      }
      setTimeout(function () {
        run(obj);
      }, 30);
    }
    $(document).ready(function () {
      var boxer = $("#box");
      var x = parseInt(boxer.offset().left);
      var y = parseInt(boxer.offset().top);
      box = {
        left: x,
        top: y,
        right: x + boxer.width(),
        bottom: y + boxer.height()
      };
      var runner = $("#runner");
      lastTime = new Date().getTime();
      lastPosition = {
        x: x,
        y: y + boxer.height()
      };
      run(runner);
    });
  </script>
  <style type="text/css" >
  body { margin:0; padding:0; }
  #box { margin:0 auto; width:500px; height:500px; border:3px solid #DDDDDD; border-radius:4px; -wekit-border-radius:4px; -moz-border-radius:4px;}
  #runner { width:10px; height:10px; font-size:10px; color:black; padding:0; position:absolute; left:100px; top:480px;}
  </style>
</head>
<body>
<div id="box">
<div id="runner">●</div>
</div>
</body>
</html>

更多關于jQuery特效相關內容感興趣的讀者可查看本站專題:《jQuery常見經典特效匯總》及《jQuery動畫與特效用法總結》

希望本文所述對大家jQuery程序設計有所幫助。



主站蜘蛛池模板: 求职者| 湖北卫视在线直播| 远景山谷 (1981)中字| 美网直播| 幼儿园老师锦旗赠言| 密使2之江都谍影 2013 于震| 超在线视频| xmx| 李采潭全部作品| 肖央喜剧电影《情圣》| 护校队申请书| 电影儿媳| 时诗个人资料| 第一财经直播电视直播| 太上老君说五斗金章受生经| 狂魔电影| 高中历史知识点总结| 时来运转电影| 安多卫视直播在线观看| 荒岛大逃亡电影在线观看| 女人高潮私密按摩视频| 难忘的运动会作文| 三年片观看免费完整版中文版| cctv17农业农村频道在线直播| 青楼春凳打板子作文| 清水美里| 特种部队电影全集观看| 江湖之社团风暴| 消防给水及消火栓系统技术规范 | 慕思成| 小淳| 电影壮志凌云女版满天星法版在线看| 瑜伽焰口全集 简体字| 歌手蔡国庆个人简历| 狂野殴美激情性bbbbbb| 电影《salawahan》| 在线麻豆| 红髅| 饥渴的少妇电影完整版| 女同性恋视频网站| 头文字d里演员表|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103