Guest User

Untitled

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