Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
Js movimiento aleatorio para cada elemento usando el interruptor

¡El problema que traté de usar el interruptor para el movimiento aleatorio funciona! pero todos los elementos se mueven con el mismo movimiento, no al azar, así que quiero que cada uno de ellos se mueva al azar

La salida: https://duck-hunt.ahmedbr123.repl.co/

RECUERDA

Quiero resolver el problema usando el interruptor

El código:

 for (var n = 0; n < boxr.length; ++n) { let lol = Math.floor(Math.random() * 8) + 1 let dml1 = parseInt(window.getComputedStyle(boxr[n]).getPropertyValue("left")); let dmt1 = parseInt(window.getComputedStyle(boxr[n]).getPropertyValue("top")); // I sit by myself coding to the moon trying get to you switch (lol) { case 1: boxr[n].style.transform = 'translate(-50%,-50%)' boxr[n].style.animation = 'animate 500ms steps(3) infinite' boxr[n].style.left = dml1 + 2 + 'px' break; case 2: boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' boxr[n].style.animation = 'animater 500ms steps(3) infinite' boxr[n].style.left = dml1 - sms + 'px' break; case 3: boxr[n].style.transform = 'translate(-50%,-50%)' boxr[n].style.animation = 'animatel 500ms steps(3) infinite' boxr[n].style.top = dmt1 - sms + 'px' break; case 4: if(boxr[n].style.animation = 'animatel 500ms steps(3) infinite' || boxr[n].style.animation == 'animate 500ms steps(3) infinite') { boxr[n].style.animation = 'animate 500ms steps(3) infinite' } else if(boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' && boxr[n].style.animation == 'animateS 500ms steps(3) infinite') { boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' boxr[n].style.animation = 'animate 500ms steps(3) infinite' } boxr[n].style.top = dmt1 + sms + 'px' break; case 5: boxr[n].style.transform = 'translate(-50%,-50%)' boxr[n].style.animation = 'animateS 500ms steps(3) infinite' boxr[n].style.left = dml1 + sms + 'px' boxr[n].style.top = dmt1 - sms + 'px' break; case 6: boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' boxr[n].style.animation = 'animateS 500ms steps(3) infinite' boxr[n].style.top = dmt1 - sms + 'px' boxr[n].style.left = dml1 - sms + 'px' break; case 7: boxr[n].style.transform = 'translate(-50%,-50%)' boxr[n].style.animation = 'animate 500ms steps(3) infinite' boxr[n].style.left = dml1 + sms + 'px' boxr[n].style.top = dmt1 + sms + 'px' break; case 8: boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' boxr[n].style.animation = 'animate 500ms steps(3) infinite' boxr[n].style.left = dml1 - sms + 'px' boxr[n].style.top = dmt1 + sms + 'px' break; }

Editar:

Mi problema es similar a este color aleatorio en diferentes div

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Debe establecer lol en un número aleatorio cada vez que pasa por el bucle, de modo que cada elemento obtenga diferentes movimientos aleatorios.

 for (var n = 0; n < boxr.length; ++n) { let dml1 = parseInt(window.getComputedStyle(boxr[n]).getPropertyValue("left")); let dmt1 = parseInt(window.getComputedStyle(boxr[n]).getPropertyValue("top")); let lol = Math.floor(Math.random() * 8) + 1 switch (lol) { case 1: boxr[n].style.transform = 'translate(-50%,-50%)' boxr[n].style.animation = 'animate 500ms steps(3) infinite' boxr[n].style.left = dml1 + 2 + 'px' break; case 2: boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' boxr[n].style.animation = 'animater 500ms steps(3) infinite' boxr[n].style.left = dml1 - sms + 'px' break; case 3: boxr[n].style.transform = 'translate(-50%,-50%)' boxr[n].style.animation = 'animatel 500ms steps(3) infinite' boxr[n].style.top = dmt1 - sms + 'px' break; case 4: if (boxr[n].style.animation = 'animatel 500ms steps(3) infinite' || boxr[n].style.animation == 'animate 500ms steps(3) infinite') { boxr[n].style.animation = 'animate 500ms steps(3) infinite' } else if (boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' && boxr[n].style.animation == 'animateS 500ms steps(3) infinite') { boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' boxr[n].style.animation = 'animate 500ms steps(3) infinite' } boxr[n].style.top = dmt1 + sms + 'px' break; case 5: boxr[n].style.transform = 'translate(-50%,-50%)' boxr[n].style.animation = 'animateS 500ms steps(3) infinite' boxr[n].style.left = dml1 + sms + 'px' boxr[n].style.top = dmt1 - sms + 'px' break; case 6: boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' boxr[n].style.animation = 'animateS 500ms steps(3) infinite' boxr[n].style.top = dmt1 - sms + 'px' boxr[n].style.left = dml1 - sms + 'px' break; case 7: boxr[n].style.transform = 'translate(-50%,-50%)' boxr[n].style.animation = 'animate 500ms steps(3) infinite' boxr[n].style.left = dml1 + sms + 'px' boxr[n].style.top = dmt1 + sms + 'px' break; case 8: boxr[n].style.transform = 'translate(-50%,-50%) scaleX(-1)' boxr[n].style.animation = 'animate 500ms steps(3) infinite' boxr[n].style.left = dml1 - sms + 'px' boxr[n].style.top = dmt1 + sms + 'px' break; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda