Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
Js random movement foreach element using switch

The problem I tried to using switch for random movement it works! but all the elements moving the same movement, not random, so I want each of them moves random

The output: https://duck-hunt.ahmedbr123.repl.co/

REMEMBER

I want to solve the problem by using switch

The code:

 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;
}

Edit:

My problem is similar to this Random color on different div's

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to set lol to a random number each time through the loop, so that each element gets different random movements.

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!