Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions scripts/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
justify-content: center;
transition: all 0.3s ease;
}
.floating-button, .floating-button * {
-webkit-user-drag: none;
user-select: none;
-webkit-touch-callout: none;
}

.floating-button img {
pointer-events: none; /* let pointerdown/move pass straight to the button, not the img */
}
.button-image {
width: 95%;
height: 95%;
Expand Down
62 changes: 62 additions & 0 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,65 @@ new MutationObserver(()=> {
updateCompanies(lastUrl);
}}, 250)
}).observe(document, {subtree: true, childList: true})

// this function grabs that current location of the button and add some Event listeners to it
// if the button is dragged it increase or decrease dynamically the x,y coordinate of the button acc to the mouse current location

function makeDraggable(button) {
let isDragging = false;
let startX = 0;
let startY = 0;
let x, y;
let rafId = null;

// Get the button's REAL current position on screen
const rect = button.getBoundingClientRect();
x = rect.left;
y = rect.top;

button.style.position = "fixed";
button.style.left = "0";
button.style.top = "0";
button.style.right = "auto"; // clear any CSS that was positioning it
button.style.bottom = "auto";
button.style.margin = "0";
button.style.transform = `translate3d(${x}px, ${y}px, 0)`;
button.style.touchAction = "none";
button.style.cursor = "grab";

function applyTransform() {
button.style.transform = `translate3d(${x}px, ${y}px, 0)`;
rafId = null;
}

function endDrag(e) {
if (!isDragging) return;
isDragging = false;
if (button.hasPointerCapture(e.pointerId)) {
button.releasePointerCapture(e.pointerId);
}
button.style.cursor = "grab";
document.body.style.userSelect = "";
}

button.addEventListener("pointerdown", (e) => {
isDragging = true;
button.setPointerCapture(e.pointerId);
startX = e.clientX - x;
startY = e.clientY - y;
button.style.cursor = "grabbing";
document.body.style.userSelect = "none";
});

button.addEventListener("pointermove", (e) => {
if (!isDragging) return;
x = e.clientX - startX;
y = e.clientY - startY;
if (rafId === null) rafId = requestAnimationFrame(applyTransform);
});

button.addEventListener("pointerup", endDrag);
button.addEventListener("pointercancel", endDrag); // handles interrupted drags (OS gestures, alt-tab, etc.)
}

makeDraggable(button);