Advertisement
Guest User

MYSLEK

a guest
Feb 19th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>Projekt - Eventy -parametry </title>
  9. <style>
  10. div {
  11. position: fixed;
  12. width: 200px;
  13. height: 200px;
  14. cursor: pointer;
  15. background-color: #000;
  16. }
  17. </style>
  18. </head>
  19.  
  20. <body>
  21. <div></div>
  22.  
  23. <script>
  24.  
  25. const div = document.querySelector('div');
  26. let divX = 150;
  27. let divY = 50;
  28. div.style.left = divX + "px";
  29. div.style.top = `${divY}px`;
  30.  
  31. let drawActive = false;
  32.  
  33. let insertDivX;
  34. let insertDivY;
  35.  
  36. div.addEventListener('mousedown', (e) => {
  37. div.style.backgroundColor = "gray";
  38. drawActive = !drawActive;
  39. // drawActive = true;
  40.  
  41. insertDivX = e.offsetX;
  42. insertDivY = e.offsetY;
  43. console.log(insertDivX, insertDivY);
  44.  
  45. })
  46.  
  47. div.addEventListener('mousemove', (e) => {
  48. if (drawActive) {
  49.  
  50.  
  51. divX = e.clientX - insertDivX;
  52. divY = e.clientY - insertDivY;
  53. div.style.left = `${divX}px`;
  54. div.style.top = `${divY}px`;
  55. }
  56. })
  57.  
  58. div.addEventListener('mouseup', () => {
  59. div.style.backgroundColor = "black";
  60. drawActive = !drawActive;
  61. // drawActive = false;
  62. })
  63.  
  64. </script>
  65. </body>
  66.  
  67. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement