Skip to content

Instantly share code, notes, and snippets.

@bokunodev
Created April 17, 2021 06:41
Show Gist options
  • Save bokunodev/f2b0b6dc130901973fbb4bc82d69ae8b to your computer and use it in GitHub Desktop.
Save bokunodev/f2b0b6dc130901973fbb4bc82d69ae8b to your computer and use it in GitHub Desktop.
make html element, dragable.
// 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