Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript] Przesuwane elementy strony
Forum PHP.pl > Forum > Przedszkole
patryk20120
Witam, mam kod, który umożliwia mi przesuwanie dowolnych elementów na stronie smile.gif lecz czy ktoś może mi powiedzieć jak ustawić dany np. <div> w pozycji np. 400,500(chodzi o współrzędne) tongue.gif

Oto kod:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <title>Przesuwane elementy strony :)</title>
  4. <style type="text/css">
  5. #cos {
  6. width: 100px;
  7. height: 100px;
  8. }
  9.  
  10. #cos1 {
  11. width: 200px;
  12. height: 140px;
  13. }
  14.  
  15. #cos2 {
  16. width: 125px;
  17. height: 189px;
  18. }
  19.  
  20.  
  21. .drag {border: 1px solid black; background-color: rgb(240, 240, 240); position: relative; padding: 0.5em; margin: 0 0 0.5em 1.5em; cursor: move;}
  22. th, td {text-align: left; padding-right: 1em;}
  23. table {margin: 0 0 0.4em 1.3em; border: 1px solid rgb(240, 240, 240);}
  24. </style>
  25.  
  26. </head>
  27. <div id="pageContainer">
  28. <h3>Przesuwane elementy strony:</h3><hr />
  29.  
  30. <div class="drag" id="cos">coś</div>
  31. <div class="drag"id="cos1">coś innego np.<br><textarea height=10 width=10></textarea></div>
  32. <div class="drag"id="cos2">też coś innego np. <input type=radio><input type=radio><input type=radio></div>
  33.  
  34.  
  35. </div>
  36.  
  37. <script language="JavaScript" type="text/javascript">
  38. <!--
  39.  
  40. // this is simply a shortcut for the eyes and fingers
  41. function $(id)
  42. {
  43. return document.getElementById(id);
  44. }
  45.  
  46. var _startX = 0; // mouse starting positions
  47. var _startY = 0;
  48. var _offsetX = 0; // current element offset
  49. var _offsetY = 0;
  50. var _dragElement; // needs to be passed from OnMouseDown to OnMouseMove
  51. var _oldZIndex = 0; // we temporarily increase the z-index during drag
  52. var _debug = $('debug'); // makes life easier
  53.  
  54.  
  55. InitDragDrop();
  56.  
  57. function InitDragDrop()
  58. {
  59. document.onmousedown = OnMouseDown;
  60. document.onmouseup = OnMouseUp;
  61. }
  62.  
  63. function OnMouseDown(e)
  64. {
  65. // IE is retarded and doesn't pass the event object
  66. if (e == null)
  67. e = window.event;
  68.  
  69. // IE uses srcElement, others use target
  70. var target = e.target != null ? e.target : e.srcElement;
  71.  
  72.  
  73. // for IE, left click == 1
  74. // for Firefox, left click == 0
  75. if ((e.button == 1 && window.event != null ||
  76. e.button == 0) &&
  77. target.className == 'drag')
  78. {
  79. // grab the mouse position
  80. _startX = e.clientX;
  81. _startY = e.clientY;
  82.  
  83. // grab the clicked element's position
  84. _offsetX = ExtractNumber(target.style.left);
  85. _offsetY = ExtractNumber(target.style.top);
  86.  
  87. // bring the clicked element to the front while it is being dragged
  88. _oldZIndex = target.style.zIndex;
  89. target.style.zIndex = 10000;
  90.  
  91. // we need to access the element in OnMouseMove
  92. _dragElement = target;
  93.  
  94. // tell our code to start moving the element with the mouse
  95. document.onmousemove = OnMouseMove;
  96.  
  97. // cancel out any text selections
  98. document.body.focus();
  99.  
  100. // prevent text selection in IE
  101. document.onselectstart = function () { return false; };
  102. // prevent IE from trying to drag an image
  103. target.ondragstart = function() { return false; };
  104.  
  105. // prevent text selection (except IE)
  106. return false;
  107. }
  108. }
  109.  
  110. function ExtractNumber(value)
  111. {
  112. var n = parseInt(value);
  113.  
  114. return n == null || isNaN(n) ? 0 : n;
  115. }
  116.  
  117. function OnMouseMove(e)
  118. {
  119. if (e == null)
  120. var e = window.event;
  121.  
  122. // this is the actual "drag code"
  123. _dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px';
  124. _dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px';
  125.  
  126. }
  127.  
  128. function OnMouseUp(e)
  129. {
  130. if (_dragElement != null)
  131. {
  132. _dragElement.style.zIndex = _oldZIndex;
  133.  
  134. // we're done with these events until the next OnMouseDown
  135. document.onmousemove = null;
  136. document.onselectstart = null;
  137. _dragElement.ondragstart = null;
  138.  
  139. // this is how we know we're not dragging
  140. _dragElement = null;
  141.  
  142. }
  143. }
  144. //-->
  145. </script>
  146.  
  147. </body>
  148.  
  149. </html>
r4xz
nie wiem czy dobrze cię zrozumiałem, ale chyba masz na myśli:
position: absolute; parametry

jako parametry podajesz odległość od krawędzi.
np.
left: 50px;
top: 300px;
patryk20120
Tak, dziękuję ;-)
A można to zrobić jeszcze inaczej, nie poprzez CSS, tylko JS questionmark.gif
ignas1987
Hmmm... fajnie by było gdyby można było dokować poszczególne elemety w danych komórkach np. tabeli... Oczywiście byłaby ukryta. Gdzieś takie coś widziałem, ale nie wiem gdzie... Może wiecie?
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.