intento 3

This commit is contained in:
David Itehua Xalamihua 2025-05-21 21:00:26 -06:00
parent de0c584f68
commit 52369e0479

View File

@ -1,4 +1,3 @@
<style>
.floating-btn {
position: fixed;
@ -35,8 +34,6 @@
.rotating {
animation: spin-right 4s linear infinite;
}
</style>
<div class="floating-btn border border-light shadow-lg" id="floatingBtn">
@ -45,15 +42,9 @@
alt="logo"
class="img-fluid rounded-circle rotating"
style="width: 100%; height: 100%;">
</a>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const btn = document.getElementById('floatingBtn');
@ -61,8 +52,9 @@
let offsetX, offsetY;
let isDragging = false;
let hasMoved = false;
let touchStartX = 0;
let touchStartY = 0;
let touchStartTimer;
const tapThreshold = 200; // Tiempo en milisegundos para considerar un "tap"
const moveThreshold = 5; // Distancia en píxeles para considerar un "move"
const savedPosition = localStorage.getItem('floatingBtnPosition');
if (savedPosition) {
@ -75,17 +67,16 @@
btn.style.top = '20px';
}
// MOUSE EVENTS
// Eventos para mouse
btn.addEventListener('mousedown', startDrag);
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', stopDrag);
// TOUCH EVENTS
btn.addEventListener('touchstart', startDragTouch, { passive: false });
document.addEventListener('touchmove', dragTouch, { passive: false });
document.addEventListener('touchend', stopDragTouch);
// Eventos para touch
btn.addEventListener('touchstart', startTouch, { passive: true });
document.addEventListener('touchmove', moveTouch, { passive: false });
document.addEventListener('touchend', endTouch);
// Evitar abrir link si se arrastró el botón
link.addEventListener('click', function (e) {
if (hasMoved) {
e.preventDefault();
@ -121,7 +112,7 @@
hasMoved = true;
}
function stopDrag(e) {
function stopDrag() {
if (!isDragging) return;
isDragging = false;
@ -135,60 +126,55 @@
savePosition(x, y);
}
function startDragTouch(e) {
function startTouch(e) {
if (e.touches.length !== 1) return;
isDragging = true;
hasMoved = false;
const touch = e.touches[0];
const startX = touch.clientX;
const startY = touch.clientY;
touchStartTimer = setTimeout(() => {
// Si el temporizador termina y no ha habido movimiento, consideramos un tap
link.click();
}, tapThreshold);
// Preparar para el movimiento
const rect = btn.getBoundingClientRect();
offsetX = e.touches[0].clientX - rect.left;
offsetY = e.touches[0].clientY - rect.top;
offsetX = startX - rect.left;
offsetY = startY - rect.top;
}
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
function moveTouch(e) {
if (!touchStartTimer || e.touches.length !== 1) return;
const touch = e.touches[0];
const currentX = touch.clientX;
const currentY = touch.clientY;
// Verificar si el movimiento supera el umbral
if (Math.abs(currentX - (touch.clientX - offsetX)) > moveThreshold || Math.abs(currentY - (touch.clientY - offsetY)) > moveThreshold) {
clearTimeout(touchStartTimer); // Cancelar el temporizador si hay movimiento
isDragging = true;
hasMoved = true;
btn.style.cursor = 'grabbing';
btn.style.left = `${rect.left}px`;
btn.style.top = `${rect.top}px`;
btn.style.left = `${currentX - offsetX}px`;
btn.style.top = `${currentY - offsetY}px`;
btn.style.right = 'auto';
e.preventDefault();
e.preventDefault(); // Evitar el scroll durante el arrastre
}
}
function dragTouch(e) {
if (!isDragging || e.touches.length !== 1) return;
const currentX = e.touches[0].clientX;
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.top = `${y}px`;
e.preventDefault(); // evita scroll
}
function stopDragTouch(e) {
if (!isDragging) return;
function endTouch(e) {
clearTimeout(touchStartTimer);
if (isDragging) {
isDragging = false;
btn.style.cursor = 'move';
const rect = btn.getBoundingClientRect();
const x = rect.left;
const y = rect.top;
stickToNearestSide(x, y);
savePosition(x, y);
stickToNearestSide(rect.left, rect.top);
savePosition(rect.left, rect.top);
}
}
function stickToNearestSide(x, y) {