jquery – 检测div碰撞?

前端之家收集整理的这篇文章主要介绍了jquery – 检测div碰撞?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这就是我现在正在做的事情:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3.  
  4. <html lang="en">
  5. <head>
  6. <Meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>untitled</title>
  8. <Meta name="generator" content="TextMate http://macromates.com/">
  9. <style type="text/css" media="screen">
  10.  
  11. body { font-family:"HelveticaNeue-Light"; margin: 0px; }
  12. input { width: 75%; }
  13. #wrap { background: #f1f1f1; border: 1px solid #d9d9d9; padding: 0px; }
  14.  
  15. /* #sprite { position: relative; background: #909090; width: 20px; height: 20px; padding: 20px; }*/
  16. #spriteChatBubble { position: relative; background: #fff; border: 1px solid #000; font-size: 10px; max-width: 200px; }
  17.  
  18. #controlArea { margin-top: 50px; }
  19.  
  20. .button { background: #fff; color: #0080ff; padding: 5px; border: 1px solid #0050ff; text-decoration: none; }
  21. .button:active { background: #0080ff; color: #fff; }
  22.  
  23. </style>
  24.  
  25. <script type="text/javascript" charset="utf-8">
  26.  
  27. $(document).ready(function() {
  28.  
  29. // on load
  30. updatePos();
  31.  
  32. // get it ready
  33.  
  34. $("#sprite").css("background-image","url(left100by100.png)");
  35.  
  36.  
  37.  
  38. // our main block
  39.  
  40. function detectCollision() {
  41.  
  42.  
  43. var spritePos = $("#sprite").position();
  44. var spritePosLeft = spritePos.left;
  45. var spritePosTop = spritePos.top;
  46.  
  47. var chunkPos = $("#chunk").position();
  48. var chunkPosLeft = chunkPos.left;
  49. var chunkPosTop = chunkPos.top;
  50.  
  51.  
  52. // show the chunk's position values (test)
  53. $("#posLeftChunk").text(posLeftChunk);
  54.  
  55. if (spritePosLeft == chunkPosLeft || spritePosTop == chunkPosTop) {
  56.  
  57.  
  58. // make it go somewhere random! :D
  59.  
  60. var randomLeft = Math.floor(Math.random() * 100);
  61. var randomTop = Math.floor(Math.random() * -100);
  62.  
  63. // $("#chunk").hide();
  64.  
  65. $("#chunk").css("top",randomTop + "px");
  66. $("#chunk").css("left",randomLeft + "px");
  67.  
  68.  
  69. // $("#spriteChatBubble").animate({"top": "-=" + randomTop},"fast");
  70.  
  71. }
  72.  
  73. }
  74.  
  75. setInterval(detectCollision,500);
  76.  
  77.  
  78. function insertValues(name){
  79.  
  80. var name = "foop";
  81. var spritePosition = $("#sprite").position();
  82. var leftVal = spritePosition.left;
  83. var topVal = spritePosition.top;
  84.  
  85. }
  86.  
  87. insertValues("");
  88.  
  89.  
  90. function showMessage(message) {
  91.  
  92. $("#spriteChatBubble").show('slow');
  93. $("#messageText").text(message);
  94. $("#spriteChatBubble").delay(5000).hide('slow');
  95.  
  96. }
  97.  
  98. function updatePos() {
  99.  
  100. var position = $("#sprite").position();
  101. $("#posLeft").text(position.left);
  102. $("#posTop").text(position.top);
  103. // updatePos();
  104.  
  105. // insert the values into a database
  106. insertValues("");
  107.  
  108. }
  109.  
  110.  
  111.  
  112. $(document).keydown(function(ee) {
  113.  
  114. $("#spriteChatBubble").hide(); // no need to show this!
  115.  
  116. // if ($("#sprite").position.left < 0) {
  117. // alert();
  118. // } // fail
  119.  
  120. if (ee.keyCode == 37) {
  121.  
  122. // going left!
  123.  
  124. $("#sprite").css("background-image","url(left100by100.png)");
  125. // alert("going left!");
  126.  
  127. $("#sprite").animate({"left": "-=50px"},"fast");
  128. $("#spriteChatBubble").animate({"left": "-=50px"},"fast");
  129. updatePos();
  130. }
  131.  
  132. if(ee.keyCode == 39) {
  133.  
  134. // going right!
  135.  
  136. $("#sprite").css("background-image","url(right100by100.png)");
  137. // alert("going right!");
  138.  
  139. $("#sprite").animate({"left": "+=50px"},"fast");
  140. $("#spriteChatBubble").animate({"left": "+=50px"},"fast");
  141. updatePos();
  142. }
  143.  
  144. if(ee.keyCode == 38) {
  145. $("#sprite").animate({"top": "-=50px"},"fast");
  146. $("#spriteChatBubble").animate({"top": "-=50px"},"fast");
  147. updatePos();
  148. }
  149.  
  150. if(ee.keyCode == 40) {
  151. $("#sprite").animate({"top": "+=50px"},"fast");
  152. $("#spriteChatBubble").animate({"top": "+=50px"},"fast");
  153. updatePos();
  154. }
  155.  
  156.  
  157.  
  158. });
  159.  
  160. $("#left").click(function() {
  161. $("#sprite").animate({"left": "-=50px"},"fast");
  162. updatePos();
  163. });
  164.  
  165. $("#right").click(function() {
  166. $("#sprite").animate({"left": "+=50px"},"fast");
  167. updatePos();
  168. });
  169.  
  170. $("#talkButton").click(function() {
  171. showMessage($("#speakField").val());
  172. $("#speakField").val("") ;
  173. });
  174.  
  175. });
  176.  
  177.  
  178.  
  179.  
  180. </script>
  181. <!-- Date: 2011-05-23 -->
  182. </head>
  183. <body>
  184.  
  185. <!-- <div id="spriteChatBubble">
  186. <a class="mt" id="messageText">message</a>
  187. </div> -->
  188.  
  189. <!-- avatar (you,the player) -->
  190.  
  191. <div id="sprite" style="position: relative; background: url('right100by100.png'); width: 100px; height: 100px;">
  192.  
  193. </div>
  194.  
  195.  
  196. <!-- chunks -->
  197.  
  198. <div id="chunk" style="position: relative; background: #909090; width: 20px; height: 5px; top: 100px; left: 80px;"></div>
  199.  
  200. <div id="controlArea">
  201.  
  202. position.left: <a id="posLeft">0</a><br>
  203. position.top: <a id="posTop">0</a><br>
  204. <br><br>
  205. position.left (chunk): <a id="posLeftChunk">0</a><br>
  206. position.top (chunk): <a id="posTopChunk">0</a><br>
  207.  
  208. <!-- <input type="text" id="speakField" onchange="javascript:void(0);">
  209. <a href="javascript:void(0);" class="button" id="talkButton">talk!</a>
  210. <a href="javascript:void(0);" onclick="showMessage('foo');" class="button" id="">test it</a>
  211.  
  212. <br><br>
  213.  
  214. <a href="javascript:void(0);" class="button" id="left">(</a>
  215. <a href="javascript:void(0);" class="button" id="right">)</a>
  216. -->
  217. </div>
  218.  
  219.  
  220. </body>
  221. </html>

在所需的方向上移动50个像素,然后检测div碰撞(精灵击中“块”)然后需要重新定位.你知道更好的方法来检测div碰撞吗?谢谢.

解决方法

我会给你一个理论上的答案:

你想要计算你的div的界限 – 得到你的x,y角值,看看是否有任何其他div的边界与你的其他div的值相交.如果有一个坐标交叉点,那么你自己就会发生碰撞.

如何获得位置坐标:

  1. Top: $("#div").offset().top
  2. Left: $("#div").offset().left
  3. Bottom: $("#div").offset().top + $("#div").height()
  4. Right: $("#div").offset().left + $("#div").width()

猜你在找的jQuery相关文章