Guest User

Untitled

a guest
Oct 30th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 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. #container span {
  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. font-weight: 700;
  39. text-align: center;
  40. font-size: 25px;
  41. margin-left: 2px;
  42. }
  43.  
  44. #container span:active {
  45. background-color: rgba(0,100,100, 1.00);
  46. }
  47.  
  48. img {
  49. margin: 10px;
  50. }
  51.  
  52. .sidebar {
  53. width: 19%;
  54. position: relative;
  55. float: right;
  56. }
  57.  
  58. .sidebar input[type="text"] {
  59. width: 20%;
  60. }
  61.  
  62. .sidebar input {
  63. margin-bottom: 10%;
  64. }
  65.  
  66. .sidebar label {
  67. font-weight: 700;
  68. }
  69. </style>
  70. </head>
  71.  
  72. <body>
  73. <div class="main-container">
  74. <div id="outerContainer">
  75. <div id="container">
  76. <!--<span id="item">1</span>-->
  77. </div>
  78. </div>
  79.  
  80. <img id="output">
  81. <script>
  82. var loadFile = function(event) {
  83. var reader = new FileReader();
  84. reader.onload = function(){
  85. var output = document.getElementById('output');
  86. output.src = reader.result;
  87. };
  88. reader.readAsDataURL(event.target.files[0]);
  89. };
  90. </script>
  91. </div>
  92. <div class="sidebar">
  93. <h1>Dodawanie elementów</h1>
  94. <label>Dodaj obraz:
  95. <input type="file" accept="image/*" onchange="loadFile(event)"> </label>
  96. <label>Wpisz ilość śrubek:
  97. <input type="text" name="lsrubki" value="" id="ilosc"> </label>
  98. <button onclick="ilosc()">Dodaj</button>
  99. </div>
  100.  
  101. <script>
  102. function ilosc(){
  103. var ilosc = document.getElementById("ilosc").value;
  104.  
  105. for(var i = 1; i <= ilosc; i++) {
  106. const element1 = document.createElement("span");
  107.  
  108. element1.id = "item"+i;
  109.  
  110. element1.innerText = i;
  111.  
  112.  
  113. const div = document.querySelector("#container");
  114. div.appendChild(element1);
  115.  
  116. var dragItem = document.querySelector("#item");
  117. var container = document.querySelector("#container");
  118.  
  119. var active = false;
  120. var currentX;
  121. var currentY;
  122. var initialX;
  123. var initialY;
  124. var xOffset = 0;
  125. var yOffset = 0;
  126.  
  127. container.addEventListener("touchstart", dragStart, false);
  128. container.addEventListener("touchend", dragEnd, false);
  129. container.addEventListener("touchmove", drag, false);
  130.  
  131. container.addEventListener("mousedown", dragStart, false);
  132. container.addEventListener("mouseup", dragEnd, false);
  133. container.addEventListener("mousemove", drag, false);
  134.  
  135. function dragStart(e) {
  136. if (e.type === "touchstart") {
  137. initialX = e.touches[0].clientX - xOffset;
  138. initialY = e.touches[0].clientY - yOffset;
  139. } else {
  140. initialX = e.clientX - xOffset;
  141. initialY = e.clientY - yOffset;
  142. }
  143.  
  144. if (e.target === dragItem) {
  145. active = true;
  146. }
  147. }
  148.  
  149. function dragEnd(e) {
  150. initialX = currentX;
  151. initialY = currentY;
  152.  
  153. active = false;
  154. }
  155.  
  156. function drag(e) {
  157. if (active) {
  158. e.preventDefault();
  159.  
  160. if (e.type === "touchmove") {
  161. currentX = e.touches[0].clientX - initialX;
  162. currentY = e.touches[0].clientY - initialY;
  163. } else {
  164. currentX = e.clientX - initialX;
  165. currentY = e.clientY - initialY;
  166. }
  167.  
  168. xOffset = currentX;
  169. yOffset = currentY;
  170.  
  171. setTranslate(currentX, currentY, dragItem);
  172. }
  173. }
  174.  
  175. function setTranslate(xPos, yPos, el) {
  176. el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
  177. }
  178. }
  179.  
  180. }
  181.  
  182.  
  183. </script>
  184. </body>
  185.  
  186. </html>
Advertisement
Add Comment
Please, Sign In to add comment