Guest User

Untitled

a guest
Oct 30th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
  6. <title>Drag</title>
  7. <style>
  8. .main-container {
  9. background-color: #cacaca;
  10. width: 80%;
  11. height: 98vh;
  12. border-radius: 7px;
  13. position: relative;
  14. float: left;
  15. }
  16.  
  17. #container {
  18. width: 80%;
  19. height: 100vh;
  20. display: flex;
  21. align-items: center;
  22. justify-content: center;
  23. overflow: hidden;
  24. border-radius: 7px;
  25. touch-action: none;
  26. position: absolute;
  27. z-index: 1
  28. }
  29.  
  30. #item {
  31. width: 30px;
  32. height: 30px;
  33. background-color: rgb(245, 230, 99);
  34. border: 2px solid rgba(0,0,0, .5);
  35. border-radius: 50%;
  36. touch-action: none;
  37. user-select: none;
  38. }
  39.  
  40. #item:active {
  41. background-color: rgba(0,100,100, 1.00);
  42. }
  43.  
  44. img {
  45. margin: 10px;
  46. }
  47.  
  48. .sidebar {
  49. width: 19%;
  50. position: relative;
  51. float: right;
  52. }
  53.  
  54. .sidebar input {
  55. width: 20%;
  56. }
  57. </style>
  58. </head>
  59.  
  60. <body>
  61. <div class="main-container">
  62. <div id="outerContainer">
  63. <div id="container">
  64. <span id="item" style="font-weight: 700;text-align: center;font-size: 25px;margin-left: 25%;">1</span>
  65. </div>
  66. </div>
  67. </div>
  68. <div class="sidebar">
  69. <h1>Dodawanie elementów</h1>
  70. <label>Wpisz ilość śrubek:
  71. <input type="text" name="lsrubki"> </label>
  72. <button>Dodaj</button>
  73. </div>
  74.  
  75. <script>
  76. var dragItem = document.querySelector("#item");
  77. var container = document.querySelector("#container");
  78.  
  79. var active = false;
  80. var currentX;
  81. var currentY;
  82. var initialX;
  83. var initialY;
  84. var xOffset = 0;
  85. var yOffset = 0;
  86.  
  87. container.addEventListener("touchstart", dragStart, false);
  88. container.addEventListener("touchend", dragEnd, false);
  89. container.addEventListener("touchmove", drag, false);
  90.  
  91. container.addEventListener("mousedown", dragStart, false);
  92. container.addEventListener("mouseup", dragEnd, false);
  93. container.addEventListener("mousemove", drag, false);
  94.  
  95. function dragStart(e) {
  96. if (e.type === "touchstart") {
  97. initialX = e.touches[0].clientX - xOffset;
  98. initialY = e.touches[0].clientY - yOffset;
  99. } else {
  100. initialX = e.clientX - xOffset;
  101. initialY = e.clientY - yOffset;
  102. }
  103.  
  104. if (e.target === dragItem) {
  105. active = true;
  106. }
  107. }
  108.  
  109. function dragEnd(e) {
  110. initialX = currentX;
  111. initialY = currentY;
  112.  
  113. active = false;
  114. }
  115.  
  116. function drag(e) {
  117. if (active) {
  118. e.preventDefault();
  119.  
  120. if (e.type === "touchmove") {
  121. currentX = e.touches[0].clientX - initialX;
  122. currentY = e.touches[0].clientY - initialY;
  123. } else {
  124. currentX = e.clientX - initialX;
  125. currentY = e.clientY - initialY;
  126. }
  127.  
  128. xOffset = currentX;
  129. yOffset = currentY;
  130.  
  131. setTranslate(currentX, currentY, dragItem);
  132. }
  133. }
  134.  
  135. function setTranslate(xPos, yPos, el) {
  136. el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
  137. }
  138. </script>
  139. </body>
  140.  
  141. </html>
Advertisement
Add Comment
Please, Sign In to add comment