Created
December 24, 2015 11:46
-
-
Save stormbreakers/7cfad9658896da109fd4 to your computer and use it in GitHub Desktop.
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
drag: function () { | |
function startDrag(e) { | |
// determine event object | |
if (!e) { | |
var e = window.event; | |
} | |
if(e.preventDefault) e.preventDefault(); | |
// IE uses srcElement, others use target | |
targ = e.target ? e.target : e.srcElement; | |
if (targ.className != 'dragme') {return}; | |
// calculate event X, Y coordinates | |
offsetX = e.clientX; | |
offsetY = e.clientY; | |
// assign default values for top and left properties | |
if(!targ.style.left) { targ.style.left='0px'}; | |
if (!targ.style.top) { targ.style.top='0px'}; | |
// calculate integer values for top and left | |
// properties | |
coordX = parseInt(targ.style.left); | |
coordY = parseInt(targ.style.top); | |
drag = true; | |
// move div element | |
document.onmousemove=dragDiv; | |
document.touchmove=dragDiv; | |
return false; | |
} | |
function dragDiv(e) { | |
if (!drag) {return}; | |
if (!e) { var e= window.event}; | |
// var targ=e.target?e.target:e.srcElement; | |
// move div element | |
targ.style.left=coordX+e.clientX-offsetX+'px'; | |
targ.style.top=coordY+e.clientY-offsetY+'px'; | |
return false; | |
} | |
function stopDrag() { | |
drag=false; | |
} | |
document.onmousedown = startDrag; | |
document.touchstart = startDrag; | |
document.onmouseup = stopDrag; | |
document.touchend = stopDrag; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment