Update '12/dragable.js'

master
Max Lehmann 5 years ago
parent 865982702a
commit 78c8335ed5

@ -70,44 +70,44 @@ makeDragable('#shell_03header', '#shell_03')
document.querySelector(dragHandle).addEventListener("touchstart", startDrag, true); document.querySelector(dragHandle).addEventListener("touchstart", startDrag, true);
/*sets offset parameters and starts listening for mouse-move*/ /*sets offset parameters and starts listening for mouse-move*/
function startDrag(e) { function startDrag(x) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
dragObj2 = document.querySelector(dragTarget); dragObj2 = document.querySelector(dragTarget);
dragObj2.style.position = "absolute"; dragObj2.style.position = "absolute";
let rect = dragObj2.getBoundingClientRect(); let rect = dragObj2.getBoundingClientRect();
if (e.type=="mousedown") { if (x.type=="mousedown") {
xOffset2 = e.clientX - rect.left; //clientX and getBoundingClientRect() both use viewable area adjusted when scrolling aka 'viewport' xOffset2 = x.clientX - rect.left; //clientX and getBoundingClientRect() both use viewable area adjusted when scrolling aka 'viewport'
yOffset2 = e.clientY - rect.top; yOffset2 = x.clientY - rect.top;
window.addEventListener('mousemove', dragObject2, true); window.addEventListener('mousemove', dragObject2, true);
} else if(e.type=="touchstart") { } else if(e.type=="touchstart") {
xOffset2 = e.targetTouches[0].clientX - rect.left; xOffset2 = x.targetTouches[0].clientX - rect.left;
yOffset2 = e.targetTouches[0].clientY - rect.top; yOffset2 = x.targetTouches[0].clientY - rect.top;
window.addEventListener('touchmove', dragObject2, true); window.addEventListener('touchmove', dragObject2, true);
} }
} }
/*Drag object*/ /*Drag object*/
function dragObject(e) { function dragObject(x) {
e.preventDefault(); x.preventDefault();
e.stopPropagation(); x.stopPropagation();
if(dragObj == null) { if(dragObj == null) {
return; // if there is no object being dragged then do nothing return; // if there is no object being dragged then do nothing
} else if(e.type=="mousemove") { } else if(x.type=="mousemove") {
dragObj2.style.left = e.clientX-xOffset2 +"px"; // adjust location of dragged object so doesn't jump to mouse position dragObj2.style.left = x.clientX-xOffset2 +"px"; // adjust location of dragged object so doesn't jump to mouse position
dragObj2.style.top = e.clientY-yOffset2 +"px"; dragObj2.style.top = x.clientY-yOffset2 +"px";
} else if(e.type=="touchmove") { } else if(e.type=="touchmove") {
dragObj2.style.left = e.targetTouches[0].clientX-xOffset2 +"px"; // adjust location of dragged object so doesn't jump to mouse position dragObj2.style.left = x.targetTouches[0].clientX-xOffset2 +"px"; // adjust location of dragged object so doesn't jump to mouse position
dragObj2.style.top = e.targetTouches[0].clientY-yOffset2 +"px"; dragObj2.style.top = x.targetTouches[0].clientY-yOffset2 +"px";
} }
} }
/*End dragging*/ /*End dragging*/
document.onmouseup = function(e) { document.onmouseup = function(x) {
if (dragObj2) { if (dragObj2) {
dragObj = null; dragObj2 = null;
window.removeEventListener('mousemove', dragObject, true); window.removeEventListener('mousemove', dragObject, true);
window.removeEventListener('touchmove', dragObject, true); window.removeEventListener('touchmove', dragObject, true);
} }

Loading…
Cancel
Save