@@ -11,37 +11,54 @@ colorInput.addEventListener("input", function () {
1111dragElement ( document . getElementById ( "settings" ) ) ;
1212
1313function dragElement ( elmnt ) {
14- var pos1 = 0 , pos2 = 0 , pos3 = 0 , pos4 = 0 ;
15-
14+ let pos1 = 0 , pos2 = 0 , pos3 = 0 , pos4 = 0 ;
15+
16+ // Mouse events
1617 elmnt . onmousedown = dragMouseDown ;
1718
19+ // Touch events
20+ elmnt . ontouchstart = dragTouchStart ;
21+
1822 function dragMouseDown ( e ) {
19- e = e || window . event ;
2023 e . preventDefault ( ) ;
21- // get the mouse cursor position at startup:
2224 pos3 = e . clientX ;
2325 pos4 = e . clientY ;
2426 document . onmouseup = closeDragElement ;
25- // call a function whenever the cursor moves:
26- document . onmousemove = elementDrag ;
27+ document . onmousemove = elementDragMouse ;
2728 }
2829
29- function elementDrag ( e ) {
30- e = e || window . event ;
30+ function elementDragMouse ( e ) {
3131 e . preventDefault ( ) ;
32- // calculate the new cursor position:
3332 pos1 = pos3 - e . clientX ;
3433 pos2 = pos4 - e . clientY ;
3534 pos3 = e . clientX ;
3635 pos4 = e . clientY ;
37- // set the element's new position:
36+ elmnt . style . top = ( elmnt . offsetTop - pos2 ) + "px" ;
37+ elmnt . style . left = ( elmnt . offsetLeft - pos1 ) + "px" ;
38+ }
39+
40+ function dragTouchStart ( e ) {
41+ e . preventDefault ( ) ;
42+ pos3 = e . touches [ 0 ] . clientX ;
43+ pos4 = e . touches [ 0 ] . clientY ;
44+ document . ontouchend = closeDragElement ;
45+ document . ontouchmove = elementDragTouch ;
46+ }
47+
48+ function elementDragTouch ( e ) {
49+ e . preventDefault ( ) ;
50+ pos1 = pos3 - e . touches [ 0 ] . clientX ;
51+ pos2 = pos4 - e . touches [ 0 ] . clientY ;
52+ pos3 = e . touches [ 0 ] . clientX ;
53+ pos4 = e . touches [ 0 ] . clientY ;
3854 elmnt . style . top = ( elmnt . offsetTop - pos2 ) + "px" ;
3955 elmnt . style . left = ( elmnt . offsetLeft - pos1 ) + "px" ;
4056 }
4157
4258 function closeDragElement ( ) {
43- // stop moving when mouse button is released:
4459 document . onmouseup = null ;
4560 document . onmousemove = null ;
61+ document . ontouchend = null ;
62+ document . ontouchmove = null ;
4663 }
47- }
64+ }
0 commit comments