intento 2
This commit is contained in:
parent
c92e8497e1
commit
de0c584f68
@ -61,6 +61,8 @@
|
|||||||
let offsetX, offsetY;
|
let offsetX, offsetY;
|
||||||
let isDragging = false;
|
let isDragging = false;
|
||||||
let hasMoved = false;
|
let hasMoved = false;
|
||||||
|
let touchStartX = 0;
|
||||||
|
let touchStartY = 0;
|
||||||
|
|
||||||
const savedPosition = localStorage.getItem('floatingBtnPosition');
|
const savedPosition = localStorage.getItem('floatingBtnPosition');
|
||||||
if (savedPosition) {
|
if (savedPosition) {
|
||||||
@ -73,16 +75,17 @@
|
|||||||
btn.style.top = '20px';
|
btn.style.top = '20px';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Eventos para mouse
|
// MOUSE EVENTS
|
||||||
btn.addEventListener('mousedown', startDrag);
|
btn.addEventListener('mousedown', startDrag);
|
||||||
document.addEventListener('mousemove', drag);
|
document.addEventListener('mousemove', drag);
|
||||||
document.addEventListener('mouseup', stopDrag);
|
document.addEventListener('mouseup', stopDrag);
|
||||||
|
|
||||||
// Eventos para touch
|
// TOUCH EVENTS
|
||||||
btn.addEventListener('touchstart', startDragTouch, { passive: false });
|
btn.addEventListener('touchstart', startDragTouch, { passive: false });
|
||||||
document.addEventListener('touchmove', dragTouch, { passive: false });
|
document.addEventListener('touchmove', dragTouch, { passive: false });
|
||||||
document.addEventListener('touchend', stopDragTouch);
|
document.addEventListener('touchend', stopDragTouch);
|
||||||
|
|
||||||
|
// Evitar abrir link si se arrastró el botón
|
||||||
link.addEventListener('click', function (e) {
|
link.addEventListener('click', function (e) {
|
||||||
if (hasMoved) {
|
if (hasMoved) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@ -118,7 +121,7 @@
|
|||||||
hasMoved = true;
|
hasMoved = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopDrag() {
|
function stopDrag(e) {
|
||||||
if (!isDragging) return;
|
if (!isDragging) return;
|
||||||
|
|
||||||
isDragging = false;
|
isDragging = false;
|
||||||
@ -134,7 +137,6 @@
|
|||||||
|
|
||||||
function startDragTouch(e) {
|
function startDragTouch(e) {
|
||||||
if (e.touches.length !== 1) return;
|
if (e.touches.length !== 1) return;
|
||||||
|
|
||||||
isDragging = true;
|
isDragging = true;
|
||||||
hasMoved = false;
|
hasMoved = false;
|
||||||
|
|
||||||
@ -142,28 +144,40 @@
|
|||||||
offsetX = e.touches[0].clientX - rect.left;
|
offsetX = e.touches[0].clientX - rect.left;
|
||||||
offsetY = e.touches[0].clientY - rect.top;
|
offsetY = e.touches[0].clientY - rect.top;
|
||||||
|
|
||||||
|
touchStartX = e.touches[0].clientX;
|
||||||
|
touchStartY = e.touches[0].clientY;
|
||||||
|
|
||||||
btn.style.cursor = 'grabbing';
|
btn.style.cursor = 'grabbing';
|
||||||
btn.style.left = `${rect.left}px`;
|
btn.style.left = `${rect.left}px`;
|
||||||
btn.style.top = `${rect.top}px`;
|
btn.style.top = `${rect.top}px`;
|
||||||
btn.style.right = 'auto';
|
btn.style.right = 'auto';
|
||||||
|
|
||||||
e.preventDefault(); // evita scroll
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
function dragTouch(e) {
|
function dragTouch(e) {
|
||||||
if (!isDragging || e.touches.length !== 1) return;
|
if (!isDragging || e.touches.length !== 1) return;
|
||||||
|
|
||||||
const x = e.touches[0].clientX - offsetX;
|
const currentX = e.touches[0].clientX;
|
||||||
const y = e.touches[0].clientY - offsetY;
|
const currentY = e.touches[0].clientY;
|
||||||
|
|
||||||
|
const deltaX = Math.abs(currentX - touchStartX);
|
||||||
|
const deltaY = Math.abs(currentY - touchStartY);
|
||||||
|
|
||||||
|
if (deltaX > 5 || deltaY > 5) {
|
||||||
|
hasMoved = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const x = currentX - offsetX;
|
||||||
|
const y = currentY - offsetY;
|
||||||
|
|
||||||
btn.style.left = `${x}px`;
|
btn.style.left = `${x}px`;
|
||||||
btn.style.top = `${y}px`;
|
btn.style.top = `${y}px`;
|
||||||
|
|
||||||
hasMoved = true;
|
|
||||||
e.preventDefault(); // evita scroll
|
e.preventDefault(); // evita scroll
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopDragTouch() {
|
function stopDragTouch(e) {
|
||||||
if (!isDragging) return;
|
if (!isDragging) return;
|
||||||
|
|
||||||
isDragging = false;
|
isDragging = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user