Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JS Bin</title>
  6. <style id="jsbin-css">
  7. #area {
  8. background-color: #ccc;
  9. width: 100px;
  10. height: 100px;
  11. position: absolute;
  12. }
  13.  
  14. #block {
  15. position: relative;
  16. width: 20px;
  17. height: 20px;
  18. background-color: #666;
  19. margin-top: 0px;
  20. margin-left: 0px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="area">
  26. <div id="block"></div>
  27. </div>
  28. <script id="jsbin-javascript">
  29. var block = document.getElementById('block');
  30. var block_style = window.getComputedStyle(block);
  31. var area = 100;
  32.  
  33. var initial_top = parseInt(block_style.marginTop);
  34. var initial_left = parseInt(block_style.marginLeft);
  35. var finish_top = initial_top + area - parseInt(block_style.width);
  36. var finish_left = initial_left + area - parseInt(block_style.height);
  37. var step = 1;
  38.  
  39. setInterval(function() {
  40. var top = parseInt(block_style.marginTop);
  41. var left = parseInt(block_style.marginLeft);
  42. if (top == initial_top && left < finish_left) {
  43. left += step;
  44. left = Math.min(left, finish_left);
  45. }
  46. else if (left >= finish_left && top < finish_top) {
  47. top += step;
  48. top = Math.min(top, finish_top);
  49. }
  50. else if (top >= finish_top && left > initial_left) {
  51. left -= step;
  52. left = Math.max(left, initial_left);
  53. }
  54. else if (left <= initial_left && top > initial_top) {
  55. top -= step;
  56. top = Math.max(top, initial_top);
  57. }
  58. block.style.marginTop = top + 'px';
  59. block.style.marginLeft = left + 'px';
  60. }, 10);
  61. </script>
  62.  
  63.  
  64. <script id="jsbin-source-css" type="text/css">#area {
  65. background-color: #ccc;
  66. width: 100px;
  67. height: 100px;
  68. position: absolute;
  69. }
  70.  
  71. #block {
  72. position: relative;
  73. width: 20px;
  74. height: 20px;
  75. background-color: #666;
  76. margin-top: 0px;
  77. margin-left: 0px;
  78. }</script>
  79.  
  80. <script id="jsbin-source-javascript" type="text/javascript">var block = document.getElementById('block');
  81. var block_style = window.getComputedStyle(block);
  82. var area = 100;
  83.  
  84. var initial_top = parseInt(block_style.marginTop);
  85. var initial_left = parseInt(block_style.marginLeft);
  86. var finish_top = initial_top + area - parseInt(block_style.width);
  87. var finish_left = initial_left + area - parseInt(block_style.height);
  88. var step = 1;
  89.  
  90. setInterval(function() {
  91. var top = parseInt(block_style.marginTop);
  92. var left = parseInt(block_style.marginLeft);
  93. if (top == initial_top && left < finish_left) {
  94. left += step;
  95. left = Math.min(left, finish_left);
  96. }
  97. else if (left >= finish_left && top < finish_top) {
  98. top += step;
  99. top = Math.min(top, finish_top);
  100. }
  101. else if (top >= finish_top && left > initial_left) {
  102. left -= step;
  103. left = Math.max(left, initial_left);
  104. }
  105. else if (left <= initial_left && top > initial_top) {
  106. top -= step;
  107. top = Math.max(top, initial_top);
  108. }
  109. block.style.marginTop = top + 'px';
  110. block.style.marginLeft = left + 'px';
  111. }, 10);</script></body>
  112. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement