Guest User

Untitled

a guest
Feb 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. **style样式**
  2. ```
  3. <style>
  4. .box{
  5. width:100px;
  6. height:100px;
  7. background-color:#ff9c69;
  8. font-size:20px;
  9. position:absolute;
  10. left:50px;
  11. text-align:center;
  12. line-height:100px;
  13. user-select:none;
  14. }
  15. </style>
  16. ```
  17.  
  18. **文本内容**
  19. ```
  20. <div class="box"></div>
  21. <div class="box" style="background-color:red;margin-left:400px;"></div>
  22. ```
  23.  
  24. **js样式**
  25. ```
  26. <script>
  27. var box=document.getElementsByClassName("box")[0];
  28. var box2=document.getElementsByClassName("box")[1];
  29. box.onmousedown=function(e){
  30. var ev=e||event;
  31. var divX=ev.offsetX;
  32. var divY=ev.offsetY;
  33. document.onmosuemvoe=function(){
  34. var ev=e||event;
  35. var x=ev.clientX-divX;
  36. var y=ev.clientY-divY;
  37. box.style.left=x+"px";
  38. box.style.top=y+"px";
  39. var moveL=box.offsetLeft;
  40. var moveR=box.offsetLeft+box.offsetWidth;
  41. var moveT=box.offsetTop;
  42. var moveB=box.offsetTop+box.offsetHeight;
  43. var hitL=box2.offsetLeft;
  44. var hitR=box2.offsetLeft+box2.offsetWidth;
  45. var hitT=box2.offsetTop;
  46. var hitB=box2.offsetTop+box2.offsetHeight;
  47. var r1=moveR>=hitL;
  48. var r2=moveL<=hitR;
  49. var r3=moveT<=hitB;
  50. var r4=moveB>=hitT;
  51. if(r1&&r2&&r3&&r4){
  52. box2.style.backgroundColor="red";
  53. box2.innerHTML="被撞了";
  54. }else{
  55. box2.style.backgroundColor="red";
  56. box2.innerHTML="";
  57. }
  58. }
  59. }
  60. </script>
  61. ```
  62.  
  63. 情况就是这样了。
Add Comment
Please, Sign In to add comment