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

130
Views
MouseEvent.movementX e Y es diferente entre Firefox, Chrome y Edge

Estoy tratando de implementar un simple arrastre para mi proyecto de interfaz. A continuación se muestra el ejemplo de trabajo mínimo. En esencia, configuro position: relative para poder controlar la posición del elemento a través style.left y style.top , y uso MouseEvent.movementX y MouseEvent.movementY para actualizar la posición de acuerdo con los movimientos del cursor.

 <!DOCTYPE html> <head> <title>Drag-n-drop</title> <style> .dragme { position: relative; width: 100px; height: 100px; background-color: grey; } </style> </head> <body> <div class="dragme"></div> <script> const box = document.querySelector(".dragme"); box.addEventListener("mousedown", dragStart); window.addEventListener("mouseup", dragStop); function dragStart(event) { window.addEventListener("mousemove", dragging); } function dragging(event) { const style = window.getComputedStyle(box); box.style.left = `calc(${style.left} + ${event.movementX}px)`; box.style.top = `calc(${style.top} + ${event.movementY}px)`; } function dragStop(event) { window.removeEventListener("mousemove", dragging); } </script> </body> </html>

El problema es que este enfoque funciona según lo previsto en Firefox, pero en Chrome y Edge, el elemento arrastrado se mueve más rápido que el cursor. Después de investigar un poco, me di cuenta de que el origen del problema es la pantalla de alto DPI. Reemplazar event.movementX por event.movementX / window.devicePixelRatio etc. hace que Chrome y Edge realicen el arrastre sin problemas, pero Firefox no.

La única solución de navegador cruzado que se me ocurrió es usar MouseEvent.screenX y MouseEvent.screenY en su lugar y estoy perfectamente bien con eso. Sin embargo, todavía tengo curiosidad, ¿hay alguna forma de poder usar MouseEvent.movementX y MouseEvent.movementY en diferentes navegadores?

about 4 years ago · Juan Pablo Isaza
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!