|
|
@@ -0,0 +1,47 @@
|
|
|
+export const draggableFun = (dom: any) => {
|
|
|
+ const draggable: any = document.getElementById(dom);
|
|
|
+
|
|
|
+ let isDragging = false;
|
|
|
+ let currentX: any;
|
|
|
+ let currentY: any;
|
|
|
+ let initialX: any;
|
|
|
+ let initialY: any;
|
|
|
+ let xOffset = 0;
|
|
|
+ let yOffset = 0;
|
|
|
+
|
|
|
+ draggable.addEventListener("mousedown", dragStart);
|
|
|
+ document.addEventListener("mouseup", dragEnd);
|
|
|
+ document.addEventListener("mousemove", drag);
|
|
|
+
|
|
|
+ function dragStart(e: any) {
|
|
|
+ initialX = e.clientX - xOffset;
|
|
|
+ initialY = e.clientY - yOffset;
|
|
|
+
|
|
|
+ if (e.target === draggable) {
|
|
|
+ isDragging = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function drag(e: any) {
|
|
|
+ if (isDragging) {
|
|
|
+ e.preventDefault();
|
|
|
+ currentX = e.clientX - initialX;
|
|
|
+ currentY = e.clientY - initialY;
|
|
|
+
|
|
|
+ xOffset = currentX;
|
|
|
+ yOffset = currentY;
|
|
|
+
|
|
|
+ setTranslate(currentX, currentY, draggable);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function setTranslate(xPos: any, yPos: any, el: any) {
|
|
|
+ el.style.transform = `translate3d(${xPos}px, ${yPos}px, 0)`;
|
|
|
+ }
|
|
|
+
|
|
|
+ function dragEnd() {
|
|
|
+ initialX = currentX;
|
|
|
+ initialY = currentY;
|
|
|
+ isDragging = false;
|
|
|
+ }
|
|
|
+};
|