Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. <script type="text/javascript">
  2. //Make the DIV element draggagle:
  3. dragElement(document.getElementById("dxy"));
  4. function dragElement(elmnt) {
  5. var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  6. if (document.getElementById(elmnt.id + "dxy")) {
  7. /* if present, the header is where you move the DIV from:*/
  8. document.getElementById(elmnt.id + "dxy").onmousedown = dragMouseDown;
  9. } else {
  10. /* otherwise, move the DIV from anywhere inside the DIV:*/
  11. elmnt.onmousedown = dragMouseDown;
  12. }
  13. function dragMouseDown(e) {
  14. e = e || window.event;
  15. e.preventDefault();
  16. // get the mouse cursor position at startup:
  17. pos3 = e.clientX;
  18. pos4 = e.clientY;
  19. document.onmouseup = closeDragElement;
  20. // call a function whenever the cursor moves:
  21. document.onmousemove = elementDrag;
  22. }
  23. function elementDrag(e) {
  24. e = e || window.event;
  25. e.preventDefault();
  26. // calculate the new cursor position:
  27. pos1 = pos3 - e.clientX;
  28. pos2 = pos4 - e.clientY;
  29. pos3 = e.clientX;
  30. pos4 = e.clientY;
  31. // set the element's new position:
  32. elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  33. elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  34. }
  35. function closeDragElement() {
  36. /* stop moving when mouse button is released:*/
  37. document.onmouseup = null;
  38. document.onmousemove = null;
  39. }
  40. }
  41. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement