Last active
October 30, 2020 07:00
-
-
Save azizkale/58ce2bee9696dbc487826bc43b21e444 to your computer and use it in GitHub Desktop.
01-How To Drag My Div
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
var elment = document.getElementById("idOfMyDiv"); | |
elment.addEventListener('mousedown', function (e) { | |
isDown = true; | |
offset = [ | |
elment.offsetLeft - e.clientX, | |
elment.offsetTop - e.clientY | |
]; | |
}, true); | |
document.addEventListener('mouseup', function () { | |
isDown = false; | |
}, true); | |
document.addEventListener('mousemove', function (event) { | |
event.preventDefault(); | |
if (isDown) { | |
mousePosition = { | |
x: event.clientX, | |
y: event.clientY | |
}; | |
elment.style.left = (mousePosition.x + offset[0]) + 'px'; | |
elment.style.top = (mousePosition.y + offset[1]) + 'px'; | |
} | |
}, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment