drag.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var insideBox = false;
  2. dragElement(document.getElementById("1"));
  3. dragElement(document.getElementById("2"));
  4. dragElement(document.getElementById("3"));
  5. dragElement(document.getElementById("4"));
  6. function dragElement(elmnt) {
  7. // if insideBox return; Besta
  8. var pos1, pos2, pos3, pos4;
  9. elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  10. elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  11. if (document.getElementById(elmnt.id + "header")) {
  12. // if present, the header is where you move the DIV from:
  13. document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  14. } else {
  15. // otherwise, move the DIV from anywhere inside the DIV:
  16. elmnt.onmousedown = dragMouseDown;
  17. }
  18. function dragMouseDown(e) {
  19. e = e || window.event;
  20. e.preventDefault();
  21. // get the mouse cursor position at startup:
  22. pos3 = e.clientX;
  23. pos4 = e.clientY;
  24. document.onmouseup = closeDragElement;
  25. // call a function whenever the cursor moves:
  26. document.onmousemove = elementDrag;
  27. }
  28. function elementDrag(e) {
  29. e = e || window.event;
  30. e.preventDefault();
  31. // calculate the new cursor position:
  32. pos1 = pos3 - e.clientX;
  33. pos2 = pos4 - e.clientY;
  34. pos3 = e.clientX;
  35. pos4 = e.clientY;
  36. // set the element's new position:
  37. elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  38. elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  39. /* if pos1 and pos2 lie within range of limits of box, insideBox = true, freeze ids 1,2,3,4 Besta man */
  40. }
  41. function closeDragElement() {
  42. // stop moving when mouse button is released:
  43. // write logic if object is in box, change captcha parameters
  44. document.onmouseup = null;
  45. document.onmousemove = null;
  46. }
  47. }
  48. // if insideBox {
  49. // document.getElementById("1").disabled = true;
  50. // document.getElementById("2").disabled = true;
  51. // document.getElementById("3").disabled = true;
  52. // document.getElementById("4").disabled = true;
  53. // }