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

177
Views
¿Cómo puedo aumentar el margen de un elemento del Dom en mi guión?

Estoy tratando de mover el botón a través del cuerpo usando flechas. Está mal poner literales numéricos, y probé que el código está vinculado al HTML.

 document.body.addEventListener("keydown", function (evt) { const btn = document.querySelector('button'); switch(evt.code) { case 'ArrowUp': btn.style.marginBottom =+ 10; break; case 'ArrowDown': btn.style.marginTop =+ 10; break; case 'ArrowRight': btn.style.marginLeft += 10; break; case 'ArrowLeft': btn.style.marginRight =+ 10; break; default: } })
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

marginLeft y otros devuelven una cadena (con el texto "px"), por lo que debe eliminarla para hacer la suma.

 document.body.addEventListener("keydown", function (evt) { const btn = document.querySelector('#mybutton'); switch(evt.code) { case 'ArrowUp': btn.style.marginTop = (parseInt(btn.style.marginTop.replace("px","")) - 10) + "px"; break; case 'ArrowDown': btn.style.marginTop = (parseInt(btn.style.marginTop.replace("px","")) + 10) + "px"; break; case 'ArrowRight': btn.style.marginLeft = (parseInt(btn.style.marginLeft.replace("px","")) + 10) + "px"; break; case 'ArrowLeft': btn.style.marginLeft = (parseInt(btn.style.marginLeft.replace("px","")) - 10) + "px"; break; default: } })
 <div>before</div> <button id="mybutton" style="margin:1px">hello</button> <div>after</div>

over 4 years ago · Santiago Trujillo 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!