|
| 1 | + |
| 2 | +function toggleDrawer() |
| 3 | +{ |
| 4 | + document.getElementById('drawer').classList.toggle('inactive'); |
| 5 | +} |
| 6 | + |
| 7 | +// based on https://www.w3schools.com/howto/howto_js_draggable.asp |
| 8 | +function makeElementDraggable(elmnt) |
| 9 | +{ |
| 10 | + var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; |
| 11 | +/* |
| 12 | + if (document.getElementById(elmnt.id + "header")) |
| 13 | + { |
| 14 | + // if present, the header is where you move the DIV from: |
| 15 | + document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown; |
| 16 | + } |
| 17 | + else |
| 18 | + { |
| 19 | + // otherwise, move the DIV from anywhere inside the DIV: |
| 20 | +*/ |
| 21 | + elmnt.onmousedown = dragMouseDown; |
| 22 | +// } |
| 23 | + |
| 24 | + function dragMouseDown(e) |
| 25 | + { |
| 26 | + e = e || window.event; |
| 27 | + e.preventDefault(); |
| 28 | + // get the mouse cursor position at startup: |
| 29 | + pos3 = e.clientX; |
| 30 | + pos4 = e.clientY; |
| 31 | + console.log("Click: "+pos3+", "+pos4); |
| 32 | + document.onmouseup = closeDragElement; |
| 33 | + // call a function whenever the cursor moves: |
| 34 | + document.onmousemove = elementDrag; |
| 35 | + } |
| 36 | + |
| 37 | + function elementDrag(e) |
| 38 | + { |
| 39 | + e = e || window.event; |
| 40 | + e.preventDefault(); |
| 41 | + // calculate the new cursor position: |
| 42 | + pos1 = pos3 - e.clientX; |
| 43 | + pos2 = pos4 - e.clientY; |
| 44 | + console.log("Pos: "+e.clientX+", "+e.clientY+", drag: "+pos1+", "+pos2+", offsetLeftTop: "+elmnt.offsetLeft+", "+elmnt.offsetTop+", newPos: "+(elmnt.offsetLeft-pos1)+", "+(elmnt.offsetTop-pos2)); |
| 45 | + pos3 = e.clientX; |
| 46 | + pos4 = e.clientY; |
| 47 | + // set the element's new position: |
| 48 | + elmnt.style.top = (elmnt.offsetTop - pos2) + "px"; |
| 49 | + elmnt.style.left = (elmnt.offsetLeft - pos1) + "px"; |
| 50 | + } |
| 51 | + |
| 52 | + function closeDragElement() |
| 53 | + { |
| 54 | + // stop moving when mouse button is released: |
| 55 | + document.onmouseup = null; |
| 56 | + document.onmousemove = null; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +window.onload = function(e) |
| 61 | +{ |
| 62 | + makeElementDraggable(document.getElementById("drawer")); |
| 63 | +}; |
0 commit comments