Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@karthickvkumar
Created May 31, 2017 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthickvkumar/d44a57311ec93b169b7cced93b277007 to your computer and use it in GitHub Desktop.
Save karthickvkumar/d44a57311ec93b169b7cced93b277007 to your computer and use it in GitHub Desktop.
Javascript Native Drag
<div id="one">
</div>
window.onload = function(){
draggable('one');
};
var dragObj = null;
function draggable(id)
{
var obj = document.getElementById(id);
obj.style.position = "absolute";
obj.onmousedown = function(){
dragObj = obj;
}
}
document.onmouseup = function(e){
dragObj = null;
};
document.onmousemove = function(e){
var x = e.pageX;
var y = e.pageY;
if(dragObj == null)
return;
dragObj.style.left = x +"px";
dragObj.style.top= y +"px";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment