Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
76
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. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <img src="https://js.cx/clipart/ball.svg"
  7. style="cursor: pointer; position: absolute; left: 279px; top: 100px; z-index: 1000;" width="40" height="40"
  8. id="ball">
  9. </head>
  10. <body>
  11. <script>
  12. var ball = document.getElementById('ball');
  13.  
  14. ball.onmousedown = function (e) {
  15.  
  16. var coords = getCoords(ball);
  17. var shiftX = e.pageX - coords.left;
  18.  
  19.  
  20. ball.style.position = 'absolute';
  21. document.body.appendChild(ball);
  22. moveAt(e);
  23.  
  24. ball.style.zIndex = 1000; // над другими элементами
  25.  
  26. function moveAt(e) {
  27. ball.style.left = e.pageX - shiftX + 'px';
  28. }
  29.  
  30. document.onmousemove = function (e) {
  31. moveAt(e);
  32. };
  33.  
  34. ball.onmouseup = function () {
  35. document.onmousemove = null;
  36. ball.onmouseup = null;
  37. };
  38.  
  39. }
  40.  
  41. ball.ondragstart = function () {
  42. return false;
  43. };
  44.  
  45. function getCoords(elem) {
  46. var box = elem.getBoundingClientRect();
  47. return {
  48. left: box.left + pageXOffset
  49. };
  50. }
  51. </script>
  52. </body>
  53. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement