Skip to content

Instantly share code, notes, and snippets.

@soysantana
Last active August 4, 2023 17:34
Show Gist options
  • Save soysantana/5535bed751b92d3b1154c2b4020369cf to your computer and use it in GitHub Desktop.
Save soysantana/5535bed751b92d3b1154c2b4020369cf to your computer and use it in GitHub Desktop.
const element = document.getElementById("animateMe");
let positionX = 0;
let direction = 1;
function animate() {
positionX += 5 * direction;
element.style.transform = `translateX(${positionX}px)`;
// Cambiar dirección cuando llegue al borde derecho o izquierdo del contenedor
if (positionX >= window.innerWidth - element.clientWidth || positionX <= 0) {
direction *= -1;
}
}
setInterval(animate, 50); // Llamar a la función animate cada 50ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment