Skip to content

Instantly share code, notes, and snippets.

@azizkale
Last active October 30, 2020 07:00
Show Gist options
  • Save azizkale/58ce2bee9696dbc487826bc43b21e444 to your computer and use it in GitHub Desktop.
Save azizkale/58ce2bee9696dbc487826bc43b21e444 to your computer and use it in GitHub Desktop.
01-How To Drag My Div
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