Advertisement
Guest User

Untitled

a guest
Oct 30th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. function ilosc(){
  2. var ilosc = document.getElementById("ilosc").value;
  3.  
  4. for(var i = 1; i <= ilosc; i++) {
  5. const element1 = document.createElement("span");
  6.  
  7. element1.id = "item"+i;
  8. element1.className = "item";
  9.  
  10. element1.innerText = i;
  11.  
  12.  
  13. const div = document.querySelector("#container");
  14. div.appendChild(element1);
  15.  
  16. var dragItem = document.querySelector("#item");
  17. var container = document.querySelector("#container");
  18.  
  19. var active = false;
  20. var currentX;
  21. var currentY;
  22. var initialX;
  23. var initialY;
  24. var xOffset = 0;
  25. var yOffset = 0;
  26. }
  27.  
  28. var items = document.querySelectorAll(".item");
  29.  
  30. items.forEach(function(item) {
  31. item.addEventListener("touchstart", dragStart, false);
  32. item.addEventListener("touchend", dragEnd, false);
  33. item.addEventListener("touchmove", drag, false);
  34.  
  35. item.addEventListener("mousedown", dragStart, false);
  36. item.addEventListener("mouseup", dragEnd, false);
  37. item.addEventListener("mousemove", drag, false);
  38. })
  39.  
  40.  
  41.  
  42. function dragStart(e) {
  43.  
  44. if (e.type === "touchstart") {
  45. initialX = e.touches[0].clientX - xOffset;
  46. initialY = e.touches[0].clientY - yOffset;
  47. } else {
  48. initialX = e.clientX - xOffset;
  49. initialY = e.clientY - yOffset;
  50. }
  51.  
  52. if (e.target === this) {
  53. active = true;
  54. }
  55. }
  56.  
  57. function dragEnd(e) {
  58. console.log(e)
  59. initialX = currentX;
  60. initialY = currentY;
  61.  
  62. active = false;
  63. }
  64.  
  65. function drag(e) {
  66. if (active) {
  67. e.preventDefault();
  68.  
  69. if (e.type === "touchmove") {
  70. currentX = e.touches[0].clientX - initialX;
  71. currentY = e.touches[0].clientY - initialY;
  72. } else {
  73. currentX = e.clientX - initialX;
  74. currentY = e.clientY - initialY;
  75. }
  76.  
  77. xOffset = currentX;
  78. yOffset = currentY;
  79.  
  80. setTranslate(currentX, currentY, this);
  81. }
  82. }
  83.  
  84. function setTranslate(xPos, yPos, el) {
  85. el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement