Created
April 17, 2021 06:41
-
-
Save bokunodev/f2b0b6dc130901973fbb4bc82d69ae8b to your computer and use it in GitHub Desktop.
make html element, dragable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dragableElement(document.querySelector("#mydiv")) | |
function dragableElement(a) { | |
let f = 0, | |
g = 0, | |
d = 0, | |
e = 0 | |
eh = a.querySelector("#header") || a | |
eh.onmousedown = function(b) { | |
b = b || window.event | |
b.preventDefault() | |
d = b.clientX | |
e = b.clientY | |
document.onmouseup = function() { | |
document.onmousemove = null | |
} | |
document.onmousemove = function(c) { | |
c.preventDefault() | |
f = d - c.clientX | |
g = e - c.clientY | |
d = c.clientX | |
e = c.clientY | |
a.style.top = a.offsetTop - g + "px" | |
a.style.left = a.offsetLeft - f + "px" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment