Guest User

draggable/sticker

a guest
Sep 25th, 2026
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.63 KB | None | 0 0
  1. <style>
  2.     #sticker{
  3. position: absolute;
  4. z-index:999;
  5. cursor:move;
  6. }
  7. </style>
  8.  
  9. <div id=sticker style="top:0px; left:0px; transform: rotate(0deg);"><img src="image.png"  width=100px alt=""></div>
  10.  
  11. <script>
  12.     dragElement(document.getElementById("sticker"));
  13.  
  14. function dragElement(elmnt) {
  15.   var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  16.   if (document.getElementById(elmnt.id + "header")) {
  17.     /* if present, the header is where you move the DIV from:*/
  18.     document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  19.   } else {
  20.     /* otherwise, move the DIV from anywhere inside the DIV:*/
  21.     elmnt.onmousedown = dragMouseDown;
  22.   }
  23.  
  24.   function dragMouseDown(e) {
  25.     e = e || window.event;
  26.     e.preventDefault();
  27.     // get the mouse cursor position at startup:
  28.     pos3 = e.clientX;
  29.     pos4 = e.clientY;
  30.     document.onmouseup = closeDragElement;
  31.     // call a function whenever the cursor moves:
  32.     document.onmousemove = elementDrag;
  33.   }
  34.  
  35.   function elementDrag(e) {
  36.     e = e || window.event;
  37.     e.preventDefault();
  38.     // calculate the new cursor position:
  39.     pos1 = pos3 - e.clientX;
  40.     pos2 = pos4 - e.clientY;
  41.     pos3 = e.clientX;
  42.     pos4 = e.clientY;
  43.     // set the element's new position:
  44.     elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
  45.     elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  46.   }
  47.  
  48.   function closeDragElement() {
  49.     /* stop moving when mouse button is released:*/
  50.     document.onmouseup = null;
  51.     document.onmousemove = null;
  52.   }
  53. }
  54. </script>
  55.  
  56. <!--to add another, copy and paste all of this again and change "sticker" to "sticker2" and so forth-->
Advertisement
Add Comment
Please, Sign In to add comment